<?php
namespace App\Modules\Notification\Event\Subscriber;
use App\Modules\Notification\Message\Email\User\ResetPasswordEmail;
use App\Modules\Notification\Service\NotificationService;
use App\Modules\User\Event\ResetPasswordRequested;
use App\Service\WebRoutes;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SendResetPasswordRequestNotification implements EventSubscriberInterface
{
private $notificationService;
private $logger;
public function __construct(NotificationService $notificationService, LoggerInterface $logger)
{
$this->notificationService = $notificationService;
$this->logger = $logger;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents()
{
return [
ResetPasswordRequested::class => 'sendEmail',
];
}
public function sendEmail(ResetPasswordRequested $event)
{
$user = $event->getUser();
try {
$username = $user->getFirstName() . ' ' . $user->getLastName();
$resetUrl = WebRoutes::getUrl(WebRoutes::RESET_PASSWORD, ['token' => $user->getConfirmationToken()]);
$recipients = [$user->getEmail()];
$emailMessage = new ResetPasswordEmail($username, $resetUrl, $recipients);
$this->notificationService->sendEmail($emailMessage);
} catch (\Exception $e) {
$this->logger->error($e->getMessage());
}
}
}