49 lines
1.1 KiB
PHP
49 lines
1.1 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Action\Article;
|
||
|
|
|
||
|
|
use App\Entity\Article;
|
||
|
|
use Doctrine\ORM\EntityManager;
|
||
|
|
use Psr\Http\Message\ResponseInterface;
|
||
|
|
use Psr\Http\Message\ServerRequestInterface;
|
||
|
|
use Zend\Diactoros\Response\JsonResponse;
|
||
|
|
|
||
|
|
class DeleteAction
|
||
|
|
{
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @var EntityManager
|
||
|
|
*/
|
||
|
|
private $em;
|
||
|
|
|
||
|
|
public function __construct(EntityManager $em)
|
||
|
|
{
|
||
|
|
$this->em = $em;
|
||
|
|
}
|
||
|
|
|
||
|
|
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);
|
||
|
|
}
|
||
|
|
|
||
|
|
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,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|