diff --git a/src/App/Action/Person/DeleteAction.php b/src/App/Action/Person/DeleteAction.php index 8ae3a48..42cc1de 100644 --- a/src/App/Action/Person/DeleteAction.php +++ b/src/App/Action/Person/DeleteAction.php @@ -3,6 +3,8 @@ namespace App\Action\Person; use App\Service\EntityServiceInterface; +use Doctrine\ORM\EntityNotFoundException; +use Doctrine\ORM\ORMInvalidArgumentException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -24,14 +26,22 @@ class DeleteAction { $id = $request->getAttribute('id'); - $result = $this->entityService->delete($id); + try { + $this->entityService->delete($id); + } catch (EntityNotFoundException $exception) { + $ret = new JsonResponse([ + 'message' => $exception->getMessage(), + ]); - $return = new JsonResponse($result); + return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id)); + } catch (ORMInvalidArgumentException $exception) { + $res = new JsonResponse([ + 'message' => $exception->getMessage(), + ]); - if (false === $result) { - return $return->withStatus(500, 'Failed to delete record.'); + return $res->withStatus(500, 'Failed to delete record.'); } - return $return; + return new JsonResponse(true); } } diff --git a/src/App/Action/Person/DeleteFactory.php b/src/App/Action/Person/DeleteFactory.php index f1fe05a..ecaf2ed 100644 --- a/src/App/Action/Person/DeleteFactory.php +++ b/src/App/Action/Person/DeleteFactory.php @@ -2,8 +2,8 @@ namespace App\Action\Person; -use App\Action\Article\DeleteAction; -use App\Service\Article\PersonService; +use App\Action\Person\DeleteAction; +use App\Service\Person\PersonService; use Interop\Container\ContainerInterface; class DeleteFactory diff --git a/src/App/Action/Person/GetAction.php b/src/App/Action/Person/GetAction.php index 8443529..8ddc314 100644 --- a/src/App/Action/Person/GetAction.php +++ b/src/App/Action/Person/GetAction.php @@ -3,6 +3,8 @@ namespace App\Action\Person; use App\Service\EntityServiceInterface; +use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -22,12 +24,21 @@ class GetAction public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { - $id = $request->getAttribute('id'); - $entity = $this->entityService->get($id); + $id = $request->getAttribute('id'); + try { + $entity = $this->entityService->get($id); + } catch (NoResultException $ex) { + $ret = new JsonResponse([ + 'message' => $ex->getMessage(), + ]); - if (null === $entity) { - $ret = new JsonResponse(false); - return $ret->withStatus(404); + return $ret->withStatus(404, sprintf('Entity with id `%s` not found.', $id)); + } catch (NonUniqueResultException $ex) { + $ret = new JsonResponse([ + 'message' => $ex->getMessage(), + ]); + + return $ret->withStatus(500, 'Duplicate key in database.'); } return new JsonResponse($entity); diff --git a/src/App/Action/Person/PostAction.php b/src/App/Action/Person/PostAction.php index 7a91f3d..7231940 100644 --- a/src/App/Action/Person/PostAction.php +++ b/src/App/Action/Person/PostAction.php @@ -4,6 +4,7 @@ namespace App\Action\Person; use App\Action\AbstractFormAction; use App\Service\EntityServiceInterface; +use Doctrine\DBAL\Exception\NotNullConstraintViolationException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -25,11 +26,13 @@ class PostAction extends AbstractFormAction { $data = $this->getRequestData($request); - $result = $this->entityService->create($data); - - if (false === $result) { - $result = new JsonResponse(false); - return $result->withStatus(500, 'Failed to insert record.'); + try { + $result = $this->entityService->create($data); + } catch (NotNullConstraintViolationException $exception) { + $res = new JsonResponse([ + 'message' => $exception->getMessage(), + ]); + return $res->withStatus(500, 'Failed to insert record. A required field was not set.'); } return new JsonResponse($result); diff --git a/src/App/Action/Person/PutAction.php b/src/App/Action/Person/PutAction.php index de6873c..423d63e 100644 --- a/src/App/Action/Person/PutAction.php +++ b/src/App/Action/Person/PutAction.php @@ -4,6 +4,7 @@ namespace App\Action\Person; use App\Action\AbstractFormAction; use App\Service\EntityServiceInterface; +use Doctrine\ORM\EntityNotFoundException; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -26,11 +27,13 @@ class PutAction extends AbstractFormAction $id = $request->getAttribute('id', false); $data = $this->getRequestData($request); - $result = $this->entityService->modify($id, $data); - - if (false === $result) { - $result = new JsonResponse(false); - return $result->withStatus(500, 'Failed to update record.'); + try { + $result = $this->entityService->modify($id, $data); + } catch (EntityNotFoundException $exception) { + $res = new JsonResponse([ + 'message' => $exception->getMessage(), + ]); + return $res->withStatus(404, sprintf('Failed to update record with id `%s`', $id)); } return new JsonResponse($result); diff --git a/src/App/Entity/Person.php b/src/App/Entity/Person.php index 576c77d..fa04d4b 100644 --- a/src/App/Entity/Person.php +++ b/src/App/Entity/Person.php @@ -2,7 +2,7 @@ namespace App\Entity; -use Doctrine\Common\Collections\ArrayCollection; +use DateTime; use Doctrine\ORM\Mapping as ORM; use JsonSerializable; @@ -36,7 +36,7 @@ class Person implements JsonSerializable private $firstName; /** - * @ORM\Column(name="middle_name", type="string", length=150) + * @ORM\Column(name="middle_name", type="string", length=150, nullable=true) * @var string */ private $middleName; @@ -54,43 +54,43 @@ class Person implements JsonSerializable private $gender; /** - * @ORM\Column(name="date_of_birth", type="date", nullable=false) + * @ORM\Column(name="date_of_birth", type="date", nullable=true) * @var string */ private $dateOfBirth; /** - * @ORM\Column(name="place_of_birth", type="string", length=150, nullable=false) + * @ORM\Column(name="place_of_birth", type="string", length=150, nullable=true) * @var string */ private $placeOfBirth; /** - * @ORM\Column(name="primary_language", type="string", length=150) + * @ORM\Column(name="primary_language", type="string", length=150, nullable=true) * @var string */ private $primaryLanguage; /** - * @ORM\Column(name="address", type="string", length=255) + * @ORM\Column(name="address", type="string", length=255, nullable=true) * @var string */ private $address; /** * @ORM\ManyToOne(targetEntity="Person") - * @ORM\JoinColumn(name="mother_id", referencedColumnName="id") + * @ORM\JoinColumn(name="mother_id", referencedColumnName="id", nullable=true) */ private $mother; /** * @ORM\ManyToOne(targetEntity="Person") - * @ORM\JoinColumn(name="father_id", referencedColumnName="id") + * @ORM\JoinColumn(name="father_id", referencedColumnName="id", nullable=true) */ private $father; /** - * @ORM\Column(name="email", type="string", length=150) + * @ORM\Column(name="email", type="string", length=150, nullable=true) * @var string */ private $email; @@ -159,7 +159,7 @@ class Person implements JsonSerializable } /** - * @return datetime + * @return DateTime */ public function getDateOfBirth() { @@ -224,7 +224,7 @@ class Person implements JsonSerializable /** * @param int $id - * @return User + * @return Person */ public function setId(int $id) { @@ -234,7 +234,7 @@ class Person implements JsonSerializable /** * @param string $personId - * @return User + * @return Person */ public function setPersonId(string $personId) { @@ -244,7 +244,7 @@ class Person implements JsonSerializable /** * @param string $firstName - * @return User + * @return Person */ public function setFirstName(string $firstName) { @@ -254,7 +254,7 @@ class Person implements JsonSerializable /** * @param string $middleName - * @return User + * @return Person */ public function setMiddleName(string $middleName) { @@ -264,7 +264,7 @@ class Person implements JsonSerializable /** * @param string $lastName - * @return User + * @return Person */ public function setLastName(string $lastName) { @@ -274,7 +274,7 @@ class Person implements JsonSerializable /** * @param int $gender - * @return User + * @return Person */ public function setGender(int $gender) { @@ -284,9 +284,9 @@ class Person implements JsonSerializable /** * @param datetime $dateOfBirth - * @return User + * @return Person */ - public function setDateOfBirth(datetime $dateOfBirth) + public function setDateOfBirth(DateTime $dateOfBirth) { $this->dateOfBirth = $dateOfBirth; return $this; @@ -294,7 +294,7 @@ class Person implements JsonSerializable /** * @param string $placeOfBirth - * @return User + * @return Person */ public function setPlaceOfBirth(string $placeOfBirth) { @@ -304,7 +304,7 @@ class Person implements JsonSerializable /** * @param string $primaryLanguage - * @return User + * @return Person */ public function setPrimaryLanguage(string $primaryLanguage) { @@ -314,7 +314,7 @@ class Person implements JsonSerializable /** * @param string $address - * @return User + * @return Person */ public function setAddress(string $address) { @@ -324,7 +324,7 @@ class Person implements JsonSerializable /** * @param Person $mother - * @return User + * @return Person */ public function setMother(Person $mother) { @@ -334,7 +334,7 @@ class Person implements JsonSerializable /** * @param Person $father - * @return User + * @return Person */ public function setFather(Person $father) { @@ -344,7 +344,7 @@ class Person implements JsonSerializable /** * @param string $email - * @return User + * @return Person */ public function setEmail(string $email) { @@ -354,7 +354,7 @@ class Person implements JsonSerializable /** * @param bool $active - * @return User + * @return Person */ public function setActive(bool $active) { diff --git a/src/App/Service/Person/PersonService.php b/src/App/Service/Person/PersonService.php index f47dadd..0780f9a 100644 --- a/src/App/Service/Person/PersonService.php +++ b/src/App/Service/Person/PersonService.php @@ -5,9 +5,13 @@ namespace App\Service\Person; use App\Entity\Person; use App\Hydrator\DoctrineObject; use App\Service\EntityServiceInterface; +use Doctrine\DBAL\Exception\NotNullConstraintViolationException; use Doctrine\ORM\EntityManager; +use Doctrine\ORM\EntityNotFoundException; +use Doctrine\ORM\NonUniqueResultException; +use Doctrine\ORM\NoResultException; +use Doctrine\ORM\ORMInvalidArgumentException; use Doctrine\ORM\Query; -use Exception; class PersonService implements EntityServiceInterface { @@ -22,12 +26,20 @@ class PersonService implements EntityServiceInterface */ private $hydrator; + /** + * @param EntityManager $em + * @param DoctrineObject $hydrator + */ public function __construct(EntityManager $em, DoctrineObject $hydrator) { $this->em = $em; $this->hydrator = $hydrator; } + /** + * + * @return mixed[] + */ public function getList() { $qb = $this->em->createQueryBuilder(); @@ -38,6 +50,13 @@ class PersonService implements EntityServiceInterface return $entities; } + /** + * + * @param int $id + * @return mixed + * @throws NonUniqueResultException + * @throws NoResultException + */ public function get($id) { $qb = $this->em->createQueryBuilder(); @@ -47,27 +66,36 @@ class PersonService implements EntityServiceInterface ->where('p.id = :id') ->setParameter('id', $id) ->getQuery() - ->getOneOrNullResult(Query::HYDRATE_ARRAY); + ->getSingleResult(Query::HYDRATE_ARRAY); return $entity; } + /** + * + * @param type $data + * @return type + * @throws NotNullConstraintViolationException + */ public function create($data) { $entity = $this->hydrator->hydrate($data, new Person()); - try { - $this->em->persist($entity); - $this->em->flush(); - } catch (Exception $ex) { - return false; - } + $this->em->persist($entity); + $this->em->flush(); return $entity; } + /** + * + * @param int $id + * @param mixed $data + * @return Person + * @throws EntityNotFoundException + */ public function modify($id, $data) { if (null === ($entity = $this->em->find(Person::class, $id))) { - return false; + throw new EntityNotFoundException('No result was found for query although at least one row was expected.'); } $entity = $this->hydrator->hydrate($data, $entity); @@ -77,18 +105,21 @@ class PersonService implements EntityServiceInterface return $entity; } + /** + * + * @param int $id + * @return boolean + * @throws EntityNotFoundException + * @throws ORMInvalidArgumentException + */ public function delete($id) { if (null === ($entity = $this->em->find(Person::class, $id))) { - return false; + throw new EntityNotFoundException('No result was found for query although at least one row was expected.'); } - try { - $this->em->remove($entity); - $this->em->flush(); - } catch (Exception $ex) { - return false; - } + $this->em->remove($entity); + $this->em->flush(); return true; }