src/EventSubscriber/EasyAdminSubscriber.php line 130

  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Order;
  4. use App\Entity\Parcel;
  5. use App\Entity\Product;
  6. use App\Entity\ReceivingOrder;
  7. use App\Entity\Scan\ScanOrderProduct;
  8. use App\Entity\Shipment;
  9. use App\Entity\ShopIntegrationPlatform\SipSettings;
  10. use App\Event\OrderCreatedEvent;
  11. use App\Event\OrderRemovedEvent;
  12. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;
  13. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;
  14. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  15. use App\Service\{InventorySingleProductService,
  16.     Logger\CronLoggerService,
  17.     OrderService,
  18.     InventoryService,
  19.     ReceivingOrderService,
  20.     ScanOrderService,
  21.     ShipmentService,
  22.     TaskService,
  23.     TranslatorService
  24. };
  25. use Doctrine\ORM\EntityManagerInterface;
  26. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;
  27. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  28. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  29. use Exception;
  30. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  31. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  32. use Symfony\Component\HttpFoundation\RequestStack;
  33. use App\EventSubscriber\Traits\{OrderHandlerTrait,
  34.     ParcelHandlerTrait,
  35.     ShipmentHandlerTrait,
  36.     ProductHandlerTrait,
  37.     ScanOrderProductHandlerTrait,
  38.     ShopIntegrationPlatform\SipSettingsProductHandlerTrait,
  39.     ShopIntegrationPlatform\SipSettingsHandlerTrait,
  40.     ReceivingOrderHandlerTrait
  41. };
  42. use App\Service\ShipmentLabelService;
  43. class EasyAdminSubscriber implements EventSubscriberInterface
  44. {
  45.     use OrderHandlerTraitParcelHandlerTraitShipmentHandlerTrait,
  46.         ProductHandlerTraitScanOrderProductHandlerTrait,
  47.         SipSettingsHandlerTraitSipSettingsProductHandlerTraitReceivingOrderHandlerTrait;
  48.     /**
  49.      * @param EntityManagerInterface $em
  50.      * @param RequestStack $requestStack
  51.      * @param TranslatorService $translatorService
  52.      * @param OrderService $orderService
  53.      * @param ShipmentService $shipmentService
  54.      * @param InventoryService $inventoryService
  55.      * @param EventDispatcherInterface $dispatcher
  56.      * @param InventorySingleProductService $inventorySingleProductService
  57.      * @param ShipmentLabelService $shipmentLabelService
  58.      * @param TaskService $taskService
  59.      * @param ReceivingOrderService $receivingOrderService
  60.      * @param ScanOrderService $scanOrderService
  61.      * @param CronLoggerService $cronLoggerService
  62.      */
  63.     public function __construct(
  64.         protected EntityManagerInterface        $em,
  65.         protected RequestStack                  $requestStack,
  66.         protected TranslatorService             $translatorService,
  67.         protected OrderService                  $orderService,
  68.         protected ShipmentService               $shipmentService,
  69.         protected InventoryService              $inventoryService,
  70.         protected EventDispatcherInterface      $dispatcher,
  71.         protected InventorySingleProductService $inventorySingleProductService,
  72.         protected ShipmentLabelService          $shipmentLabelService,
  73.         protected TaskService                   $taskService,
  74.         protected ReceivingOrderService         $receivingOrderService,
  75.         protected ScanOrderService              $scanOrderService,
  76.         protected CronLoggerService             $cronLoggerService,
  77.     )
  78.     {
  79.     }
  80.     /**
  81.      * @return array
  82.      */
  83.     public static function getSubscribedEvents(): array
  84.     {
  85.         return [
  86.             BeforeCrudActionEvent::class => ['beforeCrudAction'],
  87.             AfterEntityPersistedEvent::class => ['afterEntityPersistedAction'],
  88.             BeforeEntityPersistedEvent::class => ['beforeEntityPersistedAction'],
  89.             BeforeEntityUpdatedEvent::class => ['beforeEntityUpdatedAction'],
  90.             AfterEntityDeletedEvent::class => ['afterEntityDeletedAction'],
  91.             AfterEntityUpdatedEvent::class => ['afterEntityUpdatedAction'],
  92.         ];
  93.     }
  94.     /**
  95.      * @throws Exception
  96.      */
  97.     public function beforeCrudAction(BeforeCrudActionEvent $event)
  98.     {
  99.         $context $event->getAdminContext();
  100.         $entity $context->getEntity()->getInstance();
  101.         if ($entity instanceof Order) {
  102.             $this->handleBeforeOrderDelete($event$entity);
  103.             $this->handleBeforeParcelDelete($event$this->getParcelFromOrder($entity));
  104.             $this->handleOrder($event$entity);
  105.             $this->handleImpCourierApiCity($entity);
  106.             return;
  107.         }
  108.         if ($entity instanceof Parcel) {
  109.             $this->handleBeforeParcelDelete($event$entity);
  110.             return;
  111.         }
  112.         if ($entity instanceof ReceivingOrder) {
  113.             $this->processReceivingOrderBeforeUpdate($event$entity);
  114.             $this->processReceivingOrderBeforeDelete($event$entity);
  115.         }
  116.     }
  117.     /**
  118.      * @throws Exception
  119.      */
  120.     public function afterEntityPersistedAction(AfterEntityPersistedEvent $event)
  121.     {
  122.         $entity $event->getEntityInstance();
  123.         /** On create event */
  124.         if ($entity instanceof Order && !$entity->getPreviousStatus()) {
  125.             $event = new OrderCreatedEvent($entity);
  126.             $this->dispatcher->dispatch($event);
  127.             return;
  128.         }
  129.         if ($entity instanceof Product) {
  130.             $this->handleProductPersisted($entity);
  131.         }
  132.     }
  133.     /**
  134.      * @param BeforeEntityPersistedEvent $event
  135.      * @return void
  136.      */
  137.     public function beforeEntityPersistedAction(BeforeEntityPersistedEvent $event): void
  138.     {
  139.         $entity $event->getEntityInstance();
  140.         if ($entity instanceof Order) {
  141.             $this->handleImpCourierApiCity($entity);
  142.             $this->changeOrderStatus($entity);
  143.             return;
  144.         }
  145.         if ($entity instanceof Shipment) {
  146.             $this->changeShipmentStatus($entity);
  147.         }
  148.     }
  149.     /**
  150.      * @param BeforeEntityUpdatedEvent $event
  151.      * @return void
  152.      * @throws Exception
  153.      */
  154.     public function beforeEntityUpdatedAction(BeforeEntityUpdatedEvent $event): void
  155.     {
  156.         $entity $event->getEntityInstance();
  157.         if ($entity instanceof Order) {
  158.             $this->logBeforeUpdatingDeliveredOrderEvent($entity);
  159.             $this->processInventoryOfOrderAfterSaving();
  160.             $this->changeOrderStatus($entity);
  161.             return;
  162.         }
  163.         if ($entity instanceof Shipment) {
  164.             $this->changeShipmentStatus($entity);
  165.             return;
  166.         }
  167.         if ($entity instanceof Product) {
  168.             $this->handleProductUpdating($entity);
  169.             return;
  170.         }
  171.         if ($entity instanceof SipSettings) {
  172.             $this->handleSipSettingsSaving($entity);
  173.         }
  174.     }
  175.     /**
  176.      * @param AfterEntityDeletedEvent $event
  177.      * @return void
  178.      * @throws Exception
  179.      */
  180.     public function afterEntityDeletedAction(AfterEntityDeletedEvent $event): void
  181.     {
  182.         try {
  183.             $entity $event->getEntityInstance();
  184.             if ($entity instanceof Order) {
  185.                 $this->inventoryService->cancelOrderProductsReservation($entity);
  186.                 $this->scanOrderService->removeOrderFromScanning($entity);
  187.                 $this->processDeletedOrder($entity);
  188.                 $this->dispatcher->dispatch(new OrderRemovedEvent($entity));
  189.                 return;
  190.             }
  191.             if ($entity instanceof Parcel) {
  192.                 $this->processDeletedParcel($entity);
  193.                 return;
  194.             }
  195.             if ($entity instanceof Product) {
  196.                 $this->removeSipSettingsProduct($entity);
  197.                 return;
  198.             }
  199.             if ($entity instanceof ReceivingOrder) {
  200.                 $this->processReceivingOrderAfterDelete($entity);
  201.                 return;
  202.             }
  203.             if ($entity instanceof Shipment) {
  204.                 $this->processDeletedShipment($entity);
  205.             }
  206.             /**
  207.              * TODO: SIP - Add removing webhooks on removing connection!
  208.              */
  209.         } catch (\Exception $ex) {
  210.             $this->addFlash('danger'$ex->getMessage());
  211.         }
  212.     }
  213.     /**
  214.      * @param AfterEntityUpdatedEvent $event
  215.      * @return void
  216.      * @throws Exception
  217.      */
  218.     public function afterEntityUpdatedAction(AfterEntityUpdatedEvent $event): void
  219.     {
  220.         try {
  221.             $entity $event->getEntityInstance();
  222.             if ($entity instanceof Order) {
  223.                 $this->processOrderCourierAfterUpdating($entity);
  224.                 return;
  225.             }
  226.             if ($entity instanceof ScanOrderProduct) {
  227.                 $this->processScanOrderStatus($entity);
  228.                 return;
  229.             }
  230.             if ($entity instanceof ReceivingOrder) {
  231.                 $this->processReceivingOrderAfterUpdate($entity);
  232.             }
  233.         } catch (\Exception $ex) {
  234.             $this->addFlash('danger'$ex->getMessage());
  235.         }
  236.     }
  237.     /** ================================ Additional functions ================================ */
  238.     /**
  239.      * @param string $type
  240.      * @param string $message
  241.      * @return void
  242.      */
  243.     private function addFlash(string $typestring $message): void
  244.     {
  245.         if (!$this->requestStack->getSession()) {
  246.             return;
  247.         }
  248.         $translated $this->translatorService->trans($message);
  249.         if ($translated === $message) {
  250.             $translated $this->translatorService->trans($message, [], 'validators');
  251.         }
  252.         $this->requestStack->getSession()->getFlashBag()->add($type$translated);
  253.     }
  254. }