Completed
Push — master ( c3efa2...a812ec )
by Guilherme
13s
created

OAuthEventListener::getScope()   A

Complexity

Conditions 4
Paths 8

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 8
nop 0
dl 0
loc 14
rs 9.2
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\OAuthBundle\EventListener;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use FOS\OAuthServerBundle\Event\OAuthEvent;
15
use LoginCidadao\CoreBundle\Entity\Authorization;
16
use LoginCidadao\CoreBundle\Entity\PersonRepository;
17
use LoginCidadao\CoreBundle\Model\PersonInterface;
18
use LoginCidadao\OAuthBundle\Entity\Client;
19
use LoginCidadao\OAuthBundle\Helper\ScopeFinderHelper;
20
use LoginCidadao\OAuthBundle\Model\ClientInterface;
21
use LoginCidadao\OpenIDBundle\Entity\SubjectIdentifier;
22
use LoginCidadao\OpenIDBundle\Service\SubjectIdentifierService;
23
24
class OAuthEventListener
25
{
26
    /** @var EntityManagerInterface */
27
    private $em;
28
29
    /** @var PersonRepository */
30
    private $personRepo;
31
32
    /** @var SubjectIdentifierService */
33
    private $subjectIdentifierService;
34
35
    /** @var ScopeFinderHelper */
36
    private $scopeFinder;
37
38
    public function __construct(
39
        EntityManagerInterface $em,
40
        ScopeFinderHelper $scopeFinder,
41
        SubjectIdentifierService $subjectIdentifierService
42
    ) {
43
        $this->em = $em;
44
        $this->personRepo = $this->em->getRepository('LoginCidadaoCoreBundle:Person');
45
        $this->scopeFinder = $scopeFinder;
46
        $this->subjectIdentifierService = $subjectIdentifierService;
47
    }
48
49
    public function onPreAuthorizationProcess(OAuthEvent $event)
50
    {
51
        $scope = $this->scopeFinder->getScope();
52
        /** @var PersonInterface $user */
53
        $user = $this->getUser($event);
54
        if (!$user) {
55
            return;
56
        }
57
58
        /** @var ClientInterface $client */
59
        $client = $event->getClient();
60
61
        $event->setAuthorizedClient(
62
            $user->isAuthorizedClient($client, $scope)
0 ignored issues
show
Compatibility introduced by
$client of type object<LoginCidadao\OAut...\Model\ClientInterface> is not a sub-type of object<LoginCidadao\OAuthBundle\Entity\Client>. It seems like you assume a concrete implementation of the interface LoginCidadao\OAuthBundle\Model\ClientInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
63
        );
64
65
        if ($event->isAuthorizedClient()) {
66
            $this->checkSubjectIdentifierPersisted($user, $client);
67
        }
68
    }
69
70
    public function onPostAuthorizationProcess(OAuthEvent $event)
71
    {
72
        if (!$event->isAuthorizedClient()) {
73
            return;
74
        }
75
76
        /** @var Client $client */
77
        $client = $event->getClient();
78
79
        /** @var PersonInterface $user */
80
        $user = $this->getUser($event);
81
        $scope = $this->scopeFinder->getScope();
82
83
        $authRepo = $this->em->getRepository('LoginCidadaoCoreBundle:Authorization');
84
        $currentAuth = $authRepo->findOneBy([
85
            'person' => $user,
86
            'client' => $client,
87
        ]);
88
89
        // if the authorization is already there, update it.
90
        if ($currentAuth instanceof Authorization) {
91
            $merged = array_merge($currentAuth->getScope(), $scope);
92
            $currentAuth->setScope($merged);
93
            $this->checkSubjectIdentifierPersisted($user, $client);
94
        } else {
95
            $authorization = new Authorization();
96
            $authorization->setClient($client);
97
            $authorization->setPerson($user);
98
            $authorization->setScope($scope);
0 ignored issues
show
Bug introduced by
It seems like $scope defined by $this->scopeFinder->getScope() on line 81 can also be of type null; however, LoginCidadao\CoreBundle\...thorization::setScope() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
99
100
            $subjectIdentifier = $this->subjectIdentifierService->getSubjectIdentifier($user, $client->getMetadata());
101
            $sub = new SubjectIdentifier();
102
            $sub->setPerson($user)
103
                ->setClient($client)
104
                ->setSubjectIdentifier($subjectIdentifier);
105
106
            $this->em->persist($authorization);
107
            $this->em->persist($sub);
108
        }
109
110
        $this->em->flush();
111
    }
112
113
    public function getUser(OAuthEvent $event)
114
    {
115
        return $this->personRepo->findOneBy(['username' => $event->getUser()->getUsername()]);
116
    }
117
118
    private function checkSubjectIdentifierPersisted(PersonInterface $person, ClientInterface $client)
119
    {
120
        if ($this->subjectIdentifierService->isSubjectIdentifierPersisted($person, $client)) {
121
            return;
122
        }
123
124
        $subjectIdentifier = $this->subjectIdentifierService->getSubjectIdentifier($person, $client->getMetadata());
125
        $sub = new SubjectIdentifier();
126
        $sub->setPerson($person)
127
            ->setClient($client)
128
            ->setSubjectIdentifier($subjectIdentifier);
129
        $this->em->persist($sub);
130
        $this->em->flush($sub);
0 ignored issues
show
Unused Code introduced by
The call to EntityManagerInterface::flush() has too many arguments starting with $sub.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
131
    }
132
}
133