src/EventSubscriber/NewAccountSubscriber.php line 39

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use ApiPlatform\Symfony\EventListener\EventPriorities;
  4. use App\Entity\Employee;
  5. use App\Entity\User;
  6. use App\Service\Message;
  7. use JetBrains\PhpStorm\ArrayShape;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpKernel\Event\ViewEvent;
  11. use Symfony\Component\HttpKernel\KernelEvents;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use function json_decode;
  14. use function str_contains;
  15. final class NewAccountSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var string The relative path inside templates/messages without any extensions */
  18.     private const TEMPLATE 'user/new-account';
  19.     public function __construct(
  20.         private readonly TokenStorageInterface $tokenStorage,
  21.         private readonly Message $messageService,
  22.     )
  23.     {
  24.     }
  25.     #[ArrayShape([KernelEvents::VIEW => "array"])]
  26.     public static function getSubscribedEvents(): array
  27.     {
  28.         return [
  29.             KernelEvents::VIEW => ['sendMessage'EventPriorities::POST_WRITE],
  30.         ];
  31.     }
  32.     public function sendMessage(ViewEvent $event): void
  33.     {
  34.         $entity $event->getControllerResult();
  35.         $method $event->getRequest()->getMethod();
  36.         # POST methods are used to create instances, and we are only interested in new users
  37.         if ((!$entity instanceof User && !$entity instanceof Employee) || $method !== Request::METHOD_POST || str_contains($event->getRequest()->getPathInfo(), 'resetPassword')) {
  38.             return;
  39.         }
  40.         $requestBody json_decode($event->getRequest()->getContent(), true);
  41.         $email null;
  42.         $password null;
  43.         if ($entity instanceof User) {
  44.             $email $entity->getUsername();
  45.             $password $requestBody['password'];
  46.         }
  47.         if ($entity instanceof Employee) {
  48.             $email $entity->getUser()->getUsername();
  49.             $password $requestBody['user']['password'];
  50.         }
  51.         if (empty($email) || empty($password)) {
  52.             return;
  53.         }
  54.         $message $this->messageService
  55.             ->withRecipient($entity)
  56.             ->withSubject('You have a new account')
  57.             ->withTemplate(self::TEMPLATE)
  58.             ->withTemplateData([
  59.                 'email' => $email,
  60.                 'password' => $password,
  61.                 'loginUrl' => $event->getRequest()->server->get('HTTP_ORIGIN') . '/admin/#/login',
  62.             ])
  63.             ->withSensitiveData([$password]);
  64.         $currentUser $this->tokenStorage->getToken()->getUser();
  65.         if ($currentUser instanceof User) {
  66.             $message $message->withCurrentUser($currentUser);
  67.         }
  68.         $message->send();
  69.     }
  70. }