src/InvoiceBundle/Listener/WorkFlowSubscriber.php line 46

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\InvoiceBundle\Listener;
  12. use Carbon\Carbon;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. use SolidInvoice\InvoiceBundle\Entity\Invoice;
  15. use SolidInvoice\InvoiceBundle\Entity\RecurringInvoice;
  16. use SolidInvoice\InvoiceBundle\Model\Graph;
  17. use SolidInvoice\InvoiceBundle\Notification\InvoiceStatusNotification;
  18. use SolidInvoice\NotificationBundle\Notification\NotificationManager;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. use Symfony\Component\Workflow\Event\Event;
  21. use Symfony\Component\Workflow\Transition;
  22. /**
  23.  * @see \SolidInvoice\InvoiceBundle\Tests\Listener\WorkFlowSubscriberTest
  24.  */
  25. class WorkFlowSubscriber implements EventSubscriberInterface
  26. {
  27.     public function __construct(
  28.         private readonly ManagerRegistry $registry,
  29.         private readonly NotificationManager $notification
  30.     ) {
  31.     }
  32.     public static function getSubscribedEvents()
  33.     {
  34.         return [
  35.             'workflow.invoice.entered' => 'onWorkflowTransitionApplied',
  36.             'workflow.recurring_invoice.enter' => 'onWorkflowTransitionApplied',
  37.         ];
  38.     }
  39.     public function onWorkflowTransitionApplied(Event $event): void
  40.     {
  41.         /** @var Invoice|RecurringInvoice $invoice */
  42.         $invoice $event->getSubject();
  43.         if (($transition $event->getTransition()) instanceof Transition) {
  44.             if (Graph::TRANSITION_PAY === $transition->getName()) {
  45.                 $invoice->setPaidDate(Carbon::now());
  46.             }
  47.             if (Graph::TRANSITION_ARCHIVE === $transition->getName()) {
  48.                 $invoice->archive();
  49.             }
  50.         }
  51.         $em $this->registry->getManager();
  52.         $em->persist($invoice);
  53.         $em->flush();
  54.         if (Graph::STATUS_NEW !== $invoice->getStatus()) {
  55.             $this->notification->sendNotification('invoice_status_update', new InvoiceStatusNotification(['invoice' => $invoice]));
  56.         }
  57.     }
  58. }