* refactored with entity service, so the code can be tested
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user