src/EventSubscriber/ProspectApiCreationSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Enum\ProspectOnStoreCreationModeEnum;
  5. use App\Entity\Prospect;
  6. use App\Exception\DuplicateProspectException;
  7. use App\Service\ProspectDuplicateManagementService;
  8. use App\Service\ProspectService;
  9. use App\Service\StatService;
  10. use App\Service\UtmManager;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpKernel\Event\ViewEvent;
  15. use Symfony\Component\HttpKernel\KernelEvents;
  16. /**
  17.  * Suscribe to event regarding prospect accessed using API
  18.  * @package App\EventSubscriber
  19.  */
  20. final class ProspectApiCreationSubscriber  implements EventSubscriberInterface
  21. {
  22.     protected $prospectService;
  23.     protected $logger;
  24.     protected $prospectDuplicateManagementService;
  25.     private $statService;
  26.     private UtmManager $utmManager;
  27.     public function __construct(
  28.         ProspectService $prospectService,
  29.         ProspectDuplicateManagementService $prospectDuplicateManagementService,
  30.         StatService $statService,
  31.         LoggerInterface $logger,
  32.         UtmManager $utmManager)
  33.     {
  34.         $this->prospectService $prospectService;
  35.         $this->prospectDuplicateManagementService $prospectDuplicateManagementService;
  36.         $this->logger $logger;
  37.         $this->statService=$statService;
  38.         $this->utmManager=$utmManager;
  39.     }
  40.     public static function getSubscribedEvents()
  41.     {
  42.         return [
  43.             KernelEvents::VIEW => ['preWriteProspect'EventPriorities::PRE_WRITE],
  44.         ];
  45.     }
  46.     /**
  47.      * Call appropriate method when a prospect is created using API
  48.      * @param ViewEvent $event
  49.      * @throws \App\Exception\DuplicateProspectException
  50.      * @throws \App\Exception\NoStoreFoundException
  51.      */
  52.     public function preWriteProspect(ViewEvent $event): void
  53.     {
  54.         $this->logger->debug('ProspectApiSubscriber->preWriteProspect');
  55.         $prospect $event->getControllerResult();
  56.         $method $event->getRequest()->getMethod();
  57.         if (!$prospect instanceof Prospect || Request::METHOD_POST !== $method) {
  58.             return;
  59.         }
  60.         if (Request::METHOD_POST === $method) {
  61.             $prospect->setRequest(Prospect::TYPE_CHECKUP);
  62.             $prospect->setIsPrivacy(true);
  63.             $acquisitionMode $prospect->getAcquisitionMode();
  64.             if(empty($acquisitionMode)){
  65.                 $acquisitionMode Prospect::ACQUISITION_MODE_API;
  66.             }
  67.             //force UTM
  68.             $this->utmManager->setUtms([
  69.                 'source'=>$acquisitionMode,
  70.                 'content'=>$prospect->getAcquisitionBase(),
  71.                 'keyword'=>$prospect->getAcquisitionKeyword(),
  72.                 'url'=>$prospect->getAcquisitionUrl(),
  73.             ]);
  74.             try {
  75.                 $this->prospectService->createAndPersistProspectForStore($prospect);
  76.                 $this->logger->info('no exception.. keep going');
  77.             } catch (DuplicateProspectException $duplicateProspectException) {
  78.                 $oldProspect $duplicateProspectException->getOldProspect();
  79.                 $this->logger->info('DuplicateProspectException');
  80.                 if ($duplicateProspectException->isOlderThan30Days()) {
  81.                     $this->logger->info('older Than 30Days');
  82.                     if (!($duplicateProspectException->isSameStore() || $duplicateProspectException->isSameAdmininistrator())) {
  83.                         $currentStore $prospect->getProspectsOnStore()->first()->getStore();
  84.                         $this->prospectService->deliverProspectOnNewStore($oldProspect,$currentStorefalsenullnullProspectOnStoreCreationModeEnum::DUPLICATE);
  85.                         $event->setControllerResult($oldProspect);
  86.                     } else {
  87.                         $this->logger->info('same Store Or same Administrator => no delivery');
  88.                         throw $duplicateProspectException;
  89.                     }
  90.                 }else{
  91.                     $this->logger->info('less than 30 days');
  92.                     throw $duplicateProspectException;
  93.                 }
  94.             }
  95.             //add fake event for stat
  96.             foreach ($prospect->getProspectsOnStore() as $key => $pos) {
  97.                 $this->statService->saveFakeDisplayAPIEvent($pos->getStore()->getId());
  98.             }
  99.         }
  100.     }
  101. }