<?php
namespace App\V4\EventSubscriber\Quote;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\V4\Event\PostRemoveEvent;
use App\V4\EventSubscriber\AbstractSubscriber;
use App\V4\Handler\Task\CrudTaskHandler;
use App\V4\Model\Quote\Quote;
use App\V4\Model\QuoteState\QuoteState;
use App\V4\Model\Task\Task;
use ReflectionException;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
class QuotePostRemoveEventSubscriber extends AbstractSubscriber implements EventSubscriberInterface
{
/**
* @var CrudTaskHandler
*/
private $crudTaskHandler;
/**
* @param CrudTaskHandler $crudTaskHandler
*/
public function __construct(
CrudTaskHandler $crudTaskHandler
) {
$this->crudTaskHandler = $crudTaskHandler;
}
/**
* @param $entity
*
* @return bool
*/
public function supports($entity): bool
{
if ($entity instanceof Quote && $entity->getStatus() instanceof QuoteState) {
return !$this->isIdAlreadyManaged($entity->getId(), get_class($this));
}
return false;
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
PostRemoveEvent::NAME => 'onPostRemove',
];
}
/**
* @param PostRemoveEvent $event
*
* @throws RedirectionExceptionInterface
* @throws DecodingExceptionInterface
* @throws ClientExceptionInterface
* @throws TransportExceptionInterface
* @throws ServerExceptionInterface
* @throws ReflectionException
* @throws ResourceClassNotSupportedException
*/
public function onPostRemove(PostRemoveEvent $event): void
{
/** @var Quote $objRemoved */
$objRemoved = $event->getAfter();
if (!$objRemoved instanceof Quote) {
return;
}
$this->closeTasksConcernedByQuote($objRemoved);
}
/**
* @param Quote $objPersisted
*
* @return void
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ReflectionException
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
private function closeTasksConcernedByQuote(Quote $objPersisted)
{
$this->crudTaskHandler->closeTasksConcernedByQuote($objPersisted, Task::MEMO_LINE_TASK_WHEN_QUOTE_REMOVED);
}
}