src/V4/EventSubscriber/Task/TaskPostPersistEventSubscriber.php line 80

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Task;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  5. use App\V4\DataPersister\AbstractWithoutRequestDataPersister;
  6. use App\V4\Event\PostPersistEvent;
  7. use App\V4\EventSubscriber\AbstractSubscriber;
  8. use App\V4\Handler\Task\CrudTaskHandler;
  9. use App\V4\Logger\SentryLogger;
  10. use App\V4\Model\Quote\Quote;
  11. use App\V4\Model\QuoteReason\QuoteReason;
  12. use App\V4\Model\QuoteState\QuoteState;
  13. use App\V4\Model\QuoteTaskExternalRef\QuoteTaskExternalRef;
  14. use App\V4\Model\Task\Task;
  15. use Psr\Log\LogLevel;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Serializer\Exception\ExceptionInterface;
  18. class TaskPostPersistEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  19. {
  20.     /**
  21.      * @var CollectionDataProviderInterface
  22.      */
  23.     private $collectionDataProvider;
  24.     /**
  25.      * @var CrudTaskHandler
  26.      */
  27.     private $crudTaskHandler;
  28.     /**
  29.      * @var SentryLogger
  30.      */
  31.     private $sentryLogger;
  32.     /**
  33.      * @param CollectionDataProviderInterface $collectionDataProvider
  34.      * @param CrudTaskHandler                 $crudTaskHandler
  35.      * @param SentryLogger                    $sentryLogger
  36.      */
  37.     public function __construct(
  38.         CollectionDataProviderInterface $collectionDataProvider,
  39.         CrudTaskHandler $crudTaskHandler,
  40.         SentryLogger $sentryLogger
  41.     ) {
  42.         $this->collectionDataProvider $collectionDataProvider;
  43.         $this->crudTaskHandler $crudTaskHandler;
  44.         $this->sentryLogger $sentryLogger;
  45.     }
  46.     /**
  47.      * @param $entity
  48.      *
  49.      * @return bool
  50.      */
  51.     public function supports($entity): bool
  52.     {
  53.         return $entity instanceof Task && !$entity->getQuotes()->isEmpty();
  54.     }
  55.     /**
  56.      * @return string[]
  57.      */
  58.     public static function getSubscribedEvents(): array
  59.     {
  60.         return [
  61.             PostPersistEvent::NAME => 'onPostPersist',
  62.         ];
  63.     }
  64.     /**
  65.      * @param PostPersistEvent $event
  66.      *
  67.      * @throws ResourceClassNotSupportedException
  68.      * @throws ExceptionInterface
  69.      * @throws \Exception
  70.      */
  71.     public function onPostPersist(PostPersistEvent $event): void
  72.     {
  73.         /** @var Task $objPersisted */
  74.         $objPersisted $event->getAfter();
  75.         if (!$this->supports($objPersisted) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
  76.             return;
  77.         }
  78.         $quoteIds = [];
  79.         foreach ($objPersisted->getQuotes() as $quoteRef) {
  80.             $quoteIds[] = $quoteRef->getExternalValue();
  81.         }
  82.         if (empty($quoteIds)) {
  83.             // You can never be too careful, apparently.
  84.             $this->sentryLogger->captureMessage(
  85.                 SentryLogger::CHANNEL_SUBSCRIBER,
  86.                 'TaskPostPersistEventSubscriber::onPostPersist() called with empty quoteIds. (bypassed checks)',
  87.                 [
  88.                     'catchOnClass' => self::class,
  89.                 ],
  90.                 LogLevel::CRITICAL
  91.             );
  92.             return;
  93.         }
  94.         $quotes $this->collectionDataProvider->getCollection(Quote::class, null, [
  95.             'filters' => [
  96.                 'id' => $quoteIds,
  97.             ],
  98.         ]);
  99.         /** @var Quote $quote */
  100.         foreach ($quotes as $quote) {
  101.             if ($quote->hasTask($objPersisted->getId())) {
  102.                 continue;
  103.             }
  104.             if ($quote->getStatus() instanceof QuoteState) {
  105.                 $quote->getStatus()->setNormalizeAsIRI(true);
  106.             }
  107.             foreach ($quote->getQuoteLines() as $quoteLine) {
  108.                 $quoteLine->setNormalizeAsIRI(true);
  109.             }
  110.             foreach ($quote->getTasks() as $task) {
  111.                 $task->setNormalizeAsIRI(true);
  112.             }
  113.             if ($quote->getReason() instanceof QuoteReason) {
  114.                 $quote->getReason()->setNormalizeAsIRI(true);
  115.             }
  116.             $quote->addTask((new QuoteTaskExternalRef())->setExternalValue($objPersisted->getId()));
  117.             $this->crudTaskHandler->submitQuoteForm(
  118.                 $quote,
  119.                 [AbstractWithoutRequestDataPersister::CONTEXT_NOT_POST_PERSIST_EVENT => true]
  120.             );
  121.         }
  122.     }
  123. }