Skip to content
Permalink
Browse files

refactor: Extracted common duplicate code to own method.

  • Loading branch information...
tarlepp committed Jun 2, 2019
1 parent 2ff3a99 commit 0eb381d155dd6cdf13979eb42355a5705133b576
Showing with 19 additions and 12 deletions.
  1. +19 −12 src/Rest/Traits/RestResourceBaseMethods.php
@@ -121,10 +121,7 @@ public function findOne(string $id, ?bool $throwExceptionIfNotFound = null): ?En
/** @var EntityInterface|null $entity */
$entity = $this->getRepository()->findAdvanced($id);
// Entity not found
if ($throwExceptionIfNotFound && $entity === null) {
throw new NotFoundHttpException('Not found');
}
$this->checkThatEntityExists($throwExceptionIfNotFound, $entity);
// After callback method call
$this->afterFindOne($id, $entity);
@@ -158,10 +155,7 @@ public function findOneBy(
/** @var EntityInterface|null $entity */
$entity = $this->getRepository()->findOneBy($criteria, $orderBy);
// Entity not found
if ($throwExceptionIfNotFound && $entity === null) {
throw new NotFoundHttpException('Not found');
}
$this->checkThatEntityExists($throwExceptionIfNotFound, $entity);
// After callback method call
$this->afterFindOneBy($criteria, $orderBy, $entity);
@@ -387,6 +381,8 @@ protected function persistEntity(
}
/**
* @psalm-suppress InvalidNullableReturnType
*
* @param string $id
*
* @return EntityInterface
@@ -398,10 +394,7 @@ protected function getEntity(string $id): EntityInterface
/** @var EntityInterface|null $entity */
$entity = $this->getRepository()->find($id);
// Entity not found
if ($entity === null) {
throw new NotFoundHttpException('Not found');
}
$this->checkThatEntityExists(true, $entity);
return $entity;
}
@@ -476,4 +469,18 @@ private function createValidatorException(ConstraintViolationListInterface $erro
throw new ValidatorException(JSON::encode($output));
}
/**
* @param bool $throwExceptionIfNotFound
* @param EntityInterface|null $entity
*
* @throws NotFoundHttpException
*/
private function checkThatEntityExists(bool $throwExceptionIfNotFound, ?EntityInterface $entity): void
{
// Entity not found
if ($throwExceptionIfNotFound && $entity === null) {
throw new NotFoundHttpException('Not found');
}
}
}

0 comments on commit 0eb381d

Please sign in to comment.
You can’t perform that action at this time.