<?php
namespace App\Infrastructure\Mailing\Subscriber;
use App\Domain\Auth\Entity\Collaborator;
use App\Domain\Auth\Event\UserCreatedEvent;
use App\Infrastructure\Mailing\Mailer;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class CollaboratorSubscriber implements EventSubscriberInterface
{
private Mailer $mailer;
public function __construct(Mailer $mailer)
{
$this->mailer = $mailer;
}
public static function getSubscribedEvents(): array
{
return [
UserCreatedEvent::class => 'onCollaboratorCreated'
];
}
public function onCollaboratorCreated(UserCreatedEvent $event): void
{
$user = $event->getUser();
if($user instanceof Collaborator) {
$email = $this->mailer->createEmail('mails/auth/register.twig', [
'user' => $user,
'password' => $user->generatePassword()
])
->to($user->getEmail())
->subject('Mandalas | Votre boutique est prĂȘte !');
$this->mailer->send($email);
}
}
}