src/V4/EventSubscriber/Quote/QuotePersistEventSubscriber.php line 100

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Quote;
  3. use ApiPlatform\Core\DataPersister\DataPersisterInterface;
  4. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  5. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  6. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  7. use App\Model\ProspectSubscription\ProspectSubscription;
  8. use App\V4\DataPersister\AbstractWithoutRequestDataPersister;
  9. use App\V4\EventSubscriber\AbstractSubscriber;
  10. use App\V4\Form\Type\ProspectSubscription\ProspectSubscriptionType;
  11. use App\V4\Model\Prospect\Prospect;
  12. use App\V4\Model\Quote\Quote;
  13. use App\V4\Model\QuoteLineInfo\QuoteLineInfo;
  14. use App\V4\Model\QuoteState\QuoteState;
  15. use DateTime;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. use Symfony\Component\Form\FormEvent;
  18. use Symfony\Component\Form\FormEvents;
  19. use Symfony\Component\Form\FormFactoryInterface;
  20. use Symfony\Component\Security\Core\Security;
  21. class QuotePersistEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  22. {
  23.     /**
  24.      * @var CollectionDataProviderInterface
  25.      */
  26.     private $collectionDataProvider;
  27.     /**
  28.      * @var ItemDataProviderInterface
  29.      */
  30.     private $itemDataProvider;
  31.     /**
  32.      * @var FormFactoryInterface
  33.      */
  34.     private $formFactory;
  35.     /**
  36.      * @var DataPersisterInterface
  37.      */
  38.     private $dataPersister;
  39.     /**
  40.      * @var Security
  41.      */
  42.     private $security;
  43.     /**
  44.      * @param CollectionDataProviderInterface $collectionDataProvider
  45.      * @param ItemDataProviderInterface       $itemDataProvider
  46.      * @param FormFactoryInterface            $formFactory
  47.      * @param DataPersisterInterface          $dataPersister
  48.      * @param Security                        $security
  49.      */
  50.     public function __construct(
  51.         CollectionDataProviderInterface $collectionDataProvider,
  52.         ItemDataProviderInterface $itemDataProvider,
  53.         FormFactoryInterface $formFactory,
  54.         DataPersisterInterface $dataPersister,
  55.         Security $security
  56.     ) {
  57.         $this->collectionDataProvider $collectionDataProvider;
  58.         $this->itemDataProvider $itemDataProvider;
  59.         $this->formFactory $formFactory;
  60.         $this->dataPersister $dataPersister;
  61.         $this->security $security;
  62.     }
  63.     /**
  64.      * @param FormEvent $event
  65.      *
  66.      * @return bool
  67.      */
  68.     public function supports(FormEvent $event): bool
  69.     {
  70.         return $event->getData() instanceof Quote
  71.             && $event->getData()->getStatus() instanceof QuoteState
  72.             && $event->getData()->getStatus()->getIsWon()
  73.         ;
  74.     }
  75.     /**
  76.      * @return string[]
  77.      */
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             FormEvents::POST_SUBMIT => 'onPostSubmit',
  82.         ];
  83.     }
  84.     /**
  85.      * @param FormEvent $event
  86.      *
  87.      * @throws ResourceClassNotSupportedException
  88.      */
  89.     public function onPostSubmit(FormEvent $event): void
  90.     {
  91.         if (!$this->supports($event) || $this->isIdAlreadyManaged($event->getData()->getId(), get_class($this))) {
  92.             return;
  93.         }
  94.         /** @var Quote $quote */
  95.         $quote $event->getData();
  96.         $prospectSubscriptions $this->collectionDataProvider->getCollection(ProspectSubscription::class, null, [
  97.             'filters' => [
  98.                 'quote' => $quote->getId(),
  99.             ],
  100.             'transformResponse' => false,
  101.         ]);
  102.         if (!empty($prospectSubscriptions)) {
  103.             return;
  104.         }
  105.         $user $this->security->getUser();
  106.         $prospect $quote->getProspect() ?? $this->itemDataProvider->getItem(Prospect::class, $quote->getProspectId());
  107.         foreach ($quote->getQuoteLines() as $quoteLine) {
  108.             if (!$quoteLine->getQuoteLineInfo() instanceof QuoteLineInfo
  109.                 || !$quoteLine->getQuoteLineInfo()->hasSubscriptionProduct()) {
  110.                 continue;
  111.             }
  112.             $product $quoteLine->getQuoteLineInfo()->getProduct();
  113.             $endAt $quote->getExpectedSignedAt() instanceof DateTime ? clone $quote->getExpectedSignedAt() : new DateTime();
  114.             $endAt->modify(sprintf('+%s %s'$product->getSubscriptionValue(), $product->getSubscriptionUnit()));
  115.             $prospectSubscription = (new ProspectSubscription())
  116.                 ->setCustomerId($quote->getCustomerId() ?? $user->getCustomerId())
  117.                 ->setProspect($prospect)
  118.                 ->setQuote($quote)
  119.                 ->setProduct($product)
  120.                 ->setProductName($product->getName())
  121.                 ->setProductDescription($product->getShortDescription())
  122.                 ->setProductReference($product->getReference())
  123.                 ->setBeginAt($quote->getExpectedSignedAt() ?? new DateTime())
  124.                 ->setEndAt($endAt)
  125.             ;
  126.             $prospectSubscriptionForm $this->formFactory->create(
  127.                 ProspectSubscriptionType::class,
  128.                 $prospectSubscription,
  129.                 ['prospectId' => $quote->getProspectId()]
  130.             );
  131.             $prospectSubscriptionForm->submit(['prospect' => $quote->getProspectId()], false);
  132.             if ($prospectSubscriptionForm->isValid() && $prospectSubscriptionForm->isSubmitted()) {
  133.                 /** @var ProspectSubscription $prospectSubscription */
  134.                 $prospectSubscription $prospectSubscriptionForm->getData();
  135.                 if ($prospectSubscription->getProspect() instanceof Prospect
  136.                     && $prospectSubscription->getQuote() instanceof Quote) {
  137.                     $prospectSubscription->getProspect()->setNormalizeAsIRI(true);
  138.                     $prospectSubscription->getQuote()->setNormalizeAsIRI(true);
  139.                     $this->dataPersister->persist($prospectSubscription, [
  140.                         AbstractWithoutRequestDataPersister::CONTEXT_IS_V4 => true,
  141.                     ]);
  142.                     $prospectSubscription->getQuote()->setNormalizeAsIRI(false);
  143.                 }
  144.             }
  145.         }
  146.     }
  147. }