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

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