src/Infrastructure/Mailing/Subscriber/CollaboratorSubscriber.php line 26

Open in your IDE?
  1. <?php
  2. namespace App\Infrastructure\Mailing\Subscriber;
  3. use App\Domain\Auth\Entity\Collaborator;
  4. use App\Domain\Auth\Event\UserCreatedEvent;
  5. use App\Infrastructure\Mailing\Mailer;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class CollaboratorSubscriber implements EventSubscriberInterface
  8. {
  9.     private Mailer $mailer;
  10.     public function __construct(Mailer $mailer)
  11.     {
  12.         $this->mailer $mailer;
  13.     }
  14.     public static function getSubscribedEvents(): array
  15.     {
  16.         return [
  17.             UserCreatedEvent::class => 'onCollaboratorCreated'
  18.         ];
  19.     }
  20.     public function onCollaboratorCreated(UserCreatedEvent $event): void
  21.     {
  22.         $user $event->getUser();
  23.         if($user instanceof Collaborator) {
  24.             $email $this->mailer->createEmail('mails/auth/register.twig', [
  25.                     'user' => $user,
  26.                     'password' => $user->generatePassword()
  27.                 ])
  28.                 ->to($user->getEmail())
  29.                 ->subject('Mandalas | Votre boutique est prĂȘte !');
  30.             $this->mailer->send($email);
  31.         }
  32.     }
  33. }