Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/EventSubscriber/EasyAdminEventSubscriber.php line 59

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use App\Entity\Address;
  4. use App\Entity\DeliveryNote;
  5. use App\Service\ConstantsService;
  6. use Doctrine\ORM\EntityManagerInterface;
  7. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  8. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class EasyAdminEventSubscriber implements EventSubscriberInterface
  11. {
  12.     private EntityManagerInterface $em;
  13.     /**
  14.      * @param EntityManagerInterface $em
  15.      */
  16.     public function __construct(EntityManagerInterface $em)
  17.     {
  18.         $this->em $em;
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             BeforeEntityPersistedEvent::class => array(
  24.                 ['setUserBillingAddress'],
  25.             ),
  26.             BeforeEntityUpdatedEvent::class => array(
  27.                 ['resetWharehouseStatus']
  28.             )
  29.         ];
  30.     }
  31.     public function setUserBillingAddress(BeforeEntityPersistedEvent $event): void
  32.     {
  33.         $entity $event->getEntityInstance();
  34.         if (!($entity instanceof Address)) {
  35.             return;
  36.         }
  37.         if(!$entity->getId()) {
  38.             $entity->setFirstname($entity->getUser()->getFirstname());
  39.             $entity->setLastname($entity->getUser()->getLastname());
  40.             if(!$entity->getUser()->getBillingAddress()) {
  41.                 $entity->getUser()->setBillingAddress($entity);
  42.             }
  43.             if(!$entity->getUser()->getDeliveryAddress()) {
  44.                 $entity->getUser()->setDeliveryAddress($entity);
  45.             }
  46.         }
  47.     }
  48.     public function resetWharehouseStatus(BeforeEntityUpdatedEvent $event): void
  49.     {
  50.         $entity $event->getEntityInstance();
  51.         if (!($entity instanceof DeliveryNote)) {
  52.             return;
  53.         }
  54.         $uow $this->em->getUnitOfWork();
  55.         $uow->computeChangeSets();
  56.         if($entity->getShipment()) {
  57.             $changes $uow->getEntityChangeSet($entity->getShipment());
  58.             if (isset($changes['carrier'])) {
  59.                 $entity->setWarehouseStatus(ConstantsService::WAREHOUSE_STATUS_NULL);
  60.                 $entity->setPreparationDate(null);
  61.                 $entity->setPreparationMailSent(false);
  62.             }
  63.         }
  64.     }
  65. }