<?php
namespace App\V4\EventSubscriber\Task;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\V4\DataPersister\AbstractWithoutRequestDataPersister;
use App\V4\Event\PostPersistEvent;
use App\V4\EventSubscriber\AbstractSubscriber;
use App\V4\Handler\Task\CrudTaskHandler;
use App\V4\Logger\SentryLogger;
use App\V4\Model\Quote\Quote;
use App\V4\Model\QuoteState\QuoteState;
use App\V4\Model\QuoteTaskExternalRef\QuoteTaskExternalRef;
use App\V4\Model\Task\Task;
use Psr\Log\LogLevel;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
class TaskPostPersistEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
{
/**
* @var CollectionDataProviderInterface
*/
private $collectionDataProvider;
/**
* @var CrudTaskHandler
*/
private $crudTaskHandler;
/**
* @var SentryLogger
*/
private $sentryLogger;
/**
* @param CollectionDataProviderInterface $collectionDataProvider
* @param CrudTaskHandler $crudTaskHandler
* @param SentryLogger $sentryLogger
*/
public function __construct(
CollectionDataProviderInterface $collectionDataProvider,
CrudTaskHandler $crudTaskHandler,
SentryLogger $sentryLogger
) {
$this->collectionDataProvider = $collectionDataProvider;
$this->crudTaskHandler = $crudTaskHandler;
$this->sentryLogger = $sentryLogger;
}
/**
* @param $entity
*
* @return bool
*/
public function supports($entity): bool
{
return $entity instanceof Task && !$entity->getQuotes()->isEmpty();
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
PostPersistEvent::NAME => 'onPostPersist',
];
}
/**
* @param PostPersistEvent $event
*
* @throws ResourceClassNotSupportedException
* @throws ExceptionInterface
* @throws \Exception
*/
public function onPostPersist(PostPersistEvent $event): void
{
/** @var Task $objPersisted */
$objPersisted = $event->getAfter();
if (!$this->supports($objPersisted) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
return;
}
$quoteIds = [];
foreach ($objPersisted->getQuotes() as $quoteRef) {
$quoteIds[] = $quoteRef->getExternalValue();
}
if (empty($quoteIds)) {
// You can never be too careful, apparently.
$this->sentryLogger->captureMessage(
SentryLogger::CHANNEL_SUBSCRIBER,
'TaskPostPersistEventSubscriber::onPostPersist() called with empty quoteIds. (bypassed checks)',
[
'catchOnClass' => self::class,
],
LogLevel::CRITICAL
);
return;
}
$quotes = $this->collectionDataProvider->getCollection(Quote::class, null, [
'filters' => [
'id' => $quoteIds,
],
]);
/** @var Quote $quote */
foreach ($quotes as $quote) {
if ($quote->hasTask($objPersisted->getId())) {
continue;
}
if ($quote->getStatus() instanceof QuoteState) {
$quote->getStatus()->setNormalizeAsIRI(true);
}
foreach ($quote->getQuoteLines() as $quoteLine) {
$quoteLine->setNormalizeAsIRI(true);
}
foreach ($quote->getTasks() as $task) {
$task->setNormalizeAsIRI(true);
}
$quote->addTask((new QuoteTaskExternalRef())->setExternalValue($objPersisted->getId()));
$this->crudTaskHandler->submitQuoteForm(
$quote,
[AbstractWithoutRequestDataPersister::CONTEXT_NOT_POST_PERSIST_EVENT => true]
);
}
}
}