Failed Conditions
Push — issue#666 ( 82e9d5...91903a )
by Guilherme
08:00
created

TaskSubscriber   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 74
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0
wmc 12

3 Methods

Rating   Name   Duplication   Size   Complexity  
D onGetTasks() 0 40 10
A __construct() 0 8 1
A getSubscribedEvents() 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 FOS\OAuthServerBundle\Security\Authentication\Token\OAuthToken;
14
use LoginCidadao\CoreBundle\Model\PersonInterface;
15
use LoginCidadao\PhoneVerificationBundle\Entity\PhoneVerification;
16
use LoginCidadao\PhoneVerificationBundle\Exception\VerificationNotSentException;
17
use LoginCidadao\PhoneVerificationBundle\Model\ConfirmPhoneTask;
18
use LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface;
19
use LoginCidadao\TaskStackBundle\Event\GetTasksEvent;
20
use LoginCidadao\TaskStackBundle\TaskStackEvents;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
23
24
class TaskSubscriber implements EventSubscriberInterface
25
{
26
    /** @var TokenStorageInterface */
27
    private $tokenStorage;
28
29
    /** @var PhoneVerificationServiceInterface */
30
    private $phoneVerificationService;
31
32
    /** @var bool */
33
    private $verificationEnabled;
34
35
    /**
36
     * TaskSubscriber constructor.
37
     * @param TokenStorageInterface $tokenStorage
38
     * @param PhoneVerificationServiceInterface $phoneVerificationService
39
     * @param bool $verificationEnabled
40
     */
41 7
    public function __construct(
42
        TokenStorageInterface $tokenStorage,
43
        PhoneVerificationServiceInterface $phoneVerificationService,
44
        $verificationEnabled = false
45
    ) {
46 7
        $this->tokenStorage = $tokenStorage;
47 7
        $this->phoneVerificationService = $phoneVerificationService;
48 7
        $this->verificationEnabled = $verificationEnabled;
49 7
    }
50
51 15
    public static function getSubscribedEvents()
52
    {
53
        return [
54 15
            TaskStackEvents::GET_TASKS => ['onGetTasks', 100],
55
        ];
56
    }
57
58 7
    public function onGetTasks(GetTasksEvent $event)
59
    {
60 7
        if (!$this->verificationEnabled) {
61 1
            return;
62
        }
63
64 6
        $token = $this->tokenStorage->getToken();
65 6
        if ($token instanceof OAuthToken) {
66 1
            return;
67
        }
68
69
        /** @var PersonInterface $user */
70 5
        $user = $token->getUser();
71 5
        if (!$user instanceof PersonInterface) {
0 ignored issues
show
introduced by
The condition ! $user instanceof Login...e\Model\PersonInterface can never be true.
Loading history...
72 1
            return;
73
        }
74
75 4
        $pending = $this->phoneVerificationService->getAllPendingPhoneVerification($user);
76 4
        if (!is_array($pending) || count($pending) === 0) {
77 1
            return;
78
        }
79
80
        /** @var PhoneVerification $pendingVerification */
81 3
        $pendingVerification = reset($pending);
82 3
        $lastSentVerification = $this->phoneVerificationService->getLastSentVerification($pendingVerification);
83
84 3
        if ($pendingVerification && !$lastSentVerification) {
0 ignored issues
show
introduced by
The condition $pendingVerification && ! $lastSentVerification can never be true.
Loading history...
85
            try {
86 3
                if ($user->getMobile() != $pendingVerification->getPhone()) {
87
                    // Orphan verification. Do not add a task
88 1
                    return;
89
                }
90 2
                $this->phoneVerificationService->sendVerificationCode($pendingVerification);
91 1
            } catch (VerificationNotSentException $e) {
92
                // could not send the verification code, do not add the task
93 1
                return;
94
            }
95
        }
96
97 1
        $event->addTaskIfStackEmpty(new ConfirmPhoneTask($pendingVerification->getId()));
98 1
    }
99
}
100