src/Service/User/UserSubscriber.php line 72
<?php
/**
* Created by PhpStorm.
* User: Михаил
* Date: 30.09.2018
* Time: 23:45
*/
namespace App\Service\User;
use App\Service\Messaging\Email\Mailer;
use App\Service\Messaging\Messages\Registration;
use App\Service\User\Event\NewUserEvent;
use App\Service\User\Event\UserCreatedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class UserSubscriber implements EventSubscriberInterface
{
private Mailer $letter;
private TokenStorageInterface $tokenStorage;
private UserManager $userManager;
private Registration $registration;
public function __construct(Mailer $letter, Registration $registration, TokenStorageInterface $tokenStorage, UserManager $userManager)
{
$this->letter = $letter;
$this->tokenStorage = $tokenStorage;
$this->userManager = $userManager;
$this->registration = $registration;
}
/**
* 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:
*
* * array('eventName' => 'methodName')
* * array('eventName' => array('methodName', $priority))
* * array('eventName' => array(array('methodName1', $priority), array('methodName2')))
*
* @return array The event names to listen to
*/
public static function getSubscribedEvents(): array
{
return [
NewUserEvent::NAME => 'newUserEmail',
UserCreatedEvent::NAME => 'onCreate',
];
}
public function newUserEmail(NewUserEvent $event)
{
if (!$event->getConfig()->needRegistrationEmail) return;
$message = $this->registration->create($event->getUser(), $event->getPassword());
// return new Response($mes->getBody());
// $res = $sendLetter->sendMessage($mes);
//
// $message = new \Swift_Message();
// $message->setTo($event->getUser()->getEmail(), $event->getUser()->getFullname());
// $message->setBody($event->getPassword());
$this->letter->sendMessage($message);
}
public function onCreate(UserCreatedEvent $event)
{
$token = $this->tokenStorage->getToken();
if ($token && is_object($token->getUser())) {
return;
}
$user = $event->getUser();
$this->userManager->authUser($user);
}
}