<?php
namespace App\V4\EventSubscriber\Quote;
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
use App\V4\Event\PostPersistEvent;
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\Component\Serializer\Exception\ExceptionInterface;
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 QuoteManageTasksSubscriber extends AbstractSubscriber implements EventSubscriberInterface
{
/**
* @var CrudTaskHandler
*/
private $crudTaskHandler;
/**
* @param CrudTaskHandler $crudTaskHandler
*/
public function __construct(
CrudTaskHandler $crudTaskHandler
) {
$this->crudTaskHandler = $crudTaskHandler;
}
/**
* @param $objPersisted
* @param $objBefore
*
* @return bool
*/
public function supports($objPersisted, $objBefore): bool
{
if (!$objPersisted instanceof Quote) {
// On ne gère que les devis
return false;
}
if (!$objPersisted->getStatus() instanceof QuoteState) {
// On ne gère que les devis avec un statut
return false;
}
if (!$objBefore instanceof Quote || !$objBefore->getStatus() instanceof QuoteState) {
// On déclenche s'il s'agit d'une création de devis
// On déclenche si l'ancien devis n'a pas de statut (ce qui devrait arriver de moins en moins)
return true;
}
// On déclenche si le status du devis à changer
return $objPersisted->getStatus()->getId() !== $objBefore->getStatus()->getId();
}
/**
* @return string[]
*/
public static function getSubscribedEvents(): array
{
return [
PostPersistEvent::NAME => 'onPostPersist',
];
}
/**
* @param PostPersistEvent $event
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws ExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ReflectionException
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
public function onPostPersist(PostPersistEvent $event): void
{
/** @var Quote $objPersisted */
$objPersisted = $event->getAfter();
$objBefore = $event->getBefore();
if (!$this->supports($objPersisted, $objBefore) || $this->isIdAlreadyManaged($objPersisted->getId(), get_class($this))) {
return;
}
$this->closeTasksConcernedByQuote($objPersisted);
if (true !== $objPersisted->getStatus()->getIsDiffered()) {
return;
}
$this->crudTaskHandler->createReminderTaskAuto($objPersisted);
}
/**
* @param Quote $objPersisted
*
* @return void
*
* @throws ClientExceptionInterface
* @throws DecodingExceptionInterface
* @throws RedirectionExceptionInterface
* @throws ReflectionException
* @throws ResourceClassNotSupportedException
* @throws ServerExceptionInterface
* @throws TransportExceptionInterface
*/
private function closeTasksConcernedByQuote(Quote $objPersisted)
{
if ($objPersisted->getStatus()->getInProgress()) {
return;
}
$this->crudTaskHandler->closeTasksConcernedByQuote($objPersisted, Task::MEMO_LINE_TASK_WHEN_QUOTE_STATE_CHANGED);
}
}