<?php
namespace App\V4\EventSubscriber\Quote;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\Service\ApiWebService;
use App\Service\ApiWebServiceFilterBuilder\PaginationFilterBuilder;
use App\Service\Cache\CacheManager;
use App\Service\Provider\ApiCrmProvider;
use App\V4\Event\PostPersistEvent;
use App\V4\EventSubscriber\AbstractSubscriber;
use App\V4\Logger\SentryLogger;
use App\V4\Model\Params\Params;
use App\V4\Model\Prospect\Prospect;
use App\V4\Model\ProspectType\ProspectType;
use App\V4\Model\Quote\Quote;
use App\V4\Model\QuoteState\QuoteState;
use Psr\Cache\InvalidArgumentException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Request;
use Throwable;
class QuoteIsWonProspectIsClientSubscriber extends AbstractSubscriber implements EventSubscriberInterface
{
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var ItemDataProviderInterface
*/
private $itemDataProvider;
/**
* @var ApiWebService
*/
private $apiWebService;
/**
* @var ApiCrmProvider
*/
private $apiCrmProvider;
/**
* @var CacheManager
*/
private $cacheManager;
/**
* @var SentryLogger
*/
private $sentryLogger;
public function __construct(
ItemDataProviderInterface $itemDataProvider,
CollectionDataProviderInterface $collectionDataProvider,
ApiWebService $apiWebService,
ApiCrmProvider $apiCrmProvider,
CacheManager $cacheManager,
SentryLogger $sentryLogger
) {
$this->collectionDataProvider = $collectionDataProvider;
$this->itemDataProvider = $itemDataProvider;
$this->apiWebService = $apiWebService;
$this->apiCrmProvider = $apiCrmProvider;
$this->cacheManager = $cacheManager;
$this->sentryLogger = $sentryLogger;
}
/**
* @param $objPersisted
*
* @return bool
*/
public function supports($objPersisted): bool
{
return $objPersisted instanceof Quote
&& $objPersisted->getStatus() instanceof QuoteState
&& $objPersisted->getStatus()->getIsWon()
;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
PostPersistEvent::NAME => 'onPostPersist',
];
}
/**
* @param PostPersistEvent $event
*
* @throws ResourceClassNotSupportedException
* @throws InvalidArgumentException
*/
public function onPostPersist(PostPersistEvent $event): void
{
/** @var Quote $objPersisted */
$objPersisted = $event->getAfter();
if (!$this->supports($objPersisted) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
return;
}
$prospect = $objPersisted->getProspect();
if (!$prospect instanceof Prospect) {
if (null === $objPersisted->getProspectId()) {
return;
}
$prospect = $this->itemDataProvider->getItem(Prospect::class, $objPersisted->getProspectId());
}
if (!$prospect instanceof Prospect || ($prospect->getProspectType() instanceof Params && $prospect->getProspectType()->getIsClient())) {
return;
}
$prospectTypes = $this->collectionDataProvider->getCollection(ProspectType::class, null, [
'filters' => [
'isClient' => true,
'order[order]' => 'ASC',
PaginationFilterBuilder::DATA_FILTER_ITEM_PER_PAGE => 1,
],
]);
$prospectTypeClient = $prospectTypes[0] ?? null;
if (!$prospectTypeClient instanceof ProspectType) {
return;
}
try {
$this->apiWebService->send(
$this->apiCrmProvider,
Request::METHOD_PATCH,
'/api/prospects/'.$prospect->getId(),
[
'prospectType' => $prospectTypeClient->getId(),
]
);
$this->cacheManager->invalidateTag([Prospect::class]);
$objPersisted->setIsProspectRefreshNeeded(true);
} catch (Throwable $exception) {
$this->sentryLogger->captureException(
SentryLogger::CHANNEL_SUBSCRIBER,
$exception,
[
'catchOnClass' => get_class($this),
'QuoteSubscriber' => 'PATCH prospect',
'param request' => [
'url' => '/api/prospects/'.$prospect->getId(),
'method' => Request::METHOD_PATCH,
'id' => $prospect->getId(),
'prospectType' => $prospectTypeClient->getId(),
],
]
);
}
}
}