Failed Conditions
Push — issue#666 ( f415d0...521a08 )
by Guilherme
12:02
created

TaskSubscriber::checkScope()   D

Complexity

Conditions 17
Paths 22

Size

Total Lines 35
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 306

Importance

Changes 0
Metric Value
cc 17
eloc 28
nc 22
nop 2
dl 0
loc 35
ccs 0
cts 26
cp 0
crap 306
rs 4.9807
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\OpenIDBundle\EventListener;
12
13
use LoginCidadao\CoreBundle\Helper\SecurityHelper;
14
use LoginCidadao\CoreBundle\Model\PersonInterface;
15
use LoginCidadao\OAuthBundle\Model\ClientInterface;
16
use LoginCidadao\OpenIDBundle\Manager\ClientManager;
17
use LoginCidadao\OpenIDBundle\Task\CompleteUserInfoTaskValidator;
18
use LoginCidadao\TaskStackBundle\Event\GetTasksEvent;
19
use LoginCidadao\TaskStackBundle\Model\TaskInterface;
20
use LoginCidadao\TaskStackBundle\TaskStackEvents;
21
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
22
use Symfony\Component\HttpFoundation\Request;
23
24
class TaskSubscriber implements EventSubscriberInterface
25
{
26
    /** @var SecurityHelper */
27
    private $securityHelper;
28
29
    /** @var ClientManager */
30
    private $clientManager;
31
32
    /** @var CompleteUserInfoTaskValidator */
33
    private $taskValidator;
34
35
    /**
36
     * TaskSubscriber constructor.
37
     * @param SecurityHelper $securityHelper
38
     * @param ClientManager $clientManager
39
     * @param CompleteUserInfoTaskValidator $taskValidator
40
     */
41 2
    public function __construct(
42
        SecurityHelper $securityHelper,
43
        ClientManager $clientManager,
44
        CompleteUserInfoTaskValidator $taskValidator
45
    ) {
46 2
        $this->securityHelper = $securityHelper;
47 2
        $this->clientManager = $clientManager;
48 2
        $this->taskValidator = $taskValidator;
49 2
    }
50
51 15
    public static function getSubscribedEvents()
52
    {
53
        return [
54 15
            TaskStackEvents::GET_TASKS => ['onGetTasks', 50],
55
        ];
56
    }
57
58 2
    public function onGetTasks(GetTasksEvent $event)
59
    {
60 2
        $request = $event->getRequest();
61 2
        $user = $this->securityHelper->getUser();
62 2
        $client = $this->getClientFromRequest($request);
63
64 2
        if (!$user instanceof PersonInterface || !$client instanceof ClientInterface) {
65 1
            return;
66
        }
67
68 1
        $task = $this->taskValidator->getCompleteUserInfoTask($user, $client, $request);
69 1
        if ($task instanceof TaskInterface) {
70 1
            $event->addTask($task);
71
        }
72 1
    }
73
74 2
    private function getClientFromRequest(Request $request)
75
    {
76 2
        $clientId = $request->get('client_id', $request->get('clientId'));
77
78 2
        return $this->clientManager->getClientById($clientId);
79
    }
80
}
81