src/V4/EventSubscriber/Quote/QuoteManageTasksSubscriber.php line 87

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Quote;
  3. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  4. use App\V4\Event\PostPersistEvent;
  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\Component\Serializer\Exception\ExceptionInterface;
  13. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  14. use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  18. class QuoteManageTasksSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var CrudTaskHandler
  22.      */
  23.     private $crudTaskHandler;
  24.     /**
  25.      * @param CrudTaskHandler $crudTaskHandler
  26.      */
  27.     public function __construct(
  28.         CrudTaskHandler $crudTaskHandler
  29.     ) {
  30.         $this->crudTaskHandler $crudTaskHandler;
  31.     }
  32.     /**
  33.      * @param $objPersisted
  34.      * @param $objBefore
  35.      *
  36.      * @return bool
  37.      */
  38.     public function supports($objPersisted$objBefore): bool
  39.     {
  40.         if (!$objPersisted instanceof Quote) {
  41.             // On ne gère que les devis
  42.             return false;
  43.         }
  44.         if (!$objPersisted->getStatus() instanceof QuoteState) {
  45.             // On ne gère que les devis avec un statut
  46.             return false;
  47.         }
  48.         if (!$objBefore instanceof Quote || !$objBefore->getStatus() instanceof QuoteState) {
  49.             // On déclenche s'il s'agit d'une création de devis
  50.             // On déclenche si l'ancien devis n'a pas de statut (ce qui devrait arriver de moins en moins)
  51.             return true;
  52.         }
  53.         // On déclenche si le status du devis à changer
  54.         return $objPersisted->getStatus()->getId() !== $objBefore->getStatus()->getId();
  55.     }
  56.     /**
  57.      * @return string[]
  58.      */
  59.     public static function getSubscribedEvents(): array
  60.     {
  61.         return [
  62.             PostPersistEvent::NAME => 'onPostPersist',
  63.         ];
  64.     }
  65.     /**
  66.      * @param PostPersistEvent $event
  67.      *
  68.      * @throws ClientExceptionInterface
  69.      * @throws DecodingExceptionInterface
  70.      * @throws ExceptionInterface
  71.      * @throws RedirectionExceptionInterface
  72.      * @throws ReflectionException
  73.      * @throws ResourceClassNotSupportedException
  74.      * @throws ServerExceptionInterface
  75.      * @throws TransportExceptionInterface
  76.      */
  77.     public function onPostPersist(PostPersistEvent $event): void
  78.     {
  79.         /** @var Quote $objPersisted */
  80.         $objPersisted $event->getAfter();
  81.         $objBefore $event->getBefore();
  82.         if (!$this->supports($objPersisted$objBefore) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
  83.             return;
  84.         }
  85.         $this->closeTasksConcernedByQuote($objPersisted);
  86.         if (true !== $objPersisted->getStatus()->getIsDiffered()) {
  87.             return;
  88.         }
  89.         $this->crudTaskHandler->createReminderTaskAuto($objPersisted);
  90.     }
  91.     /**
  92.      * @param Quote $objPersisted
  93.      *
  94.      * @return void
  95.      *
  96.      * @throws ClientExceptionInterface
  97.      * @throws DecodingExceptionInterface
  98.      * @throws RedirectionExceptionInterface
  99.      * @throws ReflectionException
  100.      * @throws ResourceClassNotSupportedException
  101.      * @throws ServerExceptionInterface
  102.      * @throws TransportExceptionInterface
  103.      */
  104.     private function closeTasksConcernedByQuote(Quote $objPersisted)
  105.     {
  106.         if ($objPersisted->getStatus()->getInProgress()) {
  107.             return;
  108.         }
  109.         $this->crudTaskHandler->closeTasksConcernedByQuote($objPersistedTask::MEMO_LINE_TASK_WHEN_QUOTE_STATE_CHANGED);
  110.     }
  111. }