<?php
namespace App\EventSubscriber;
use ApiPlatform\Core\Api\IriConverterInterface;
use ApiPlatform\Core\EventListener\EventPriorities;
use App\Dto\MailInfoDto;
use App\Entity\Customer;
use App\Entity\User;
use App\Service\EmailService;
use App\Service\NotificationService;
use Doctrine\Persistence\ManagerRegistry;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
use Vich\UploaderBundle\Storage\StorageInterface;
class CustomerSubscriber implements EventSubscriberInterface
{
private $managerRegistry;
private $passwordEncoder;
private $notificationService;
private $logger;
private $emailService;
public function __construct(
ManagerRegistry $managerRegistry,
UserPasswordEncoderInterface $passwordEncoder,
NotificationService $notificationService,
LoggerInterface $logger,
EmailService $emailService
) {
$this->managerRegistry = $managerRegistry;
$this->passwordEncoder = $passwordEncoder;
$this->notificationService = $notificationService;
$this->logger = $logger;
$this->emailService = $emailService;
}
public static function getSubscribedEvents()
{
return [
KernelEvents::VIEW => [
['processCustomerAdd', EventPriorities::PRE_WRITE],
],
];
}
/**
* @throws \Exception
*/
public function processCustomerAdd(ViewEvent $event)
{
$customer = $event->getControllerResult();
$method = $event->getRequest()->getMethod();
$link = $event->getRequest()->getPathInfo();
$this->logger->info($link);
if (!$customer instanceof Customer || Request::METHOD_POST !== $method || str_contains($link, "customers/login") ||
str_contains($link, "customers/sendcode") || str_contains($link, "customers/verifyotp") || str_contains($link, "customers/change-password")) {
return;
}
if($customer->getEnterpriseName()){
return;
}
$this->logger->info("CRYPTAGE DU MOT DE PASSE");
$defaultPassword = $customer->getPassword();
$customer->setPassword(password_hash($customer->getPassword(), PASSWORD_BCRYPT));
if (!is_null($customer->getPlayerId())) try {
$this->notificationService->sendNotification($customer->getPlayerId(), "Bienvenu sur l'application hoges.");
} catch (\Exception $e) {
$this->logger->critical($e->getMessage());
}
if (!is_null($customer->getEmail())) {
$this->emailService->sendWelcommeMail($customer, $defaultPassword);
}
}
}