<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Enum\ProspectOnStoreCreationModeEnum;
use App\Entity\Prospect;
use App\Exception\DuplicateProspectException;
use App\Service\ProspectDuplicateManagementService;
use App\Service\ProspectService;
use App\Service\StatService;
use App\Service\UtmManager;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Suscribe to event regarding prospect accessed using API
* @package App\EventSubscriber
*/
final class ProspectApiCreationSubscriber implements EventSubscriberInterface
{
protected $prospectService;
protected $logger;
protected $prospectDuplicateManagementService;
private $statService;
private UtmManager $utmManager;
public function __construct(
ProspectService $prospectService,
ProspectDuplicateManagementService $prospectDuplicateManagementService,
StatService $statService,
LoggerInterface $logger,
UtmManager $utmManager)
{
$this->prospectService = $prospectService;
$this->prospectDuplicateManagementService = $prospectDuplicateManagementService;
$this->logger = $logger;
$this->statService=$statService;
$this->utmManager=$utmManager;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => ['preWriteProspect', EventPriorities::PRE_WRITE],
];
}
/**
* Call appropriate method when a prospect is created using API
* @param ViewEvent $event
* @throws \App\Exception\DuplicateProspectException
* @throws \App\Exception\NoStoreFoundException
*/
public function preWriteProspect(ViewEvent $event): void
{
$this->logger->debug('ProspectApiSubscriber->preWriteProspect');
$prospect = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
if (!$prospect instanceof Prospect || Request::METHOD_POST !== $method) {
return;
}
if (Request::METHOD_POST === $method) {
$prospect->setRequest(Prospect::TYPE_CHECKUP);
$prospect->setIsPrivacy(true);
$acquisitionMode = $prospect->getAcquisitionMode();
if(empty($acquisitionMode)){
$acquisitionMode = Prospect::ACQUISITION_MODE_API;
}
//force UTM
$this->utmManager->setUtms([
'source'=>$acquisitionMode,
'content'=>$prospect->getAcquisitionBase(),
'keyword'=>$prospect->getAcquisitionKeyword(),
'url'=>$prospect->getAcquisitionUrl(),
]);
try {
$this->prospectService->createAndPersistProspectForStore($prospect);
$this->logger->info('no exception.. keep going');
} catch (DuplicateProspectException $duplicateProspectException) {
$oldProspect = $duplicateProspectException->getOldProspect();
$this->logger->info('DuplicateProspectException');
if ($duplicateProspectException->isOlderThan30Days()) {
$this->logger->info('older Than 30Days');
if (!($duplicateProspectException->isSameStore() || $duplicateProspectException->isSameAdmininistrator())) {
$currentStore = $prospect->getProspectsOnStore()->first()->getStore();
$this->prospectService->deliverProspectOnNewStore($oldProspect,$currentStore, false, null, null, ProspectOnStoreCreationModeEnum::DUPLICATE);
$event->setControllerResult($oldProspect);
} else {
$this->logger->info('same Store Or same Administrator => no delivery');
throw $duplicateProspectException;
}
}else{
$this->logger->info('less than 30 days');
throw $duplicateProspectException;
}
}
//add fake event for stat
foreach ($prospect->getProspectsOnStore() as $key => $pos) {
$this->statService->saveFakeDisplayAPIEvent($pos->getStore()->getId());
}
}
}
}