* refactored with entity service, so the code can be tested
This commit is contained in:
parent
f3939bbd13
commit
2de0bf8add
1
.gitignore
vendored
1
.gitignore
vendored
@ -2,3 +2,4 @@
|
||||
composer.phar
|
||||
phpunit.xml
|
||||
vendor/
|
||||
nbproject/private/
|
||||
|
||||
@ -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,
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
@ -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;
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
if (null === ($entity = $this->em->find(Article::class, $id))) {
|
||||
$ret = new JsonResponse([
|
||||
'success' => false
|
||||
]);
|
||||
return $ret->withStatus(404);
|
||||
}
|
||||
|
||||
$data = $this->getRequestData($request);
|
||||
$entity = $this->hydrator->hydrate($data, $entity);
|
||||
$this->em->persist($entity);
|
||||
$this->em->flush();
|
||||
|
||||
return new JsonResponse([
|
||||
'success' => true,
|
||||
'result' => $entity,
|
||||
]);
|
||||
$result = $this->entityService->modify($id, $data);
|
||||
|
||||
if (false === $result) {
|
||||
$result = new JsonResponse(false);
|
||||
return $result->withStatus(500, 'Failed to update record.');
|
||||
}
|
||||
|
||||
return new JsonResponse($result);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
}
|
||||
}
|
||||
@ -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,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@ -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();
|
||||
}
|
||||
}
|
||||
@ -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()]);
|
||||
|
||||
@ -12,7 +12,9 @@ use JsonSerializable;
|
||||
*/
|
||||
class Article implements JsonSerializable
|
||||
{
|
||||
|
||||
use Traits\GetterSetter;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
|
||||
@ -12,7 +12,9 @@ use JsonSerializable;
|
||||
*/
|
||||
class Comment implements JsonSerializable
|
||||
{
|
||||
|
||||
use Traits\GetterSetter;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Entity\Traits;
|
||||
|
||||
Trait GetterSetter
|
||||
trait GetterSetter
|
||||
{
|
||||
|
||||
/**
|
||||
|
||||
@ -12,7 +12,9 @@ use JsonSerializable;
|
||||
*/
|
||||
class User implements JsonSerializable
|
||||
{
|
||||
|
||||
use Traits\GetterSetter;
|
||||
|
||||
/**
|
||||
* @ORM\Id
|
||||
* @ORM\Column(name="id", type="integer")
|
||||
|
||||
@ -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)) {
|
||||
|
||||
@ -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)
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
<?php
|
||||
|
||||
namespace App\Action;
|
||||
namespace App\Service;
|
||||
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
abstract class AbstractFactory
|
||||
abstract class AbstractEntityServiceFactory
|
||||
{
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return \Doctrine\ORM\EntityManager
|
||||
* @return EntityManager
|
||||
*/
|
||||
protected function getEntityManager(ContainerInterface $container)
|
||||
{
|
||||
@ -18,7 +20,7 @@ abstract class AbstractFactory
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
* @return \App\Hydrator\DoctrineObject
|
||||
* @return DoctrineObject
|
||||
*/
|
||||
protected function getDoctrineHydrator(ContainerInterface $container)
|
||||
{
|
||||
99
src/App/Service/Article/ArticleService.php
Normal file
99
src/App/Service/Article/ArticleService.php
Normal file
@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Article;
|
||||
|
||||
use App\Entity\Article;
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
use Doctrine\ORM\Query;
|
||||
use Exception;
|
||||
|
||||
class ArticleService implements EntityServiceInterface
|
||||
{
|
||||
|
||||
/**
|
||||
* @var EntityManager
|
||||
*/
|
||||
private $em;
|
||||
|
||||
/**
|
||||
* @var DoctrineObject
|
||||
*/
|
||||
private $hydrator;
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator)
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
16
src/App/Service/Article/ArticleServiceFactory.php
Normal file
16
src/App/Service/Article/ArticleServiceFactory.php
Normal file
@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service\Article;
|
||||
|
||||
use Interop\Container\ContainerInterface;
|
||||
|
||||
class ArticleServiceFactory extends \App\Service\AbstractEntityServiceFactory
|
||||
{
|
||||
|
||||
public function __invoke(ContainerInterface $container)
|
||||
{
|
||||
$em = $this->getEntityManager($container);
|
||||
$hydrator = $this->getDoctrineHydrator($container);
|
||||
return new ArticleService($em, $hydrator);
|
||||
}
|
||||
}
|
||||
22
src/App/Service/EntityServiceInterface.php
Normal file
22
src/App/Service/EntityServiceInterface.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Hydrator\DoctrineObject;
|
||||
use Doctrine\ORM\EntityManager;
|
||||
|
||||
interface EntityServiceInterface
|
||||
{
|
||||
|
||||
public function __construct(EntityManager $em, DoctrineObject $hydrator);
|
||||
|
||||
public function getList();
|
||||
|
||||
public function get($id);
|
||||
|
||||
public function create($data);
|
||||
|
||||
public function modify($id, $data);
|
||||
|
||||
public function delete($id);
|
||||
}
|
||||
38
test/AppTest/Action/Article/ListActionTest.php
Normal file
38
test/AppTest/Action/Article/ListActionTest.php
Normal file
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace AppTest\Action\Article;
|
||||
|
||||
use App\Action\Article\ListAction;
|
||||
use App\Entity\User;
|
||||
use App\Service\Article\ArticleService;
|
||||
use App\Service\EntityServiceInterface;
|
||||
use Zend\Diactoros\Response;
|
||||
use Zend\Diactoros\ServerRequest;
|
||||
|
||||
class ListActionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/** @var EntityServiceInterface */
|
||||
protected $entityService;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$user = new User();
|
||||
|
||||
$entityService = $this->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);
|
||||
}
|
||||
}
|
||||
51
test/AppTest/Action/Article/ListFactoryTest.php
Normal file
51
test/AppTest/Action/Article/ListFactoryTest.php
Normal file
@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace AppTest\Action\Article;
|
||||
|
||||
use App\Action\Article\ListAction;
|
||||
use App\Action\Article\ListFactory;
|
||||
use App\Service\Article\ArticleService;
|
||||
use Interop\Container\ContainerInterface;
|
||||
use Zend\Expressive\Template\TemplateRendererInterface;
|
||||
|
||||
class ListFactoryTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
/** @var ContainerInterface */
|
||||
protected $container;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->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);
|
||||
}
|
||||
}
|
||||
@ -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);
|
||||
|
||||
@ -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()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user