* refactored with entity service, so the code can be tested

This commit is contained in:
Danyi Dávid
2016-08-01 17:30:43 +02:00
parent f3939bbd13
commit 2de0bf8add
35 changed files with 359 additions and 215 deletions

View File

@@ -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);
}
}
}