src/V4/EventSubscriber/Quote/QuotePostRemoveEventSubscriber.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Quote;
  3. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  4. use App\V4\Event\PostRemoveEvent;
  5. use App\V4\EventSubscriber\AbstractSubscriber;
  6. use App\V4\Handler\Task\CrudTaskHandler;
  7. use App\V4\Model\Quote\Quote;
  8. use App\V4\Model\QuoteState\QuoteState;
  9. use App\V4\Model\Task\Task;
  10. use ReflectionException;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  17. class QuotePostRemoveEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var CrudTaskHandler
  21.      */
  22.     private $crudTaskHandler;
  23.     /**
  24.      * @param CrudTaskHandler $crudTaskHandler
  25.      */
  26.     public function __construct(
  27.         CrudTaskHandler $crudTaskHandler
  28.     ) {
  29.         $this->crudTaskHandler $crudTaskHandler;
  30.     }
  31.     /**
  32.      * @param $entity
  33.      *
  34.      * @return bool
  35.      */
  36.     public function supports($entity): bool
  37.     {
  38.         if ($entity instanceof Quote && $entity->getStatus() instanceof QuoteState) {
  39.             return !$this->isIdAlreadyManaged($entity->getId(), get_class($this));
  40.         }
  41.         return false;
  42.     }
  43.     /**
  44.      * @return string[]
  45.      */
  46.     public static function getSubscribedEvents(): array
  47.     {
  48.         return [
  49.             PostRemoveEvent::NAME => 'onPostRemove',
  50.         ];
  51.     }
  52.     /**
  53.      * @param PostRemoveEvent $event
  54.      *
  55.      * @throws RedirectionExceptionInterface
  56.      * @throws DecodingExceptionInterface
  57.      * @throws ClientExceptionInterface
  58.      * @throws TransportExceptionInterface
  59.      * @throws ServerExceptionInterface
  60.      * @throws ReflectionException
  61.      * @throws ResourceClassNotSupportedException
  62.      */
  63.     public function onPostRemove(PostRemoveEvent $event): void
  64.     {
  65.         /** @var Quote $objRemoved */
  66.         $objRemoved $event->getAfter();
  67.         if (!$objRemoved instanceof Quote) {
  68.             return;
  69.         }
  70.         $this->closeTasksConcernedByQuote($objRemoved);
  71.     }
  72.     /**
  73.      * @param Quote $objPersisted
  74.      *
  75.      * @return void
  76.      *
  77.      * @throws ClientExceptionInterface
  78.      * @throws DecodingExceptionInterface
  79.      * @throws RedirectionExceptionInterface
  80.      * @throws ReflectionException
  81.      * @throws ResourceClassNotSupportedException
  82.      * @throws ServerExceptionInterface
  83.      * @throws TransportExceptionInterface
  84.      */
  85.     private function closeTasksConcernedByQuote(Quote $objPersisted)
  86.     {
  87.         $this->crudTaskHandler->closeTasksConcernedByQuote($objPersistedTask::MEMO_LINE_TASK_WHEN_QUOTE_REMOVED);
  88.     }
  89. }