src/EventSubscriber/CustomerSubscriber.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Core\Api\IriConverterInterface;
  4. use ApiPlatform\Core\EventListener\EventPriorities;
  5. use App\Dto\MailInfoDto;
  6. use App\Entity\Customer;
  7. use App\Entity\User;
  8. use App\Service\EmailService;
  9. use App\Service\NotificationService;
  10. use Doctrine\Persistence\ManagerRegistry;
  11. use Psr\Log\LoggerInterface;
  12. use Symfony\Component\DependencyInjection\ContainerBuilder;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\Filesystem\Filesystem;
  16. use Symfony\Component\HttpFoundation\Request;
  17. use Symfony\Component\HttpKernel\Event\ViewEvent;
  18. use Symfony\Component\HttpKernel\KernelEvents;
  19. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  20. use Vich\UploaderBundle\Storage\StorageInterface;
  21. class CustomerSubscriber implements EventSubscriberInterface
  22. {
  23.     private $managerRegistry;
  24.     private $passwordEncoder;
  25.     private $notificationService;
  26.     private $logger;
  27.     private $emailService;
  28.     public function __construct(
  29.         ManagerRegistry $managerRegistry,
  30.         UserPasswordEncoderInterface $passwordEncoder,
  31.         NotificationService $notificationService,
  32.         LoggerInterface $logger,
  33.         EmailService $emailService
  34.     ) {
  35.         $this->managerRegistry $managerRegistry;
  36.         $this->passwordEncoder $passwordEncoder;
  37.         $this->notificationService $notificationService;
  38.         $this->logger $logger;
  39.         $this->emailService $emailService;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             KernelEvents::VIEW => [
  45.                 ['processCustomerAdd'EventPriorities::PRE_WRITE],
  46.             ],
  47.         ];
  48.     }
  49.     /**
  50.      * @throws \Exception
  51.      */
  52.     public function processCustomerAdd(ViewEvent $event)
  53.     {
  54.         $customer $event->getControllerResult();
  55.         $method $event->getRequest()->getMethod();
  56.         $link $event->getRequest()->getPathInfo();
  57.         $this->logger->info($link);
  58.         if (!$customer instanceof Customer || Request::METHOD_POST !== $method || str_contains($link"customers/login") ||
  59.             str_contains($link"customers/sendcode") || str_contains($link"customers/verifyotp") || str_contains($link"customers/change-password")) {
  60.             return;
  61.         }
  62.         if($customer->getEnterpriseName()){
  63.             return;
  64.         }
  65.         $this->logger->info("CRYPTAGE DU MOT DE PASSE");
  66.         $defaultPassword $customer->getPassword();
  67.         $customer->setPassword(password_hash($customer->getPassword(), PASSWORD_BCRYPT));
  68.         if (!is_null($customer->getPlayerId())) try {
  69.             $this->notificationService->sendNotification($customer->getPlayerId(), "Bienvenu sur l'application hoges.");
  70.         } catch (\Exception $e) {
  71.             $this->logger->critical($e->getMessage());
  72.         }
  73.         if (!is_null($customer->getEmail())) {
  74.             $this->emailService->sendWelcommeMail($customer$defaultPassword);
  75.         }
  76.     }
  77. }