src/Modules/Notification/Event/Subscriber/SendResetPasswordRequestNotification.php line 49

Open in your IDE?
  1. <?php
  2. namespace App\Modules\Notification\Event\Subscriber;
  3. use App\Modules\Notification\Message\Email\User\ResetPasswordEmail;
  4. use App\Modules\Notification\Service\NotificationService;
  5. use App\Modules\User\Event\ResetPasswordRequested;
  6. use App\Service\WebRoutes;
  7. use Psr\Log\LoggerInterface;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class SendResetPasswordRequestNotification implements EventSubscriberInterface
  10. {
  11.     private $notificationService;
  12.     private $logger;
  13.     public function __construct(NotificationService $notificationServiceLoggerInterface $logger)
  14.     {
  15.         $this->notificationService $notificationService;
  16.         $this->logger $logger;
  17.     }
  18.     /**
  19.      * Returns an array of event names this subscriber wants to listen to.
  20.      *
  21.      * The array keys are event names and the value can be:
  22.      *
  23.      *  * The method name to call (priority defaults to 0)
  24.      *  * An array composed of the method name to call and the priority
  25.      *  * An array of arrays composed of the method names to call and respective
  26.      *    priorities, or 0 if unset
  27.      *
  28.      * For instance:
  29.      *
  30.      *  * ['eventName' => 'methodName']
  31.      *  * ['eventName' => ['methodName', $priority]]
  32.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  33.      *
  34.      * @return array The event names to listen to
  35.      */
  36.     public static function getSubscribedEvents()
  37.     {
  38.         return [
  39.             ResetPasswordRequested::class => 'sendEmail',
  40.         ];
  41.     }
  42.     public function sendEmail(ResetPasswordRequested $event)
  43.     {
  44.         $user $event->getUser();
  45.         try {
  46.             $username $user->getFirstName() . ' ' $user->getLastName();
  47.             $resetUrl WebRoutes::getUrl(WebRoutes::RESET_PASSWORD, ['token' => $user->getConfirmationToken()]);
  48.             $recipients = [$user->getEmail()];
  49.             $emailMessage = new ResetPasswordEmail($username$resetUrl$recipients);
  50.             $this->notificationService->sendEmail($emailMessage);
  51.         } catch (\Exception $e) {
  52.             $this->logger->error($e->getMessage());
  53.         }
  54.     }
  55. }