Completed
Push — 3.2 ( cb819c...8b200a )
by Piotr
18s queued 11s
created

ResendActvationMailListener   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 33
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A resendActivationMail() 0 10 3
A __construct() 0 4 1
A getSubscribedEvents() 0 3 1
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\EventListener;
13
14
use FSi\Bundle\AdminSecurityBundle\Event\ActivationEvent;
15
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
16
use FSi\Bundle\AdminSecurityBundle\Mailer\MailerInterface;
17
use FSi\Bundle\AdminSecurityBundle\Security\Token\TokenFactoryInterface;
18
use FSi\Bundle\AdminSecurityBundle\Security\User\ActivableInterface;
19
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
20
21
final class ResendActvationMailListener implements EventSubscriberInterface
22
{
23
    /**
24
     * @var MailerInterface
25
     */
26
    private $mailer;
27
28
    /**
29
     * @var TokenFactoryInterface
30
     */
31
    private $tokenFactory;
32
33
    public function __construct(MailerInterface $mailer, TokenFactoryInterface $tokenFactory)
34
    {
35
        $this->mailer = $mailer;
36
        $this->tokenFactory = $tokenFactory;
37
    }
38
39
    public static function getSubscribedEvents(): array
40
    {
41
        return [AdminSecurityEvents::RESEND_ACTIVATION_TOKEN => 'resendActivationMail'];
42
    }
43
44
    public function resendActivationMail(ActivationEvent $event): void
45
    {
46
        $user = $event->getUser();
47
        if (false === $user instanceof ActivableInterface || true === $user->isEnabled()) {
48
            return;
49
        }
50
51
        $user->removeActivationToken();
52
        $user->setActivationToken($this->tokenFactory->createToken());
53
        $this->mailer->send($user);
54
    }
55
}
56