src/EventSubscriber/OrderAmountAndVtaSubscriber.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\OrderLine;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpKernel\Event\ViewEvent;
  8. use Symfony\Component\HttpKernel\KernelEvents;
  9. class OrderAmountAndVtaSubscriber 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.             $total 0;
  25.             $entity->getOrderLines()->map(function (OrderLine $orderLine) use (&$total) {
  26.                 $total += $orderLine->getAmount();
  27.             });
  28.             $entity
  29.                 ->setAmount((string) $total)
  30.                 ->setVat((string) ($total Order::VTA_PERCENT 100))
  31.             ;
  32.         }
  33.     }
  34. }