Skip to content

Commit

Permalink
Refactored notFound method to return a throwable exception, and added…
Browse files Browse the repository at this point in the history
… check to ensure the validator fails (ignored in code coverage as this should never happen)
  • Loading branch information
zakhenry committed Aug 3, 2015
1 parent 0032c19 commit 8696741
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions api/app/Http/Controllers/ApiController.php
Expand Up @@ -20,6 +20,7 @@
use Spira\Responder\Paginator\PaginatedRequestDecoratorInterface;
use Spira\Responder\Response\ApiResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Yaml\Exception\RuntimeException;

abstract class ApiController extends Controller
{
Expand Down Expand Up @@ -76,7 +77,7 @@ public function getOne($id)
try {
$model = $this->getRepository()->find($id);
} catch (ModelNotFoundException $e) {
$this->notFound();
throw $this->notFoundException();
}

return $this->getResponse()
Expand Down Expand Up @@ -176,7 +177,7 @@ public function patchOne($id, Request $request)
$model->fill($request->all());
$this->getRepository()->save($model);
} catch (ModelNotFoundException $e) {
$this->notFound($this->getKeyName());
throw $this->notFoundException($this->getKeyName());
}

return $this->getResponse()->noContent();
Expand All @@ -194,7 +195,7 @@ public function patchMany(Request $request)
$ids = $this->getIds($requestCollection);
$models = $this->getRepository()->findMany($ids);
if ($models->count() !== count($ids)) {
$this->notFoundMany($ids, $models);
throw $this->notFoundManyException($ids, $models);
}

foreach ($requestCollection as $requestEntity) {
Expand Down Expand Up @@ -223,7 +224,7 @@ public function deleteOne($id)
$model = $this->getRepository()->find($id);
$this->getRepository()->delete($model);
} catch (ModelNotFoundException $e) {
$this->notFound($this->getKeyName());
throw $this->notFoundException();
}

return $this->getResponse()->noContent();
Expand All @@ -242,7 +243,7 @@ public function deleteMany(Request $request)
$models = $this->getRepository()->findMany($ids);

if (count($ids) !== $models->count()) {
$this->notFoundMany($ids, $models);
throw $this->notFoundManyException($ids, $models);
}

$this->getRepository()->deleteMany($models);
Expand Down Expand Up @@ -309,34 +310,44 @@ protected function getIds($entityCollection, $validate = true)
}


protected function notFound()
/**
* Build notFoundException
* @return ValidationException
*/
protected function notFoundException()
{
$validation = $this->getValidationFactory()->make([$this->getKeyName()=>$this->getKeyName()], [$this->getKeyName()=>'notFound']);
if ($validation->fails()) {
throw new ValidationException($validation->getMessageBag());
if (!$validation->fails()) {
// @codeCoverageIgnoreStart
throw new \LogicException("Validator should have failed");
// @codeCoverageIgnoreEnd
}

return new ValidationException($validation->getMessageBag());
}

/**
* Get notFoundManyException
* @param $ids
* @param Collection $models
* @return ValidationExceptionCollection
*/
protected function notFoundMany($ids, $models)
protected function notFoundManyException($ids, $models)
{
$errors = [];
foreach ($ids as $id) {
if ($models->get($id)) {
$errors[] = null;
} else {
try {
$this->notFound();
throw $this->notFoundException();
} catch (ValidationException $e) {
$errors[] = $e;
}
}
}

throw new ValidationExceptionCollection($errors);
return new ValidationExceptionCollection($errors);
}

/**
Expand Down

0 comments on commit 8696741

Please sign in to comment.