src/EventSubscriber/OrderAddOrderNoteSubscriber.php line 24

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\EventListener\EventPriorities;
  4. use App\Entity\Order\Order;
  5. use App\Entity\Order\OrderNote;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class OrderAddOrderNoteSubscriber implements EventSubscriberInterface
  10. {
  11.     public static function getSubscribedEvents(): array
  12.     {
  13.         return [
  14.             KernelEvents::VIEW => ['updateOrder'EventPriorities::PRE_WRITE],
  15.         ];
  16.     }
  17.     /**
  18.      * @return void
  19.      */
  20.     public function updateOrder(ViewEvent $args)
  21.     {
  22.         $entity $args->getControllerResult();
  23.         if ($entity instanceof Order) {
  24.             if ($entity->getOrderState()) {
  25.                 $orderNote = new OrderNote();
  26.                 $orderNote->setOrderState($entity->getOrderState());
  27.                 $entity->addOrderNote($orderNote);
  28.             }
  29.         }
  30.     }
  31. }