src/EventSubscriber/EasyAdminSubscriber.php line 246
<?phpnamespace App\EventSubscriber;use App\Entity\Order;use App\Entity\Parcel;use App\Entity\Product;use App\Entity\ReceivingOrder;use App\Entity\Scan\ScanOrderProduct;use App\Entity\Shipment;use App\Entity\ShopIntegrationPlatform\SipSettings;use App\Event\OrderCreatedEvent;use App\Event\OrderRemovedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityDeletedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityPersistedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;use App\Service\{InventorySingleProductService,Logger\CronLoggerService,OrderService,InventoryService,ReceivingOrderService,ScanOrderService,ShipmentService,TaskService,TranslatorService};use Doctrine\ORM\EntityManagerInterface;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeCrudActionEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;use Exception;use Symfony\Component\EventDispatcher\EventDispatcherInterface;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\RequestStack;use App\EventSubscriber\Traits\{OrderHandlerTrait,ParcelHandlerTrait,ShipmentHandlerTrait,ProductHandlerTrait,ScanOrderProductHandlerTrait,ShopIntegrationPlatform\SipSettingsProductHandlerTrait,ShopIntegrationPlatform\SipSettingsHandlerTrait,ReceivingOrderHandlerTrait};use App\Service\ShipmentLabelService;class EasyAdminSubscriber implements EventSubscriberInterface{use OrderHandlerTrait, ParcelHandlerTrait, ShipmentHandlerTrait,ProductHandlerTrait, ScanOrderProductHandlerTrait,SipSettingsHandlerTrait, SipSettingsProductHandlerTrait, ReceivingOrderHandlerTrait;/*** @param EntityManagerInterface $em* @param RequestStack $requestStack* @param TranslatorService $translatorService* @param OrderService $orderService* @param ShipmentService $shipmentService* @param InventoryService $inventoryService* @param EventDispatcherInterface $dispatcher* @param InventorySingleProductService $inventorySingleProductService* @param ShipmentLabelService $shipmentLabelService* @param TaskService $taskService* @param ReceivingOrderService $receivingOrderService* @param ScanOrderService $scanOrderService* @param CronLoggerService $cronLoggerService*/public function __construct(protected EntityManagerInterface $em,protected RequestStack $requestStack,protected TranslatorService $translatorService,protected OrderService $orderService,protected ShipmentService $shipmentService,protected InventoryService $inventoryService,protected EventDispatcherInterface $dispatcher,protected InventorySingleProductService $inventorySingleProductService,protected ShipmentLabelService $shipmentLabelService,protected TaskService $taskService,protected ReceivingOrderService $receivingOrderService,protected ScanOrderService $scanOrderService,protected CronLoggerService $cronLoggerService,){}/*** @return array*/public static function getSubscribedEvents(): array{return [BeforeCrudActionEvent::class => ['beforeCrudAction'],AfterEntityPersistedEvent::class => ['afterEntityPersistedAction'],BeforeEntityPersistedEvent::class => ['beforeEntityPersistedAction'],BeforeEntityUpdatedEvent::class => ['beforeEntityUpdatedAction'],AfterEntityDeletedEvent::class => ['afterEntityDeletedAction'],AfterEntityUpdatedEvent::class => ['afterEntityUpdatedAction'],];}/*** @throws Exception*/public function beforeCrudAction(BeforeCrudActionEvent $event){$context = $event->getAdminContext();$entity = $context->getEntity()->getInstance();if ($entity instanceof Order) {$this->handleBeforeOrderDelete($event, $entity);$this->handleBeforeParcelDelete($event, $this->getParcelFromOrder($entity));$this->handleOrder($event, $entity);$this->handleImpCourierApiCity($entity);return;}if ($entity instanceof Parcel) {$this->handleBeforeParcelDelete($event, $entity);return;}if ($entity instanceof ReceivingOrder) {$this->processReceivingOrderBeforeUpdate($event, $entity);$this->processReceivingOrderBeforeDelete($event, $entity);}}/*** @throws Exception*/public function afterEntityPersistedAction(AfterEntityPersistedEvent $event){$entity = $event->getEntityInstance();/** On create event */if ($entity instanceof Order && !$entity->getPreviousStatus()) {$event = new OrderCreatedEvent($entity);$this->dispatcher->dispatch($event);return;}if ($entity instanceof Product) {$this->handleProductPersisted($entity);}}/*** @param BeforeEntityPersistedEvent $event* @return void*/public function beforeEntityPersistedAction(BeforeEntityPersistedEvent $event): void{$entity = $event->getEntityInstance();if ($entity instanceof Order) {$this->handleImpCourierApiCity($entity);$this->changeOrderStatus($entity);return;}if ($entity instanceof Shipment) {$this->changeShipmentStatus($entity);}}/*** @param BeforeEntityUpdatedEvent $event* @return void* @throws Exception*/public function beforeEntityUpdatedAction(BeforeEntityUpdatedEvent $event): void{$entity = $event->getEntityInstance();if ($entity instanceof Order) {$this->logBeforeUpdatingDeliveredOrderEvent($entity);$this->processInventoryOfOrderAfterSaving();$this->changeOrderStatus($entity);return;}if ($entity instanceof Shipment) {$this->changeShipmentStatus($entity);return;}if ($entity instanceof Product) {$this->handleProductUpdating($entity);return;}if ($entity instanceof SipSettings) {$this->handleSipSettingsSaving($entity);}}/*** @param AfterEntityDeletedEvent $event* @return void* @throws Exception*/public function afterEntityDeletedAction(AfterEntityDeletedEvent $event): void{try {$entity = $event->getEntityInstance();if ($entity instanceof Order) {$this->inventoryService->cancelOrderProductsReservation($entity);$this->scanOrderService->removeOrderFromScanning($entity);$this->processDeletedOrder($entity);$this->dispatcher->dispatch(new OrderRemovedEvent($entity));return;}if ($entity instanceof Parcel) {$this->processDeletedParcel($entity);return;}if ($entity instanceof Product) {$this->removeSipSettingsProduct($entity);return;}if ($entity instanceof ReceivingOrder) {$this->processReceivingOrderAfterDelete($entity);return;}if ($entity instanceof Shipment) {$this->processDeletedShipment($entity);}/*** TODO: SIP - Add removing webhooks on removing connection!*/} catch (\Exception $ex) {$this->addFlash('danger', $ex->getMessage());}}/*** @param AfterEntityUpdatedEvent $event* @return void* @throws Exception*/public function afterEntityUpdatedAction(AfterEntityUpdatedEvent $event): void{try {$entity = $event->getEntityInstance();if ($entity instanceof Order) {$this->processOrderCourierAfterUpdating($entity);return;}if ($entity instanceof ScanOrderProduct) {$this->processScanOrderStatus($entity);return;}if ($entity instanceof ReceivingOrder) {$this->processReceivingOrderAfterUpdate($entity);}} catch (\Exception $ex) {$this->addFlash('danger', $ex->getMessage());}}/** ================================ Additional functions ================================ *//*** @param string $type* @param string $message* @return void*/private function addFlash(string $type, string $message): void{if (!$this->requestStack->getSession()) {return;}$translated = $this->translatorService->trans($message);if ($translated === $message) {$translated = $this->translatorService->trans($message, [], 'validators');}$this->requestStack->getSession()->getFlashBag()->add($type, $translated);}}