/
EmailConfirmationListener.php
69 lines (55 loc) 路 2.11 KB
/
EmailConfirmationListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/*
* This file is part of the NucleosProfileBundle package.
*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nucleos\ProfileBundle\EventListener;
use Nucleos\ProfileBundle\Event\UserFormEvent;
use Nucleos\ProfileBundle\Mailer\RegistrationMailer;
use Nucleos\ProfileBundle\NucleosProfileEvents;
use Nucleos\UserBundle\Util\TokenGenerator;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
final class EmailConfirmationListener implements EventSubscriberInterface
{
private RegistrationMailer $mailer;
private TokenGenerator $tokenGenerator;
private UrlGeneratorInterface $router;
private RequestStack $requestStack;
public function __construct(
RegistrationMailer $mailer,
TokenGenerator $tokenGenerator,
UrlGeneratorInterface $router,
RequestStack $requestStack
) {
$this->mailer = $mailer;
$this->tokenGenerator = $tokenGenerator;
$this->router = $router;
$this->requestStack = $requestStack;
}
public static function getSubscribedEvents(): array
{
return [
NucleosProfileEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess',
];
}
public function onRegistrationSuccess(UserFormEvent $event): void
{
$user = $event->getUser();
$user->setEnabled(false);
if (null === $user->getConfirmationToken()) {
$user->setConfirmationToken($this->tokenGenerator->generateToken());
}
$this->mailer->sendConfirmationEmailMessage($user);
$this->requestStack->getSession()->set('nucleos_profile_send_confirmation_email/email', $user->getEmail());
$event->setResponse(
new RedirectResponse($this->router->generate('nucleos_profile_registration_check_email'))
);
}
}