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

SecurityWithFacilityPermission   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A isAuthorized() 0 14 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 CultuurNet\UDB3\Place\Commands\UpdateFacilities;
8
use CultuurNet\UDB3\Security\SecurityDecoratorBase;
9
use CultuurNet\UDB3\Security\SecurityInterface;
10
use CultuurNet\UDB3\Security\UserIdentificationInterface;
11
use ValueObjects\StringLiteral\StringLiteral;
12
13
class SecurityWithFacilityPermission extends SecurityDecoratorBase
14
{
15
    /**
16
     * @var UserIdentificationInterface
17
     */
18
    private $userIdentification;
19
20
    /**
21
     * @var PermissionVoterInterface
22
     */
23
    private $permissionVoter;
24
25
    /**
26
     * @param SecurityInterface $decoratee
27
     * @param UserIdentificationInterface $userIdentification
28
     * @param PermissionVoterInterface $permissionVoter
29
     */
30
    public function __construct(
31
        SecurityInterface $decoratee,
32
        UserIdentificationInterface $userIdentification,
33
        PermissionVoterInterface $permissionVoter
34
    ) {
35
        parent::__construct($decoratee);
36
37
        $this->userIdentification = $userIdentification;
38
        $this->permissionVoter = $permissionVoter;
39
    }
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function isAuthorized(AuthorizableCommandInterface $command)
45
    {
46
        //@todo: When extending for events create an interface.
47
        // https://jira.uitdatabank.be/browse/III-2413
48
        if ($command instanceof UpdateFacilities) {
49
            return $this->permissionVoter->isAllowed(
50
                $command->getPermission(),
51
                new StringLiteral(''),
52
                $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...
53
            );
54
        }
55
56
        return parent::isAuthorized($command);
57
    }
58
}
59