src/EventSubscriber/Procedure/ProcedureDispatchSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay, Symfony package.
  4.  *
  5.  * (c) Mohamed Radhi GUENNICHI <rg@mate.tn> <+216 50 711 816>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace App\EventSubscriber\Procedure;
  12. use App\Entity\ProcedureStep;
  13. use App\Event\EmptyInvoiceContactEvent;
  14. use App\Event\ProcedureDispatchEvent;
  15. use App\Events;
  16. use App\Manager\ProcedureManager;
  17. use App\Manager\ProcedureStepManager;
  18. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class ProcedureDispatchSubscriber implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var ProcedureStepManager
  24.      */
  25.     protected $procedureStepManager;
  26.     /**
  27.      * @var EventDispatcherInterface
  28.      */
  29.     protected $eventDispatcher;
  30.     /**
  31.      * @var ProcedureManager
  32.      */
  33.     protected $procedureManager;
  34.     /**
  35.      * ProcedureDispatchSubscriber constructor.
  36.      *
  37.      * @param ProcedureStepManager $procedureStepManager
  38.      * @param EventDispatcherInterface $eventDispatcher
  39.      * @param ProcedureManager $procedureManager
  40.      */
  41.     public function __construct(ProcedureStepManager $procedureStepManagerEventDispatcherInterface $eventDispatcherProcedureManager $procedureManager)
  42.     {
  43.         $this->procedureStepManager $procedureStepManager;
  44.         $this->eventDispatcher $eventDispatcher;
  45.         $this->procedureManager $procedureManager;
  46.     }
  47.     /**
  48.      * @param ProcedureDispatchEvent $event
  49.      */
  50.     public function onProcedureDispatch(ProcedureDispatchEvent $event): void
  51.     {
  52.         /** @var ProcedureStep $procedureStep */
  53.         $procedureStep $event->getPendingStep();
  54.         if (null === $procedureStep || $procedureStep->getStatus() !== ProcedureStep::STATUS_STEP_PENDING) {
  55.             return;
  56.         }
  57.         $this->procedureStepManager->send($procedureStep$event);
  58.     }
  59.     public function onProcedureBeforeDispatch(ProcedureDispatchEvent $event): void
  60.     {
  61.         $procedure $event->getProcedure();
  62.         $invoice $procedure->getInvoice();
  63.         if (null !== $category $procedure->getWorkflow()->getCategory()) {
  64.             if ($category->getName() === 'category.no_treat_title') {
  65.                 return;
  66.             }
  67.         }
  68.         // Check if the invoice customer contacts is empty
  69.         if (!$invoice->containsAtLeastOneCustomerContact()) {
  70.             // Dispatch empty invoice contact event (send notification email)
  71.             $this->eventDispatcher->dispatch(new EmptyInvoiceContactEvent($invoice));
  72.             // We need to pause procedure here.
  73.             $this->procedureManager->pause($procedure1);
  74.             $event->setAllowed(false);
  75.         }
  76.     }
  77.     /**
  78.      * @inheritDoc
  79.      */
  80.     public static function getSubscribedEvents()
  81.     {
  82.         return [
  83.             Events::PROCEDURE_ON_DISPATCH => 'onProcedureDispatch',
  84.             Events::PROCEDURE_BEFORE_DISPATCH => 'onProcedureBeforeDispatch'
  85.         ];
  86.     }
  87. }