diff --git a/.gitignore b/.gitignore index fa449f6..e9b602b 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,4 @@ composer.phar phpunit.xml vendor/ +nbproject/private/ diff --git a/config/autoload/dependencies.global.php b/config/autoload/dependencies.global.php index f6f05f9..c756a36 100644 --- a/config/autoload/dependencies.global.php +++ b/config/autoload/dependencies.global.php @@ -21,6 +21,7 @@ return [ Helper\UrlHelper::class => Helper\UrlHelperFactory::class, 'doctrine.entity_manager.orm_default' => \ContainerInteropDoctrine\EntityManagerFactory::class, 'doctrine.hydrator' => \App\Hydrator\DoctrineObjectFactory::class, + \App\Service\Article\ArticleService::class => \App\Service\Article\ArticleServiceFactory::class, ], ], ]; diff --git a/src/App/Action/AbstractFormAction.php b/src/App/Action/AbstractFormAction.php index 1b1cee5..1d2ad2e 100644 --- a/src/App/Action/AbstractFormAction.php +++ b/src/App/Action/AbstractFormAction.php @@ -8,7 +8,7 @@ abstract class AbstractFormAction { /** - * + * * @param type $request * @return type */ @@ -27,7 +27,7 @@ abstract class AbstractFormAction } /** - * + * * @param type $input * @param type $contentType * @return type @@ -41,7 +41,7 @@ abstract class AbstractFormAction } /** - * + * * @param type $contentType * @return type */ @@ -71,4 +71,4 @@ abstract class AbstractFormAction return $input; }; } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/DeleteAction.php b/src/App/Action/Article/DeleteAction.php index 02064c0..1b67ae8 100644 --- a/src/App/Action/Article/DeleteAction.php +++ b/src/App/Action/Article/DeleteAction.php @@ -2,8 +2,7 @@ namespace App\Action\Article; -use App\Entity\Article; -use Doctrine\ORM\EntityManager; +use App\Service\EntityServiceInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -12,38 +11,27 @@ class DeleteAction { /** - * @var EntityManager + * @var EntityServiceInterface */ - private $em; + private $entityService; - public function __construct(EntityManager $em) + public function __construct(EntityServiceInterface $entityService) { - $this->em = $em; + $this->entityService = $entityService; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $id = $request->getAttribute('id'); - if (null === ($entity = $this->em->find(Article::class, $id))) { - $ret = new JsonResponse([ - 'success' => false - ]); - return $ret->withStatus(404); + $result = $this->entityService->delete($id); + + $return = new JsonResponse($result); + + if (false === $result) { + return $return->withStatus(500, 'Failed to delete record.'); } - try { - $this->em->remove($entity); - $this->em->flush(); - } catch (\Exception $ex) { - $ret = new JsonResponse([ - 'success' => false - ]); - return $ret->withStatus(500); - } - - return new JsonResponse([ - 'success' => true, - ]); + return $return; } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/DeleteFactory.php b/src/App/Action/Article/DeleteFactory.php index ec3e8ff..4f6f1da 100644 --- a/src/App/Action/Article/DeleteFactory.php +++ b/src/App/Action/Article/DeleteFactory.php @@ -2,16 +2,16 @@ namespace App\Action\Article; -use App\Action\AbstractFactory; use App\Action\Article\DeleteAction; +use App\Service\Article\ArticleService; use Interop\Container\ContainerInterface; -class DeleteFactory extends AbstractFactory +class DeleteFactory { public function __invoke(ContainerInterface $container) { - $em = $this->getEntityManager($container); - return new DeleteAction($em); + $entityService = $container->get(ArticleService::class); + return new DeleteAction($entityService); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/GetAction.php b/src/App/Action/Article/GetAction.php index ffd3ead..44d735b 100644 --- a/src/App/Action/Article/GetAction.php +++ b/src/App/Action/Article/GetAction.php @@ -2,9 +2,7 @@ namespace App\Action\Article; -use App\Entity\Article; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Query; +use App\Service\EntityServiceInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -13,39 +11,25 @@ class GetAction { /** - * @var EntityManager + * @var EntityServiceInterface */ - private $em; + private $entityService; - public function __construct(EntityManager $em) + public function __construct(EntityServiceInterface $entityService) { - $this->em = $em; + $this->entityService = $entityService; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { - $id = $request->getAttribute('id'); - $qb = $this->em->createQueryBuilder(); - - $entity = $qb->select('a,u,c') - ->from(Article::class, 'a') - ->leftJoin('a.author', 'u') - ->leftJoin('a.comments', 'c') - ->where('a.id = :aid') - ->setParameter('aid', $id) - ->getQuery() - ->getOneOrNullResult(Query::HYDRATE_ARRAY); + $id = $request->getAttribute('id'); + $entity = $this->entityService->get($id); if (null === $entity) { - $ret = new JsonResponse([ - 'success' => false - ]); + $ret = new JsonResponse(false); return $ret->withStatus(404); } - return new JsonResponse([ - 'success' => true, - 'result' => $entity, - ]); + return new JsonResponse($entity); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/GetFactory.php b/src/App/Action/Article/GetFactory.php index 7829b9b..dafe16b 100644 --- a/src/App/Action/Article/GetFactory.php +++ b/src/App/Action/Article/GetFactory.php @@ -2,16 +2,16 @@ namespace App\Action\Article; -use App\Action\AbstractFactory; use App\Action\Article\GetAction; +use App\Service\Article\ArticleService; use Interop\Container\ContainerInterface; -class GetFactory extends AbstractFactory +class GetFactory { public function __invoke(ContainerInterface $container) { - $em = $this->getEntityManager($container); - return new GetAction($em); + $entityService = $container->get(ArticleService::class); + return new GetAction($entityService); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/ListAction.php b/src/App/Action/Article/ListAction.php index 0f2f503..b9d7ec2 100644 --- a/src/App/Action/Article/ListAction.php +++ b/src/App/Action/Article/ListAction.php @@ -2,8 +2,7 @@ namespace App\Action\Article; -use App\Entity\Article; -use Doctrine\ORM\EntityManager; +use App\Service\EntityServiceInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -12,28 +11,17 @@ class ListAction { /** - * @var EntityManager + * @var EntityServiceInterface */ - private $em; + private $entityService; - public function __construct(EntityManager $em) + public function __construct(EntityServiceInterface $entityService) { - $this->em = $em; + $this->entityService = $entityService; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { - $qb = $this->em->createQueryBuilder(); - $entities = $qb->select('a,u,c') - ->from(Article::class, 'a') - ->leftJoin('a.author', 'u') - ->leftJoin('a.comments', 'c') - ->getQuery() - ->getArrayResult(); - - return new JsonResponse([ - 'success' => true, - 'result' => $entities, - ]); + return new JsonResponse($this->entityService->getList()); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/ListFactory.php b/src/App/Action/Article/ListFactory.php index ac44ab6..2e2549d 100644 --- a/src/App/Action/Article/ListFactory.php +++ b/src/App/Action/Article/ListFactory.php @@ -2,16 +2,16 @@ namespace App\Action\Article; -use App\Action\AbstractFactory; use App\Action\Article\ListAction; +use App\Service\Article\ArticleService; use Interop\Container\ContainerInterface; -class ListFactory extends AbstractFactory +class ListFactory { public function __invoke(ContainerInterface $container) { - $em = $this->getEntityManager($container); - return new ListAction($em); + $entityService = $container->get(ArticleService::class); + return new ListAction($entityService); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/PostAction.php b/src/App/Action/Article/PostAction.php index 26447ee..1609ab8 100644 --- a/src/App/Action/Article/PostAction.php +++ b/src/App/Action/Article/PostAction.php @@ -3,9 +3,7 @@ namespace App\Action\Article; use App\Action\AbstractFormAction; -use App\Entity\Article; -use App\Hydrator\DoctrineObject; -use Doctrine\ORM\EntityManager; +use App\Service\EntityServiceInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -14,32 +12,26 @@ class PostAction extends AbstractFormAction { /** - * @var EntityManager + * @var EntityServiceInterface */ - private $em; + private $entityService; - /** - * @var DoctrineObject - */ - private $hydrator; - - public function __construct(EntityManager $em, DoctrineObject $hydrator) + public function __construct(EntityServiceInterface $entityService) { - $this->em = $em; - $this->hydrator = $hydrator; + $this->entityService = $entityService; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { $data = $this->getRequestData($request); - $entity = $this->hydrator->hydrate($data, new Article()); - $this->em->persist($entity); - $this->em->flush(); + $result = $this->entityService->create($data); - return new JsonResponse([ - 'success' => true, - 'result' => $entity, - ]); + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to insert record.'); + } + + return new JsonResponse($result); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/PostFactory.php b/src/App/Action/Article/PostFactory.php index e28e1e2..d558193 100644 --- a/src/App/Action/Article/PostFactory.php +++ b/src/App/Action/Article/PostFactory.php @@ -2,17 +2,16 @@ namespace App\Action\Article; -use App\Action\AbstractFactory; use App\Action\Article\PostAction; +use App\Service\Article\ArticleService; use Interop\Container\ContainerInterface; -class PostFactory extends AbstractFactory +class PostFactory { public function __invoke(ContainerInterface $container) { - $em = $this->getEntityManager($container); - $hydrator = $this->getDoctrineHydrator($container); - return new PostAction($em, $hydrator); + $entityService = $container->get(ArticleService::class); + return new PostAction($entityService); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/PutAction.php b/src/App/Action/Article/PutAction.php index 3ab0edc..37374f0 100644 --- a/src/App/Action/Article/PutAction.php +++ b/src/App/Action/Article/PutAction.php @@ -3,9 +3,7 @@ namespace App\Action\Article; use App\Action\AbstractFormAction; -use App\Entity\Article; -use App\Hydrator\DoctrineObject; -use Doctrine\ORM\EntityManager; +use App\Service\EntityServiceInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -14,40 +12,27 @@ class PutAction extends AbstractFormAction { /** - * @var EntityManager + * @var EntityServiceInterface */ - private $em; + private $entityService; - /** - * @var DoctrineObject - */ - private $hydrator; - - public function __construct(EntityManager $em, DoctrineObject $hydrator) + public function __construct(EntityServiceInterface $entityService) { - $this->em = $em; - $this->hydrator = $hydrator; + $this->entityService = $entityService; } public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { - $id = $request->getAttribute('id', false); + $id = $request->getAttribute('id', false); + $data = $this->getRequestData($request); - if (null === ($entity = $this->em->find(Article::class, $id))) { - $ret = new JsonResponse([ - 'success' => false - ]); - return $ret->withStatus(404); + $result = $this->entityService->modify($id, $data); + + if (false === $result) { + $result = new JsonResponse(false); + return $result->withStatus(500, 'Failed to update record.'); } - $data = $this->getRequestData($request); - $entity = $this->hydrator->hydrate($data, $entity); - $this->em->persist($entity); - $this->em->flush(); - - return new JsonResponse([ - 'success' => true, - 'result' => $entity, - ]); + return new JsonResponse($result); } -} \ No newline at end of file +} diff --git a/src/App/Action/Article/PutFactory.php b/src/App/Action/Article/PutFactory.php index 77e43eb..6afb465 100644 --- a/src/App/Action/Article/PutFactory.php +++ b/src/App/Action/Article/PutFactory.php @@ -2,17 +2,16 @@ namespace App\Action\Article; -use App\Action\AbstractFactory; use App\Action\Article\PutAction; +use App\Service\Article\ArticleService; use Interop\Container\ContainerInterface; -class PutFactory extends AbstractFactory +class PutFactory { public function __invoke(ContainerInterface $container) { - $em = $this->getEntityManager($container); - $hydrator = $this->getDoctrineHydrator($container); - return new PutAction($em, $hydrator); + $entityService = $container->get(ArticleService::class); + return new PutAction($entityService); } -} \ No newline at end of file +} diff --git a/src/App/Action/HomePageAction.php b/src/App/Action/HomePageAction.php index 617b880..927ae61 100644 --- a/src/App/Action/HomePageAction.php +++ b/src/App/Action/HomePageAction.php @@ -2,9 +2,6 @@ namespace App\Action; -use App\Entity\User; -use Doctrine\ORM\EntityManager; -use Doctrine\ORM\Query; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Zend\Diactoros\Response\JsonResponse; @@ -12,32 +9,10 @@ use Zend\Diactoros\Response\JsonResponse; class HomePageAction { - /** - * @var EntityManager - */ - private $em; - - public function __construct(EntityManager $em) - { - $this->em = $em; - } - public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { - $qb = $this->em->createQueryBuilder(); - $user = $qb->select('u, a, ac, uc') - ->from(User::class, 'u') - ->leftJoin('u.comments', 'uc') - ->leftJoin('u.articles', 'a') - ->leftJoin('a.comments', 'ac') - ->where('u.id = :uid') - ->setParameter('uid', 1) - ->getQuery() - ->getSingleResult(Query::HYDRATE_ARRAY); - return new JsonResponse([ 'welcome' => 'Congratulations! You have reached our API endpoint.', - 'user' => $user, ]); } -} \ No newline at end of file +} diff --git a/src/App/Action/HomePageFactory.php b/src/App/Action/HomePageFactory.php index 5689647..de5e49f 100644 --- a/src/App/Action/HomePageFactory.php +++ b/src/App/Action/HomePageFactory.php @@ -10,7 +10,6 @@ class HomePageFactory public function __invoke(ContainerInterface $container) { - $em = $container->get('doctrine.entity_manager.orm_default'); - return new HomePageAction($em); + return new HomePageAction(); } -} \ No newline at end of file +} diff --git a/src/App/Action/PingAction.php b/src/App/Action/PingAction.php index ea2ae22..6a8cb68 100644 --- a/src/App/Action/PingAction.php +++ b/src/App/Action/PingAction.php @@ -8,6 +8,7 @@ use Psr\Http\Message\ServerRequestInterface; class PingAction { + public function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next = null) { return new JsonResponse(['ack' => time()]); diff --git a/src/App/Entity/Article.php b/src/App/Entity/Article.php index 105afa3..2a6096e 100644 --- a/src/App/Entity/Article.php +++ b/src/App/Entity/Article.php @@ -12,7 +12,9 @@ use JsonSerializable; */ class Article implements JsonSerializable { + use Traits\GetterSetter; + /** * @ORM\Id * @ORM\Column(name="id", type="integer") @@ -177,4 +179,4 @@ class Article implements JsonSerializable 'visible' => $this->visible, ]; } -} \ No newline at end of file +} diff --git a/src/App/Entity/Comment.php b/src/App/Entity/Comment.php index a7f2560..190ddfb 100644 --- a/src/App/Entity/Comment.php +++ b/src/App/Entity/Comment.php @@ -12,7 +12,9 @@ use JsonSerializable; */ class Comment implements JsonSerializable { + use Traits\GetterSetter; + /** * @ORM\Id * @ORM\Column(name="id", type="integer") @@ -175,4 +177,4 @@ class Comment implements JsonSerializable 'visible' => $this->visible, ]; } -} \ No newline at end of file +} diff --git a/src/App/Entity/Traits/GetterSetter.php b/src/App/Entity/Traits/GetterSetter.php index b5fceff..9342127 100644 --- a/src/App/Entity/Traits/GetterSetter.php +++ b/src/App/Entity/Traits/GetterSetter.php @@ -2,7 +2,7 @@ namespace App\Entity\Traits; -Trait GetterSetter +trait GetterSetter { /** @@ -14,7 +14,7 @@ Trait GetterSetter protected function getterName($field) { return sprintf('get%s', ucfirst( - str_replace(' ', '', ucwords(str_replace('_', ' ', $field))) + str_replace(' ', '', ucwords(str_replace('_', ' ', $field))) )); } @@ -27,7 +27,7 @@ Trait GetterSetter protected function setterName($field) { return sprintf('set%s', ucfirst( - str_replace(' ', '', ucwords(str_replace('_', ' ', $field))) + str_replace(' ', '', ucwords(str_replace('_', ' ', $field))) )); } @@ -49,4 +49,4 @@ Trait GetterSetter return true; } -} \ No newline at end of file +} diff --git a/src/App/Entity/User.php b/src/App/Entity/User.php index 02c5d92..1403aaf 100644 --- a/src/App/Entity/User.php +++ b/src/App/Entity/User.php @@ -12,7 +12,9 @@ use JsonSerializable; */ class User implements JsonSerializable { + use Traits\GetterSetter; + /** * @ORM\Id * @ORM\Column(name="id", type="integer") @@ -181,4 +183,4 @@ class User implements JsonSerializable 'active' => $this->active, ]; } -} \ No newline at end of file +} diff --git a/src/App/Hydrator/DoctrineObject.php b/src/App/Hydrator/DoctrineObject.php index 3597166..912e5c2 100644 --- a/src/App/Hydrator/DoctrineObject.php +++ b/src/App/Hydrator/DoctrineObject.php @@ -175,7 +175,7 @@ class DoctrineObject extends AbstractHydrator ? $object->getFilter() : $this->filterComposite; - $data = array(); + $data = []; foreach ($fieldNames as $fieldName) { if ($filter && !$filter->filter($fieldName)) { continue; @@ -216,7 +216,7 @@ class DoctrineObject extends AbstractHydrator ? $object->getFilter() : $this->filterComposite; - $data = array(); + $data = []; foreach ($fieldNames as $fieldName) { if ($filter && !$filter->filter($fieldName)) { continue; @@ -347,7 +347,7 @@ class DoctrineObject extends AbstractHydrator { $metadata = $this->metadata; $identifierNames = $metadata->getIdentifierFieldNames($object); - $identifierValues = array(); + $identifierValues = []; if (empty($identifierNames)) { return $object; @@ -417,11 +417,10 @@ class DoctrineObject extends AbstractHydrator $values = (array)$values; } - $collection = array(); + $collection = []; // If the collection contains identifiers, fetch the objects from database foreach ($values as $value) { - if ($value instanceof $target) { // assumes modifications have already taken place in object $collection[] = $value; @@ -432,7 +431,7 @@ class DoctrineObject extends AbstractHydrator continue; } - $find = array(); + $find = []; if (is_array($identifier)) { foreach ($identifier as $field) { switch (gettype($value)) { diff --git a/src/App/Hydrator/DoctrineObjectFactory.php b/src/App/Hydrator/DoctrineObjectFactory.php index 08c4ee5..d7d10ea 100644 --- a/src/App/Hydrator/DoctrineObjectFactory.php +++ b/src/App/Hydrator/DoctrineObjectFactory.php @@ -12,4 +12,4 @@ class DoctrineObjectFactory $em = $container->get('doctrine.entity_manager.orm_default'); return new DoctrineObject($em); } -} \ No newline at end of file +} diff --git a/src/App/Hydrator/Filter/PropertyName.php b/src/App/Hydrator/Filter/PropertyName.php index 628bd85..daafc86 100644 --- a/src/App/Hydrator/Filter/PropertyName.php +++ b/src/App/Hydrator/Filter/PropertyName.php @@ -36,7 +36,7 @@ class PropertyName implements FilterInterface * * @var array */ - protected $properties = array(); + protected $properties = []; /** * Either an exclude or an include. @@ -54,7 +54,7 @@ class PropertyName implements FilterInterface $this->exclude = $exclude; $this->properties = is_array($properties) ? $properties - : array($properties); + : [$properties]; } public function filter($property) diff --git a/src/App/Hydrator/Strategy/AllowRemoveByReference.php b/src/App/Hydrator/Strategy/AllowRemoveByReference.php index 3e483c2..57e4222 100644 --- a/src/App/Hydrator/Strategy/AllowRemoveByReference.php +++ b/src/App/Hydrator/Strategy/AllowRemoveByReference.php @@ -42,8 +42,8 @@ class AllowRemoveByReference extends AbstractCollectionStrategy $collection = $this->getCollectionFromObjectByReference(); $collectionArray = $collection->toArray(); - $toAdd = array_udiff($value, $collectionArray, array($this, 'compareObjects')); - $toRemove = array_udiff($collectionArray, $value, array($this, 'compareObjects')); + $toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']); + $toRemove = array_udiff($collectionArray, $value, [$this, 'compareObjects']); foreach ($toAdd as $element) { $collection->add($element); diff --git a/src/App/Hydrator/Strategy/AllowRemoveByValue.php b/src/App/Hydrator/Strategy/AllowRemoveByValue.php index 6ebf9f7..2ac85ae 100644 --- a/src/App/Hydrator/Strategy/AllowRemoveByValue.php +++ b/src/App/Hydrator/Strategy/AllowRemoveByValue.php @@ -65,8 +65,8 @@ class AllowRemoveByValue extends AbstractCollectionStrategy $collection = $collection->toArray(); } - $toAdd = new ArrayCollection(array_udiff($value, $collection, array($this, 'compareObjects'))); - $toRemove = new ArrayCollection(array_udiff($collection, $value, array($this, 'compareObjects'))); + $toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects'])); + $toRemove = new ArrayCollection(array_udiff($collection, $value, [$this, 'compareObjects'])); $this->object->$adder($toAdd); $this->object->$remover($toRemove); diff --git a/src/App/Hydrator/Strategy/DisallowRemoveByReference.php b/src/App/Hydrator/Strategy/DisallowRemoveByReference.php index 4222f90..e1cbb41 100644 --- a/src/App/Hydrator/Strategy/DisallowRemoveByReference.php +++ b/src/App/Hydrator/Strategy/DisallowRemoveByReference.php @@ -42,7 +42,7 @@ class DisallowRemoveByReference extends AbstractCollectionStrategy $collection = $this->getCollectionFromObjectByReference(); $collectionArray = $collection->toArray(); - $toAdd = array_udiff($value, $collectionArray, array($this, 'compareObjects')); + $toAdd = array_udiff($value, $collectionArray, [$this, 'compareObjects']); foreach ($toAdd as $element) { $collection->add($element); diff --git a/src/App/Hydrator/Strategy/DisallowRemoveByValue.php b/src/App/Hydrator/Strategy/DisallowRemoveByValue.php index 2e6019b..6f2bbcc 100644 --- a/src/App/Hydrator/Strategy/DisallowRemoveByValue.php +++ b/src/App/Hydrator/Strategy/DisallowRemoveByValue.php @@ -63,7 +63,7 @@ class DisallowRemoveByValue extends AbstractCollectionStrategy $collection = $collection->toArray(); } - $toAdd = new ArrayCollection(array_udiff($value, $collection, array($this, 'compareObjects'))); + $toAdd = new ArrayCollection(array_udiff($value, $collection, [$this, 'compareObjects'])); $this->object->$adder($toAdd); diff --git a/src/App/Action/AbstractFactory.php b/src/App/Service/AbstractEntityServiceFactory.php similarity index 70% rename from src/App/Action/AbstractFactory.php rename to src/App/Service/AbstractEntityServiceFactory.php index 3593493..f5568b6 100644 --- a/src/App/Action/AbstractFactory.php +++ b/src/App/Service/AbstractEntityServiceFactory.php @@ -1,15 +1,17 @@ get('doctrine.hydrator'); } -} \ No newline at end of file +} diff --git a/src/App/Service/Article/ArticleService.php b/src/App/Service/Article/ArticleService.php new file mode 100644 index 0000000..5b80c89 --- /dev/null +++ b/src/App/Service/Article/ArticleService.php @@ -0,0 +1,99 @@ +em = $em; + $this->hydrator = $hydrator; + } + + public function getList() + { + $qb = $this->em->createQueryBuilder(); + $entities = $qb->select('a,u,c') + ->from(Article::class, 'a') + ->leftJoin('a.author', 'u') + ->leftJoin('a.comments', 'c') + ->getQuery() + ->getArrayResult(); + return $entities; + } + + public function get($id) + { + $qb = $this->em->createQueryBuilder(); + + $entity = $qb->select('a,u,c') + ->from(Article::class, 'a') + ->leftJoin('a.author', 'u') + ->leftJoin('a.comments', 'c') + ->where('a.id = :aid') + ->setParameter('aid', $id) + ->getQuery() + ->getOneOrNullResult(Query::HYDRATE_ARRAY); + + return $entity; + } + + public function create($data) + { + $entity = $this->hydrator->hydrate($data, new Article()); + try { + $this->em->persist($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + return $entity; + } + + public function modify($id, $data) + { + if (null === ($entity = $this->em->find(Article::class, $id))) { + return false; + } + + $entity = $this->hydrator->hydrate($data, $entity); + $this->em->persist($entity); + $this->em->flush(); + + return $entity; + } + + public function delete($id) + { + if (null === ($entity = $this->em->find(Article::class, $id))) { + return false; + } + + try { + $this->em->remove($entity); + $this->em->flush(); + } catch (Exception $ex) { + return false; + } + + return true; + } +} diff --git a/src/App/Service/Article/ArticleServiceFactory.php b/src/App/Service/Article/ArticleServiceFactory.php new file mode 100644 index 0000000..89e3fd9 --- /dev/null +++ b/src/App/Service/Article/ArticleServiceFactory.php @@ -0,0 +1,16 @@ +getEntityManager($container); + $hydrator = $this->getDoctrineHydrator($container); + return new ArticleService($em, $hydrator); + } +} diff --git a/src/App/Service/EntityServiceInterface.php b/src/App/Service/EntityServiceInterface.php new file mode 100644 index 0000000..7f80778 --- /dev/null +++ b/src/App/Service/EntityServiceInterface.php @@ -0,0 +1,22 @@ +prophesize(ArticleService::class); + $entityService->willImplement(EntityServiceInterface::class); + $entityService->getList()->willReturn([]); + + $this->entityService = $entityService; + } + + public function testResponse() + { + $page = new ListAction($this->entityService->reveal()); + $response = $page(new ServerRequest(['/api/article']), new Response(), function () { + + }); + + $this->assertTrue($response instanceof Response); + } +} diff --git a/test/AppTest/Action/Article/ListFactoryTest.php b/test/AppTest/Action/Article/ListFactoryTest.php new file mode 100644 index 0000000..a3d3456 --- /dev/null +++ b/test/AppTest/Action/Article/ListFactoryTest.php @@ -0,0 +1,51 @@ +container = $this->prophesize(ContainerInterface::class); + $articleService = $this->prophesize(ArticleService::class); + + $this->container->get(ArticleService::class)->willReturn($articleService); + } + + public function testFactoryWithoutTemplate() + { + $factory = new ListFactory(); + $this->container->has(TemplateRendererInterface::class)->willReturn(false); + + $this->assertTrue($factory instanceof ListFactory); + + $page = $factory($this->container->reveal()); + + $this->assertTrue($page instanceof ListAction); + } + + public function testFactoryWithTemplate() + { + $factory = new ListFactory(); + $this->container->has(TemplateRendererInterface::class)->willReturn(true); + $this->container + ->get(TemplateRendererInterface::class) + ->willReturn($this->prophesize(TemplateRendererInterface::class)); + + $this->assertTrue($factory instanceof ListFactory); + + $homePage = $factory($this->container->reveal()); + + $this->assertTrue($homePage instanceof ListAction); + } +} diff --git a/test/AppTest/Action/HomePageActionTest.php b/test/AppTest/Action/HomePageActionTest.php index 3a083c7..4088c34 100644 --- a/test/AppTest/Action/HomePageActionTest.php +++ b/test/AppTest/Action/HomePageActionTest.php @@ -5,22 +5,21 @@ namespace AppTest\Action; use App\Action\HomePageAction; use Zend\Diactoros\Response; use Zend\Diactoros\ServerRequest; -use Zend\Expressive\Router\RouterInterface; +use Doctrine\ORM\EntityManager; class HomePageActionTest extends \PHPUnit_Framework_TestCase { - /** @var RouterInterface */ - protected $router; protected function setUp() { - $this->router = $this->prophesize(RouterInterface::class); + } public function testResponse() { - $homePage = new HomePageAction($this->router->reveal(), null); + $homePage = new HomePageAction(); $response = $homePage(new ServerRequest(['/']), new Response(), function () { + }); $this->assertTrue($response instanceof Response); diff --git a/test/AppTest/Action/HomePageFactoryTest.php b/test/AppTest/Action/HomePageFactoryTest.php index 3a98fcd..9aea235 100644 --- a/test/AppTest/Action/HomePageFactoryTest.php +++ b/test/AppTest/Action/HomePageFactoryTest.php @@ -5,7 +5,7 @@ namespace AppTest\Action; use App\Action\HomePageAction; use App\Action\HomePageFactory; use Interop\Container\ContainerInterface; -use Zend\Expressive\Router\RouterInterface; +use Doctrine\ORM\EntityManager; use Zend\Expressive\Template\TemplateRendererInterface; class HomePageFactoryTest extends \PHPUnit_Framework_TestCase @@ -16,9 +16,9 @@ class HomePageFactoryTest extends \PHPUnit_Framework_TestCase protected function setUp() { $this->container = $this->prophesize(ContainerInterface::class); - $router = $this->prophesize(RouterInterface::class); + $em = $this->prophesize(EntityManager::class); - $this->container->get(RouterInterface::class)->willReturn($router); + $this->container->get('doctrine.entity_manager.orm_default')->willReturn($em); } public function testFactoryWithoutTemplate()