src/EventSubscriber/EntitySubscriber.php line 119

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use DateTime;
  4. use App\Entity\Staff;
  5. use App\Entity\Business;
  6. use App\Entity\Customer;
  7. use App\Repository\CustomerRepository;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use Doctrine\Persistence\ManagerRegistry;
  10. use Symfony\Component\Security\Core\Security;
  11. use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
  12. use Symfony\Component\HttpFoundation\RedirectResponse;
  13. use EasyCorp\Bundle\EasyAdminBundle\Router\AdminUrlGenerator;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use EasyCorp\Bundle\EasyAdminBundle\Event\AfterEntityUpdatedEvent;
  16. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityUpdatedEvent;
  17. use EasyCorp\Bundle\EasyAdminBundle\Event\BeforeEntityPersistedEvent;
  18. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  19. class EntitySubscriber implements EventSubscriberInterface
  20. {
  21.     private $encoder;
  22.     private $customer_repository;
  23.     private $user;
  24.     private $url;
  25.     public function __construct(UserPasswordHasherInterface $encoderManagerRegistry $registrySecurity $securityAdminUrlGenerator $adminUrlGenerator)
  26.     {
  27.         $this->encoder $encoder;
  28.         $this->customer_repository = new CustomerRepository($registry);
  29.         $this->user $security->getUser();
  30.         $this->url $adminUrlGenerator;
  31.     }
  32.     public function hashPassword($event)
  33.     {
  34.         $entity $event->getEntityInstance();
  35.         if (property_exists($entity"password")) {
  36.             $password $this->encoder->hashPassword($entity$entity->getPassword());
  37.             $entity->setPassword($password);
  38.         }
  39.     }
  40.     public function updatePassword($event)
  41.     {
  42.         $entity $event->getEntityInstance();
  43.         if (property_exists($entity"businessUser")) {
  44.             $user $entity->getBusinessUser();
  45.             if ($user->newPassword) {
  46.                 $password $this->encoder->hashPassword($user$user->newPassword);
  47.                 $user->setPassword($password);
  48.             }
  49.         } else if (property_exists($entity"newPassword")) {
  50.             if ($entity->newPassword) {
  51.                 $password $this->encoder->hashPassword($entity$entity->newPassword);
  52.                 $entity->setPassword($password);
  53.             }
  54.         }
  55.     }
  56.     public function setCreatedAt($event)
  57.     {
  58.         $entity $event->getEntityInstance();
  59.         if (property_exists($entity"date")) {
  60.             $now = new DateTime("now");
  61.             $entity->setDate($now);
  62.         }
  63.     }
  64.     public function setCustomerNumber($event)
  65.     {
  66.         $entity $event->getEntityInstance();
  67.         if (!$entity instanceof Customer) {
  68.             return;
  69.         }
  70.         $customer_number '';
  71.         for ($i 0$i 12$i++) {
  72.             $customer_number .= mt_rand(09);
  73.         }
  74.         $customer_number implode('-'str_split($customer_number3));
  75.         $customer $this->customer_repository->findOneByCustomerNumber($customer_number);
  76.         if ($customer) {
  77.             return $this->setCustomerNumber($event);
  78.         } else {
  79.             $entity->setCustomerNumber($customer_number);
  80.         }
  81.     }
  82.     public function setStaffBusiness($event)
  83.     {
  84.         $entity $event->getEntityInstance();
  85.         if (!$entity instanceof Staff) {
  86.             return;
  87.         }
  88.         if (in_array("ROLE_BUSINESS"$this->user->getRoles())) {
  89.             $business $this->user->getBusiness();
  90.             $entity->setBusiness($business);
  91.         }
  92.     }
  93.     public function onPersistedEvent($event)
  94.     {
  95.         $this->checkBusinessUserStep($event);
  96.         $this->setCustomerNumber($event);
  97.         $this->setCreatedAt($event);
  98.         $this->hashPassword($event);
  99.         $this->setStaffBusiness($event);
  100.     }
  101.     public function onUpdatedEvent($event)
  102.     {
  103.         $this->setCreatedAt($event);
  104.         $this->updatePassword($event);
  105.     }
  106.     public function afterUpdatedEvent($event)
  107.     {
  108.         $this->checkBusinessUserStep($event);
  109.     }
  110.     public function checkBusinessUserStep($event)
  111.     {
  112.         $entity $event->getEntityInstance();
  113.         if (!$entity instanceof Business)
  114.             return;
  115.         if (!$entity->getBusinessUser()->getId()) {
  116.             $url $this->url
  117.                 ->setController(BusinessUserCrudController::class)
  118.                 ->setAction(Action::NEW)
  119.                 ->set('id_business'$entity->getId())
  120.                 ->generateUrl();
  121.             $event->setController(function () {
  122.                 return new RedirectResponse($url);
  123.             });
  124.         }
  125.     }
  126.     public static function getSubscribedEvents()
  127.     {
  128.         return [
  129.             BeforeEntityUpdatedEvent::class => 'onUpdatedEvent',
  130.             BeforeEntityPersistedEvent::class => 'onPersistedEvent',
  131.             AfterEntityUpdatedEvent::class => 'afterUpdatedEvent'
  132.         ];
  133.     }
  134. }