src/EventSubscriber/CustomerWorkflowChangedSubscriber.php line 57

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the CosaVostra, TrackPay 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;
  12. use App\Entity\Organization;
  13. use App\Entity\Procedure;
  14. use App\Event\CustomerWorkflowChangedEvent;
  15. use App\Manager\ProcedureManager;
  16. use App\Repository\ProcedureRepository;
  17. use Doctrine\ORM\EntityManagerInterface;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. class CustomerWorkflowChangedSubscriber implements EventSubscriberInterface
  20. {
  21.     /**
  22.      * @var ProcedureManager
  23.      */
  24.     protected $procedureManager;
  25.     /**
  26.      * @var ProcedureRepository
  27.      */
  28.     protected $procedureRepository;
  29.     /**
  30.      * @var EntityManagerInterface
  31.      */
  32.     protected $entityManager;
  33.     public function __construct(ProcedureManager $procedureManagerProcedureRepository $procedureRepositoryEntityManagerInterface $entityManager)
  34.     {
  35.         $this->procedureRepository $procedureRepository;
  36.         $this->entityManager       $entityManager;
  37.         $this->procedureManager    $procedureManager;
  38.     }
  39.     /**
  40.      * @inheritDoc
  41.      */
  42.     public static function getSubscribedEvents()
  43.     {
  44.         return [
  45.             CustomerWorkflowChangedEvent::class => 'onWorkflowChanged'
  46.         ];
  47.     }
  48.     public function onWorkflowChanged(CustomerWorkflowChangedEvent $event): void
  49.     {
  50.         // Get procedures with pending/paused status
  51.         $customer   $event->getCustomer();
  52.         $procedures $this->getNotActivatedProceduresFor($customer);
  53.         // Remove the pendingStep and dispatchedStep objects
  54.         foreach ($procedures as $procedure) {
  55.             $procedure->setWorkflow(
  56.                 $customer->getWorkflow()
  57.             );
  58.             $procedure->setDispatchedStep(null);
  59.             // Remove the dispatchedStep object, we don't need it anymore here.
  60.             if (null !== $procedure->getPendingStep()) {
  61.                 $this->entityManager->remove($procedure->getPendingStep());
  62.                 $procedure->setPendingStep(null);
  63.             }
  64.             // Flush changes.
  65.             $this->entityManager->flush();
  66.             // Process the new workflow
  67.             $this->procedureManager->proceed($procedure);
  68.         }
  69.     }
  70.     /**
  71.      * @param Organization $customer
  72.      *
  73.      * @return Procedure[]
  74.      */
  75.     protected function getNotActivatedProceduresFor(Organization $customer): array
  76.     {
  77.         $queryBuilder $this->procedureRepository->createQueryBuilder('o');
  78.         return $queryBuilder
  79.             ->innerJoin('o.invoice''invoice')
  80.             ->andWhere(
  81.                 $queryBuilder->expr()->eq('invoice.customer'':customer')
  82.             )
  83.             ->andWhere(
  84.                 $queryBuilder->expr()->in('o.status'':status')
  85.             )
  86.             ->andWhere(
  87.                 $queryBuilder->expr()->isNull('o.activatedAt')
  88.             )
  89.             ->setParameters([
  90.                 'customer' => $customer,
  91.                 'status'   => [Procedure::STATUS_PENDINGProcedure::STATUS_PAUSED]
  92.             ])
  93.             ->getQuery()
  94.             ->getResult();
  95.     }
  96. }