<?php
declare(strict_types=1);
namespace App\Bundles\UserBundle\EventListener;
use App\Bundles\EpidemiologicalSurveillanceBundle\Service\SurveillanceTemplateDeleteService;
use App\Bundles\UserBundle\Entity\User;
use App\Bundles\UserBundle\Event\UserUpdatedEvent;
use App\Bundles\UserBundle\Service\UserEmail\UserEmailSender;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class UserUpdatedListener implements EventSubscriberInterface
{
public function __construct(
private readonly UserEmailSender $emailSender,
private readonly SurveillanceTemplateDeleteService $surveillanceTemplateDeleteService,
) {
}
public static function getSubscribedEvents(): array
{
return [
UserUpdatedEvent::NAME => 'onUserUpdated',
];
}
public function onUserUpdated(UserUpdatedEvent $event): void
{
$updatedUser = $event->getUser();
if ($event->isEmailChanged()) {
$this->emailSender->sendEmailWithOnlyVerificationLink($updatedUser);
}
if ($event->getNewPassword()) {
$this->emailSender->sendUserPassword($updatedUser, $event->getNewPassword());
}
if ($updatedUser instanceof User) {
$this->surveillanceTemplateDeleteService->deleteAllSharedForUser($updatedUser);
}
}
}