Example #1
1
 /**
  * Retrieves an specific Edition
  * specified by the IDs of
  * the article and the edition.
  *
  * @param int $articleID
  * @param int $editionID
  *
  * @throws ModelNotFoundException
  *
  * @return Edition
  **/
 public function editionAtIndex($articleID, $editionID)
 {
     $edition = Edition::where('id', $editionID)->where('article_id', $articleID)->get()->first();
     if ($edition == null) {
         $e = new ModelNotFoundException();
         $e->setModel('Edition');
         throw $e;
     }
     return $edition;
 }
 /**
  * Almost identical to getBy, but instead of returning null or empty collections, instead throws an exception.
  *
  * @param string $field
  * @param string $value
  * @return mixed
  * @throws ModelNotFoundException
  */
 public function requireBy($field, $value)
 {
     $model = $this->getBy($field, $value);
     if (!$model) {
         $exception = new ModelNotFoundException();
         $exception->setModel(get_class($this->model));
         throw $exception;
     }
     return $model;
 }
Example #3
0
 /**
  * Render a ModelNotFound exception into an HTTP response.
  *
  * @param  Illuminate\Database\Eloquent\ModelNotFoundException  $exception
  * @return \Illuminate\Http\RedirectResponse
  */
 public function renderModelnotFound(ModelNotFoundException $exception)
 {
     // Redirect when Account model not found
     if ($exception->getModel() === 'App\\Account') {
         return redirect()->action('HomeController@getIndex')->withErrors(trans('account.index.notfoundMessage'));
     }
     // Redirect when Envelope model not found
     if ($exception->getModel() === 'App\\Envelope') {
         return redirect()->action('HomeController@getIndex')->withErrors(trans('envelope.index.notfoundMessage'));
     }
     // Redirect when other model not found
     return redirect()->action('HomeController@getIndex')->withErrors([trans('app.error.404')]);
 }
 public function __construct($class, $id)
 {
     $reflection = new ReflectionClass($class);
     $model = $reflection->getShortName();
     $message = sprintf('Model `%s` not found by id `%s`. Try to set parent id in your model or use inner_hits feature.', $model, $id);
     parent::__construct($message);
 }
 public function getDecisions($size = null, $table_id = null, $variant_id = null)
 {
     /** @var \Jenssegers\Mongodb\Eloquent\Builder $query */
     if ($table_id) {
         $query = $this->getModel()->query()->where('table._id', $table_id);
         if ($query->count() <= 0) {
             $e = new ModelNotFoundException();
             $e->setModel(Decision::class);
             throw $e;
         }
         $query = $query->orderBy(Decision::CREATED_AT, 'DESC');
     } elseif ($variant_id) {
         $query = $this->getModel()->query()->where('table.variant._id', $variant_id);
     } else {
         $query = Decision::orderBy(Decision::CREATED_AT, 'DESC');
     }
     $query = $query->where('applications', ApplicationableHelper::getApplicationId());
     return $this->paginateQuery($query, $size);
 }
 public function __construct($class, $id = null)
 {
     $reflection = new ReflectionClass($class);
     $model = $reflection->getShortName();
     if ($id) {
         $message = sprintf('Model `%s` not found by id `%s`', $model, $id);
     } else {
         $message = sprintf('Model `%s` not found', $model);
     }
     parent::__construct($message);
 }
Example #7
0
 /**
  * Switch the current team of the user
  *
  * @param object|array|integer $team
  * @return $this
  * @throws ModelNotFoundException
  * @throws UserNotInTeamException
  */
 public function switchTeam($team)
 {
     if ($team !== 0 && $team !== null) {
         $team = $this->retrieveTeamId($team);
         $teamModel = Config::get('teamwork.team_model');
         $teamObject = (new $teamModel())->find($team);
         if (!$teamObject) {
             $exception = new ModelNotFoundException();
             $exception->setModel($teamModel);
             throw $exception;
         }
         if (!$teamObject->users->contains($this->getKey())) {
             $exception = new UserNotInTeamException();
             $exception->setTeam($teamObject->name);
             throw $exception;
         }
     }
     $this->current_team_id = $team;
     $this->save();
     return $this;
 }
