Passed
Push — master ( a6dc76...6d79cb )
by Guilherme
01:12 queued 11s
created

BlocklistSubscriber   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 55
ccs 19
cts 19
cp 1
rs 10
c 0
b 0
f 0
wmc 8

5 Methods

Rating   Name   Duplication   Size   Complexity  
A onPhoneChange() 0 4 1
A getSubscribedEvents() 0 5 1
A onLogin() 0 6 2
A checkPhone() 0 4 3
A __construct() 0 4 1
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\Security\Core\Authorization\AuthorizationCheckerInterface;
19
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
20
use Symfony\Component\Security\Http\SecurityEvents;
21
22
class BlocklistSubscriber implements EventSubscriberInterface
23
{
24
    /** @var BlocklistInterface */
25
    private $blocklist;
26
27
    /** @var AuthorizationCheckerInterface */
28
    private $authChecker;
29
30
    /**
31
     * BlocklistSubscriber constructor.
32
     * @param BlocklistInterface $blocklist
33
     * @param AuthorizationCheckerInterface $authorizationChecker
34
     */
35 4
    public function __construct(BlocklistInterface $blocklist, AuthorizationCheckerInterface $authorizationChecker)
36
    {
37 4
        $this->blocklist = $blocklist;
38 4
        $this->authChecker = $authorizationChecker;
39 4
    }
40
41
    /**
42
     * @inheritDoc
43
     */
44 1
    public static function getSubscribedEvents()
45
    {
46
        return [
47 1
            PhoneVerificationEvents::PHONE_CHANGED => 'onPhoneChange',
48
            SecurityEvents::INTERACTIVE_LOGIN => 'onLogin',
49
        ];
50
    }
51
52
    /**
53
     * @param PhoneChangedEvent $event
54
     */
55 2
    public function onPhoneChange(PhoneChangedEvent $event)
56
    {
57 2
        $phone = $event->getPerson()->getMobile();
58 2
        $this->checkPhone($phone);
59 2
    }
60
61
    /**
62
     * @param InteractiveLoginEvent $event
63
     */
64 2
    public function onLogin(InteractiveLoginEvent $event)
65
    {
66 2
        $person = $event->getAuthenticationToken()->getUser();
67 2
        if ($person instanceof PersonInterface) {
68 2
            $phone = $person->getMobile();
69 2
            $this->checkPhone($phone);
70
        }
71 2
    }
72
73 4
    private function checkPhone(?PhoneNumber $phoneNumber)
74
    {
75 4
        if ($phoneNumber instanceof PhoneNumber && $this->authChecker->isGranted('FEATURE_PHONE_BLOCKLIST')) {
76 2
            $this->blocklist->checkPhoneNumber($phoneNumber);
77
        }
78 4
    }
79
}
80