Passed
Pull Request — 3.2 (#125)
by Piotr
04:06
created

resendActivationMail()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
rs 10
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