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