src/V4/EventSubscriber/Quote/QuoteIsWonProspectIsClientSubscriber.php line 102

Open in your IDE?
  1. <?php
  2. namespace App\V4\EventSubscriber\Quote;
  3. use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
  4. use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
  5. use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
  6. use App\Service\ApiWebService;
  7. use App\Service\ApiWebServiceFilterBuilder\PaginationFilterBuilder;
  8. use App\Service\Cache\CacheManager;
  9. use App\Service\Provider\ApiCrmProvider;
  10. use App\V4\Event\PostPersistEvent;
  11. use App\V4\EventSubscriber\AbstractSubscriber;
  12. use App\V4\Logger\SentryLogger;
  13. use App\V4\Model\Params\Params;
  14. use App\V4\Model\Prospect\Prospect;
  15. use App\V4\Model\ProspectType\ProspectType;
  16. use App\V4\Model\Quote\Quote;
  17. use App\V4\Model\QuoteState\QuoteState;
  18. use Psr\Cache\InvalidArgumentException;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Throwable;
  22. class QuoteIsWonProspectIsClientSubscriber extends AbstractSubscriber implements EventSubscriberInterface
  23. {
  24.     /**
  25.      * @var CollectionDataProviderInterface
  26.      */
  27.     private $collectionDataProvider;
  28.     /**
  29.      * @var ItemDataProviderInterface
  30.      */
  31.     private $itemDataProvider;
  32.     /**
  33.      * @var ApiWebService
  34.      */
  35.     private $apiWebService;
  36.     /**
  37.      * @var ApiCrmProvider
  38.      */
  39.     private $apiCrmProvider;
  40.     /**
  41.      * @var CacheManager
  42.      */
  43.     private $cacheManager;
  44.     /**
  45.      * @var SentryLogger
  46.      */
  47.     private $sentryLogger;
  48.     public function __construct(
  49.         ItemDataProviderInterface $itemDataProvider,
  50.         CollectionDataProviderInterface $collectionDataProvider,
  51.         ApiWebService $apiWebService,
  52.         ApiCrmProvider $apiCrmProvider,
  53.         CacheManager $cacheManager,
  54.         SentryLogger $sentryLogger
  55.     ) {
  56.         $this->collectionDataProvider $collectionDataProvider;
  57.         $this->itemDataProvider $itemDataProvider;
  58.         $this->apiWebService $apiWebService;
  59.         $this->apiCrmProvider $apiCrmProvider;
  60.         $this->cacheManager $cacheManager;
  61.         $this->sentryLogger $sentryLogger;
  62.     }
  63.     /**
  64.      * @param $objPersisted
  65.      *
  66.      * @return bool
  67.      */
  68.     public function supports($objPersisted): bool
  69.     {
  70.         return $objPersisted instanceof Quote
  71.             && $objPersisted->getStatus() instanceof QuoteState
  72.             && $objPersisted->getStatus()->getIsWon()
  73.         ;
  74.     }
  75.     /**
  76.      * @return string[]
  77.      */
  78.     public static function getSubscribedEvents(): array
  79.     {
  80.         return [
  81.             PostPersistEvent::NAME => 'onPostPersist',
  82.         ];
  83.     }
  84.     /**
  85.      * @param PostPersistEvent $event
  86.      *
  87.      * @throws ResourceClassNotSupportedException
  88.      * @throws InvalidArgumentException
  89.      */
  90.     public function onPostPersist(PostPersistEvent $event): void
  91.     {
  92.         /** @var Quote $objPersisted */
  93.         $objPersisted $event->getAfter();
  94.         if (!$this->supports($objPersisted) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
  95.             return;
  96.         }
  97.         $prospect $objPersisted->getProspect();
  98.         if (!$prospect instanceof Prospect) {
  99.             if (null === $objPersisted->getProspectId()) {
  100.                 return;
  101.             }
  102.             $prospect $this->itemDataProvider->getItem(Prospect::class, $objPersisted->getProspectId());
  103.         }
  104.         if (!$prospect instanceof Prospect || ($prospect->getProspectType() instanceof Params && $prospect->getProspectType()->getIsClient())) {
  105.             return;
  106.         }
  107.         $prospectTypes $this->collectionDataProvider->getCollection(ProspectType::class, null, [
  108.             'filters' => [
  109.                 'isClient' => true,
  110.                 'order[order]' => 'ASC',
  111.                 PaginationFilterBuilder::DATA_FILTER_ITEM_PER_PAGE => 1,
  112.             ],
  113.         ]);
  114.         $prospectTypeClient $prospectTypes[0] ?? null;
  115.         if (!$prospectTypeClient instanceof ProspectType) {
  116.             return;
  117.         }
  118.         try {
  119.             $this->apiWebService->send(
  120.                 $this->apiCrmProvider,
  121.                 Request::METHOD_PATCH,
  122.                 '/api/prospects/'.$prospect->getId(),
  123.                 [
  124.                     'prospectType' => $prospectTypeClient->getId(),
  125.                 ]
  126.             );
  127.             $this->cacheManager->invalidateTag([Prospect::class]);
  128.             $objPersisted->setIsProspectRefreshNeeded(true);
  129.         } catch (Throwable $exception) {
  130.             $this->sentryLogger->captureException(
  131.                 SentryLogger::CHANNEL_SUBSCRIBER,
  132.                 $exception,
  133.                 [
  134.                     'catchOnClass' => get_class($this),
  135.                     'QuoteSubscriber' => 'PATCH prospect',
  136.                     'param request' => [
  137.                         'url' => '/api/prospects/'.$prospect->getId(),
  138.                         'method' => Request::METHOD_PATCH,
  139.                         'id' => $prospect->getId(),
  140.                         'prospectType' => $prospectTypeClient->getId(),
  141.                     ],
  142.                 ]
  143.             );
  144.         }
  145.     }
  146. }