Example #8
0
 /**
  * @param ModelNotFoundException $e
  *
  * @return Response
  */
 private function sendResponseForModelNotFound(ModelNotFoundException $e)
 {
     $model = $e->getModel();
     if (method_exists($model, 'getNotFoundMessage')) {
         $message = app()->call("{$model}@getNotFoundMessage");
     } else {
         $message = trans('cms::core.messages.model_not_found');
     }
     return back()->withErrors($message, 'model_not_found');
 }
 public function __construct($name, \Exception $previous = null)
 {
     parent::__construct("{$name} not found", ErrorCode::MODEL_NOT_FOUND, $previous);
 }
Example #10
0
 /**
  * Return the relationship configurations.
  *
  * @param string $name of related model
  * @return array
  * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
  */
 public function getRelationship($name)
 {
     // If relationship does not exist throw an exception
     if (!$this->isRelationship($name)) {
         $exception = new ModelNotFoundException();
         $exception->setModel($name);
         throw $exception;
     }
     return $this->relationships[$name];
 }
 /**
  * @param string $model
  *
  * @return $this
  */
 public function setModel($model)
 {
     parent::setModel($model);
     $this->message = "No query results for model [{$model}] when scoped by tenant.";
     return $this;
 }
 /**
  * @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
  */
 public function test_should_return_find_fail()
 {
     $mockRepository = M::mock(AbstractRepository::class);
     $throw = new ModelNotFoundException();
     $throw->setModel(\stdClass::class);
     $mockRepository->shouldReceive('find')->with(0)->andThrow($throw);
     $mockRepository->find(0);
 }
 /**
  * Set the affected Eloquent model.
  *
  * @param  string   $model
  *
  * @return self
  */
 public function setModel($model)
 {
     parent::setModel($model);
     $this->message = 'Unconfirmed user was not found.';
     return $this;
 }
Example #14
0
 /**
  * Switch the current team of the user
  *
  * @param object|array|integer $team
  * @return $this
  * @throws ModelNotFoundException
  * @throws UserNotInTeamException
  */
 public function switchTeam($team)
 {
     if ($team !== 0 && $team !== null) {
         if (is_object($team) && method_exists($team, 'getKey')) {
             $team = $team->getKey();
         }
         if (is_array($team) && isset($team["id"])) {
             $team = $team["id"];
         }
         $teamModel = \Config::get('teamwork.team_model');
         $teamObject = (new $teamModel())->find($team);
         if (!$teamObject) {
             $exception = new ModelNotFoundException();
             $exception->setModel($teamModel);
             throw $exception;
         }
         if (!$teamObject->users->contains($this->getKey())) {
             $exception = new UserNotInTeamException();
             $exception->setTeam($teamObject->name);
             throw $exception;
         }
     }
     $this->current_team_id = $team;
     $this->save();
     return $this;
 }
Example #15
0
 /**
  * Get the model name, without the namespace.
  *
  * @param  \Illuminate\Database\Eloquent\ModelNotFoundException $error
  * @return string
  */
 protected function getModelName(ModelNotFoundException $error)
 {
     return $this->resources->name($error->getModel());
 }
Example #16
0
 /**
  * Get the first record that matches the query
  * or throw an exception otherwise.
  *
  * @param string $objectId
  * @param mixed  $selectKeys
  *
  * @return ObjectModel
  *
  * @throws ModelNotFoundException
  */
 public function firstOrFail($selectKeys = null)
 {
     $first = $this->first($selectKeys);
     if (!$first) {
         $e = new ModelNotFoundException();
         $e->setModel($this->fullClassName);
         throw $e;
     }
     return $first;
 }