EmailConfirmationListener   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 7
dl 0
loc 73
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getSubscribedEvents() 0 6 1
A sendConfirmationEmail() 0 16 2
A generateToken() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of the awurth/silex-user package.
5
 *
6
 * (c) Alexis Wurth <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace AWurth\Silex\User\EventListener;
13
14
use AWurth\Silex\User\Model\UserInterface;
15
use AWurth\Silex\User\Event\Events;
16
use AWurth\Silex\User\Event\FormEvent;
17
use AWurth\Silex\User\Mailer\MailerInterface;
18
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Session\SessionInterface;
21
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
22
23
class EmailConfirmationListener implements EventSubscriberInterface
24
{
25
    /**
26
     * @var MailerInterface
27
     */
28
    protected $mailer;
29
30
    /**
31
     * @var UrlGeneratorInterface
32
     */
33
    protected $router;
34
35
    /**
36
     * @var SessionInterface
37
     */
38
    protected $session;
39
40
    /**
41
     * Constructor.
42
     *
43
     * @param MailerInterface       $mailer
44
     * @param UrlGeneratorInterface $router
45
     * @param SessionInterface      $session
46
     */
47
    public function __construct(MailerInterface $mailer, UrlGeneratorInterface $router, SessionInterface $session)
48
    {
49
        $this->mailer = $mailer;
50
        $this->router = $router;
51
        $this->session = $session;
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public static function getSubscribedEvents()
58
    {
59
        return [
60
            Events::REGISTRATION_SUCCESS => 'sendConfirmationEmail'
61
        ];
62
    }
63
64
    /**
65
     * Sends an email to the user to activate his account.
66
     *
67
     * @param FormEvent $event
68
     */
69
    public function sendConfirmationEmail(FormEvent $event)
70
    {
71
        /** @var UserInterface $user */
72
        $user = $event->getForm()->getData();
73
74
        $user->setEnabled(false);
75
        if (null === $user->getConfirmationToken()) {
76
            $user->setConfirmationToken($this->generateToken());
77
        }
78
79
        $this->mailer->sendConfirmationEmailMessage($user);
80
81
        $this->session->set('silex_user_confirmation_email', $user->getEmail());
82
83
        $event->setResponse(new RedirectResponse($this->router->generate('silex_user.registration_check_email')));
84
    }
85
86
    /**
87
     * Generates a new confirmation token.
88
     *
89
     * @return string
90
     */
91
    protected function generateToken()
92
    {
93
        return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
94
    }
95
}
96