src/ApiBundle/Event/Listener/QuoteCreateListener.php line 45

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\ApiBundle\Event\Listener;
  12. use ApiPlatform\Symfony\EventListener\EventPriorities;
  13. use SolidInvoice\QuoteBundle\Entity\Quote;
  14. use SolidInvoice\QuoteBundle\Model\Graph;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Workflow\StateMachine;
  20. /**
  21.  * @see \SolidInvoice\ApiBundle\Tests\Event\Listener\QuoteCreateListenerTest
  22.  */
  23. class QuoteCreateListener implements EventSubscriberInterface
  24. {
  25.     public function __construct(
  26.         private readonly StateMachine $quoteStateMachine
  27.     ) {
  28.     }
  29.     /**
  30.      * @return array<string, list<list<string|int>>>
  31.      */
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             KernelEvents::VIEW => [['setQuoteStatus'EventPriorities::PRE_WRITE]],
  36.         ];
  37.     }
  38.     public function setQuoteStatus(ViewEvent $event): void
  39.     {
  40.         $quote $event->getControllerResult();
  41.         $method $event->getRequest()->getMethod();
  42.         if (! $quote instanceof Quote || Request::METHOD_POST !== $method || ! $event->isMainRequest() || $quote->getStatus()) {
  43.             return;
  44.         }
  45.         $this->quoteStateMachine->apply($quoteGraph::TRANSITION_NEW);
  46.     }
  47. }