Completed
Push — issue#666 ( 928a8e...2654e9 )
by Guilherme
04:24
created

RemoteClaimManager::isAuthorized()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 2
nop 3
dl 0
loc 13
rs 9.4285
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\RemoteClaimsBundle\Model;
12
13
use Doctrine\ORM\EntityManagerInterface;
14
use LoginCidadao\CoreBundle\Entity\Authorization;
15
use LoginCidadao\CoreBundle\Model\PersonInterface;
16
use LoginCidadao\OAuthBundle\Model\ClientInterface;
17
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorization;
18
use LoginCidadao\RemoteClaimsBundle\Entity\RemoteClaimAuthorizationRepository;
19
20
class RemoteClaimManager implements RemoteClaimManagerInterface
21
{
22
    /** @var EntityManagerInterface */
23
    private $em;
24
25
    /** @var RemoteClaimAuthorizationRepository */
26
    private $remoteClaimAuthorizationRepository;
27
28
    /**
29
     * RemoteClaimManager constructor.
30
     * @param EntityManagerInterface $em
31
     * @param RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository
32
     */
33
    public function __construct(
34
        EntityManagerInterface $em,
35
        RemoteClaimAuthorizationRepository $remoteClaimAuthorizationRepository
36
    ) {
37
        $this->em = $em;
38
        $this->remoteClaimAuthorizationRepository = $remoteClaimAuthorizationRepository;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function enforceAuthorization(RemoteClaimAuthorizationInterface $authorization)
45
    {
46
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
47
        if ($existingAuthorization instanceof RemoteClaimAuthorizationInterface) {
48
            return $existingAuthorization;
49
        }
50
51
        $this->em->persist($authorization);
52
53
        return $authorization;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function isAuthorized($claimName, PersonInterface $person, ClientInterface $client)
60
    {
61
        if (!$claimName instanceof TagUri) {
62
            $claimName = TagUri::createFromString($claimName);
63
        }
64
        $authorization = (new RemoteClaimAuthorization())
65
            ->setClaimName($claimName)
66
            ->setPerson($person)
67
            ->setClient($client);
68
        $existingAuthorization = $this->remoteClaimAuthorizationRepository->findAuthorization($authorization);
69
70
        return $existingAuthorization instanceof RemoteClaimAuthorizationInterface;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function revokeAllAuthorizations(Authorization $authorization)
77
    {
78
        $remoteClaimAuthorizations = $this->remoteClaimAuthorizationRepository
79
            ->findAllByClientAndPerson($authorization->getClient(), $authorization->getPerson());
0 ignored issues
show
Bug introduced by
It seems like $authorization->getClient() can be null; however, findAllByClientAndPerson() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
Bug introduced by
It seems like $authorization->getPerson() can be null; however, findAllByClientAndPerson() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
80
81
        foreach ($remoteClaimAuthorizations as $remoteClaimAuthorization) {
82
            $this->em->remove($remoteClaimAuthorization);
83
        }
84
        $this->em->flush();
85
86
        return true;
87
    }
88
}
89