src/QuoteBundle/Listener/WorkFlowSubscriber.php line 68

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of SolidInvoice project.
  5.  *
  6.  * (c) Pierre du Plessis <open-source@solidworx.co>
  7.  *
  8.  * This source file is subject to the MIT license that is bundled
  9.  * with this source code in the file LICENSE.
  10.  */
  11. namespace SolidInvoice\QuoteBundle\Listener;
  12. use Doctrine\Persistence\ManagerRegistry;
  13. use JsonException;
  14. use SolidInvoice\InvoiceBundle\Manager\InvoiceManager;
  15. use SolidInvoice\InvoiceBundle\Model\Graph as InvoiceGraph;
  16. use SolidInvoice\NotificationBundle\Notification\NotificationManager;
  17. use SolidInvoice\QuoteBundle\Entity\Quote;
  18. use SolidInvoice\QuoteBundle\Exception\InvalidTransitionException;
  19. use SolidInvoice\QuoteBundle\Mailer\QuoteMailer;
  20. use SolidInvoice\QuoteBundle\Model\Graph as QuoteGraph;
  21. use SolidInvoice\QuoteBundle\Notification\QuoteStatusNotification;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  24. use Symfony\Component\Workflow\Event\Event;
  25. use Symfony\Component\Workflow\StateMachine;
  26. /**
  27.  * @see \SolidInvoice\QuoteBundle\Tests\Listener\WorkFlowSubscriberTest
  28.  */
  29. final class WorkFlowSubscriber implements EventSubscriberInterface
  30. {
  31.     public function __construct(
  32.         private readonly ManagerRegistry $registry,
  33.         private readonly InvoiceManager $invoiceManager,
  34.         private readonly StateMachine $invoiceStateMachine,
  35.         private readonly NotificationManager $notification,
  36.         private readonly QuoteMailer $quoteMailer
  37.     ) {
  38.     }
  39.     /**
  40.      * @return array<string, string>
  41.      */
  42.     public static function getSubscribedEvents(): array
  43.     {
  44.         return [
  45.             'workflow.quote.entered.accepted' => 'onQuoteAccepted',
  46.             'workflow.quote.entered' => 'onWorkflowTransitionApplied',
  47.         ];
  48.     }
  49.     public function onQuoteAccepted(Event $event): void
  50.     {
  51.         $quote $event->getSubject();
  52.         assert($quote instanceof Quote);
  53.         $invoice $this->invoiceManager->createFromQuote($quote);
  54.         $this->invoiceStateMachine->apply($invoiceInvoiceGraph::TRANSITION_NEW);
  55.     }
  56.     /**
  57.      * @throws JsonException|InvalidTransitionException|TransportExceptionInterface
  58.      */
  59.     public function onWorkflowTransitionApplied(Event $event): void
  60.     {
  61.         /** @var Quote $quote */
  62.         $quote $event->getSubject();
  63.         if (null !== ($transition $event->getTransition()) && QuoteGraph::TRANSITION_ARCHIVE === $transition->getName()) {
  64.             $quote->archive();
  65.         }
  66.         $em $this->registry->getManager();
  67.         $em->persist($quote);
  68.         $em->flush();
  69.         if (null !== ($transition $event->getTransition()) && QuoteGraph::TRANSITION_SEND === $transition->getName()) {
  70.             $this->quoteMailer->send($quote);
  71.         }
  72.         if (QuoteGraph::STATUS_NEW !== $quote->getStatus()) {
  73.             $this->notification->sendNotification('quote_status_update', new QuoteStatusNotification(['quote' => $quote]));
  74.         }
  75.     }
  76. }