vendor/bluue/shipments-bundle/src/EventSubscriber/SalesBundle/OrderSubscriber.php line 67

Open in your IDE?
  1. <?php
  2. /**
  3.  * @author Léo BANNHOLTZER (contact@scaledev.fr)
  4.  * @copyright 2021 - ScaleDEV SAS, 12 RUE CHARLES MORET, 10120 ST ANDRE LES VERGERS
  5.  * @license commercial
  6.  */
  7. declare(strict_types=1);
  8. namespace Bluue\ShipmentsBundle\EventSubscriber\SalesBundle;
  9. use Bluue\ProductsBundle\Repository\DeclinationRepository;
  10. use Bluue\ProductsBundle\Repository\ProductRepository;
  11. use Doctrine\ORM\EntityManagerInterface;
  12. use Bluue\ShipmentsBundle\Entity\OrderOptions;
  13. use Bluue\SalesBundle\Event\OrderMovementsEvent;
  14. use Bluue\ShipmentsBundle\Repository\OrderOptionsRepository;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class OrderSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var EntityManagerInterface $em
  20.      */
  21.     private EntityManagerInterface $em;
  22.     /**
  23.      * @var OrderOptionsRepository $orderOptionsRepo
  24.      */
  25.     private OrderOptionsRepository $orderOptionsRepo;
  26.     /**
  27.      * @var DeclinationRepository $declinationRepo
  28.      */
  29.     private DeclinationRepository $declinationRepo;
  30.     /**
  31.      * @var ProductRepository $productRepo
  32.      */
  33.     private ProductRepository $productRepo;
  34.     public function __construct(
  35.         EntityManagerInterface $em,
  36.         OrderOptionsRepository $orderOptionsRepo,
  37.         DeclinationRepository $declinationRepo,
  38.         ProductRepository $productRepo
  39.     ) {
  40.         $this->em $em;
  41.         $this->orderOptionsRepo $orderOptionsRepo;
  42.         $this->declinationRepo $declinationRepo;
  43.         $this->productRepo $productRepo;
  44.     }
  45.     /**
  46.      * @return array
  47.      */
  48.     public static function getSubscribedEvents(): array
  49.     {
  50.         return [OrderMovementsEvent::VALIDATE_ORDER => 'validateOrder'];
  51.     }
  52.     /**
  53.      * @param OrderMovementsEvent $event
  54.      * @return void
  55.      */
  56.     public function validateOrder(OrderMovementsEvent $event): void
  57.     {
  58.         $order $event->getOrder();
  59.         $order_options $this->orderOptionsRepo->findOneBy(['order' => $order]);
  60.         if (!$order_options) {
  61.             $order_options = new OrderOptions();
  62.             $order_options->setOrder($order);
  63.         }
  64.         $order_weight 0;
  65.         foreach ($order->getFilterLines(truetrue) as $order_line) {
  66.             if ($order_line->getQuantity() > && $element_id $order_line->getElementId()) {
  67.                 if ($order_line->hasDeclinationLinked()) {
  68.                     $weight $this->declinationRepo->find($element_id)->getWeight();
  69.                 } else {
  70.                     $weight $this->productRepo->find($element_id)->getWeight();
  71.                 }
  72.                 $order_weight += $weight $order_line->getQuantity();
  73.             }
  74.         }
  75.         $order_options->setWeight((string) $order_weight);
  76.         $order_options->setStatus(0);
  77.         $this->em->persist($order_options);
  78.         $this->em->flush();
  79.     }
  80. }