src/V4/EventSubscriber/CustomerEventServiceEventSubscriber.php line 50

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber;
  3. use App\DataPersister\AbstractDataPersister;
  4. use App\V4\Builder\CustomerEventServices\CustomerEventServicesBuilder;
  5. use App\V4\Event\PostPersistEvent;
  6. use App\V4\Model\ChildrenAwareInterface;
  7. use App\V4\Model\Quote\Quote;
  8. use App\V4\Service\Quote\QuoteExternalCalculation;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /* @see: https://sfimultimedia.atlassian.net/browse/PV-1203 Dette Technique */
  11. class CustomerEventServiceEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  12. {
  13.     /**
  14.      * @var CustomerEventServicesBuilder
  15.      */
  16.     private $customerEventServicesBuilder;
  17.     /**
  18.      * @var QuoteExternalCalculation
  19.      */
  20.     private $quoteExternalCalculation;
  21.     public function __construct(
  22.         CustomerEventServicesBuilder $customerEventServicesBuilder,
  23.         QuoteExternalCalculation $quoteExternalCalculation
  24.     ) {
  25.         $this->customerEventServicesBuilder $customerEventServicesBuilder;
  26.         $this->quoteExternalCalculation $quoteExternalCalculation;
  27.     }
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return [
  31.             PostPersistEvent::NAME => 'onPostPersist',
  32.         ];
  33.     }
  34.     public function supports($entity): bool
  35.     {
  36.         return $entity instanceof ChildrenAwareInterface
  37.             && method_exists($entity'getId')
  38.             && null !== $entity->getId()
  39.             && $this->customerEventServicesBuilder->hasCustomerEventServices()
  40.         ;
  41.     }
  42.     public function onPostPersist(PostPersistEvent $event): void
  43.     {
  44.         /** @var Quote $objPersisted */
  45.         $objPersisted $event->getAfter();
  46.         $context $event->getContext();
  47.         if (!$this->supports($objPersisted) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
  48.             return;
  49.         }
  50.         if (empty($context[AbstractDataPersister::CONTEXT_ORIGIN]) || QuoteExternalCalculation::class !== $context[AbstractDataPersister::CONTEXT_ORIGIN]) {
  51.             $this->quoteExternalCalculation->process($objPersisted);
  52.         }
  53.     }
  54. }