Completed
Pull Request — master (#335)
by Luc
04:38
created

SecurityWithUserPermission   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 6
dl 0
loc 52
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A isAuthorized() 0 12 2
1
<?php
2
3
namespace CultuurNet\UDB3\Security;
4
5
use CultuurNet\UDB3\Offer\Commands\AuthorizableCommandInterface;
6
use CultuurNet\UDB3\Offer\Security\Permission\PermissionVoterInterface;
7
use ValueObjects\StringLiteral\StringLiteral;
8
9
class SecurityWithUserPermission extends SecurityDecoratorBase
10
{
11
    /**
12
     * @var UserIdentificationInterface
13
     */
14
    private $userIdentification;
15
16
    /**
17
     * @var PermissionVoterInterface
18
     */
19
    private $permissionVoter;
20
21
    /**
22
     * @var CommandFilterInterface
23
     */
24
    private $commandFilter;
25
26
    /**
27
     * @param SecurityInterface $decoratee
28
     * @param UserIdentificationInterface $userIdentification
29
     * @param PermissionVoterInterface $permissionVoter
30
     * @param CommandFilterInterface $commandFilter
31
     */
32
    public function __construct(
33
        SecurityInterface $decoratee,
34
        UserIdentificationInterface $userIdentification,
35
        PermissionVoterInterface $permissionVoter,
36
        CommandFilterInterface $commandFilter
37
    ) {
38
        parent::__construct($decoratee);
39
40
        $this->userIdentification = $userIdentification;
41
        $this->permissionVoter = $permissionVoter;
42
        $this->commandFilter = $commandFilter;
43
    }
44
45
    /**
46
     * @inheritdoc
47
     */
48
    public function isAuthorized(AuthorizableCommandInterface $command)
49
    {
50
        if ($this->commandFilter->matches($command)) {
51
            return $this->permissionVoter->isAllowed(
52
                $command->getPermission(),
53
                new StringLiteral(''),
54
                $this->userIdentification->getId()
0 ignored issues
show
Bug introduced by
It seems like $this->userIdentification->getId() can be null; however, isAllowed() 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...
55
            );
56
        }
57
58
        return parent::isAuthorized($command);
59
    }
60
}
61