* 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

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