src/EventListener/DeleteRespondListener.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
  4. use App\Services\MessageResponderService;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\HttpKernel\Event\ViewEvent;
  7. class DeleteRespondListener
  8. {
  9.     /**
  10.      * @var MessageResponderService
  11.      */
  12.     private $messageResponder;
  13.     /**
  14.      * @var ResourceMetadataFactoryInterface
  15.      */
  16.     private $resourceMetadataFactory;
  17.     public function __construct(
  18.         MessageResponderService $messageResponder,
  19.         ResourceMetadataFactoryInterface $resourceMetadataFactory
  20.     ) {
  21.         $this->messageResponder $messageResponder;
  22.         $this->resourceMetadataFactory $resourceMetadataFactory;
  23.     }
  24.     public function onKernelView(ViewEvent $event): void
  25.     {
  26.         $resourceClass $event->getRequest()->get('_api_resource_class');
  27.         if (!$resourceClass) {
  28.             return;
  29.         }
  30.         $resourceMetadata $this->resourceMetadataFactory->create($resourceClass);
  31.         $shortName = (string) $resourceMetadata->getShortName();
  32.         $request $event->getRequest();
  33.         if ($request->isMethod('DELETE')) {
  34.             $this->messageResponder->setCode(sprintf('%s.itemSuccessfulyRemoved'strtolower($shortName)));
  35.             $this->messageResponder->setStatus(Response::HTTP_OK);
  36.             $event->setResponse($this->messageResponder->send());
  37.         }
  38.     }
  39. }