Completed
Push — issue#830 ( c39427...eac670 )
by Guilherme
04:26
created

BlocklistSubscriber::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\PhoneVerificationBundle\Event;
12
13
use libphonenumber\PhoneNumber;
14
use LoginCidadao\CoreBundle\Model\PersonInterface;
15
use LoginCidadao\PhoneVerificationBundle\PhoneVerificationEvents;
16
use LoginCidadao\PhoneVerificationBundle\Service\BlocklistInterface;
17
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
18
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
19
use Symfony\Component\HttpKernel\KernelEvents;
20
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
21
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
22
use Symfony\Component\Security\Http\SecurityEvents;
23
24
class BlocklistSubscriber implements EventSubscriberInterface
25
{
26
    /** @var BlocklistInterface */
27
    private $blocklist;
28
29
    /** @var TokenStorageInterface */
30
    private $tokenStorage;
31
32
    /**
33
     * BlocklistSubscriber constructor.
34
     * @param BlocklistInterface $blocklist
35
     */
36 2
    public function __construct(BlocklistInterface $blocklist)
37
    {
38 2
        $this->blocklist = $blocklist;
39 2
    }
40
41
    /**
42
     * @param TokenStorageInterface $tokenStorage
43
     * @codeCoverageIgnore
44
     */
45
    public function setTokenStorage(TokenStorageInterface $tokenStorage)
46
    {
47
        $this->tokenStorage = $tokenStorage;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53 1
    public static function getSubscribedEvents()
54
    {
55
        return [
56 1
            PhoneVerificationEvents::PHONE_CHANGED => 'onPhoneChange',
57
            SecurityEvents::INTERACTIVE_LOGIN => 'onLogin',
58
            KernelEvents::REQUEST => 'onRequest', // TODO: remove
59
        ];
60
    }
61
62 1
    public function onPhoneChange(PhoneChangedEvent $event)
63
    {
64 1
        $phone = $event->getPerson()->getMobile();
65 1
        $this->checkPhone($phone);
66 1
    }
67
68 1
    public function onLogin(InteractiveLoginEvent $event)
69
    {
70 1
        $person = $event->getAuthenticationToken()->getUser();
71 1
        if ($person instanceof PersonInterface) {
72 1
            $phone = $person->getMobile();
73 1
            $this->checkPhone($phone);
74
        }
75 1
    }
76
77
    /**
78
     * @param GetResponseEvent $event
79
     * @codeCoverageIgnore
80
     */
81
    public function onRequest(GetResponseEvent $event)
82
    {
83
        if ($event->isMasterRequest() && null !== $this->tokenStorage->getToken()) {
84
            /** @var PersonInterface $person */
85
            $person = $this->tokenStorage->getToken()->getUser();
86
            if ($person instanceof PersonInterface) {
0 ignored issues
show
introduced by
$person is always a sub-type of LoginCidadao\CoreBundle\Model\PersonInterface.
Loading history...
87
                $phone = $person->getMobile();
88
89
                $this->checkPhone($phone);
90
            }
91
        }
92
    }
93
94
    /**
95
     * @param PhoneNumber|null $phoneNumber
96
     */
97 2
    private function checkPhone(?PhoneNumber $phoneNumber)
98
    {
99 2
        if ($phoneNumber instanceof PhoneNumber) {
100 2
            $this->blocklist->checkPhoneNumber($phoneNumber);
101
        }
102 2
    }
103
}
104