<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Entity\Order\Order;
use App\Entity\Order\OrderNote;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class OrderAddOrderNoteSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents(): array
{
return [
KernelEvents::VIEW => ['updateOrder', EventPriorities::PRE_WRITE],
];
}
/**
* @return void
*/
public function updateOrder(ViewEvent $args)
{
$entity = $args->getControllerResult();
if ($entity instanceof Order) {
if ($entity->getOrderState()) {
$orderNote = new OrderNote();
$orderNote->setOrderState($entity->getOrderState());
$entity->addOrderNote($orderNote);
}
}
}
}