Completed
Pull Request — master (#379)
by Kristof
12:23
created

PermissionSplitVoter::isAllowed()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
/**
3
 * @file
4
 */
5
6
namespace CultuurNet\UDB3\Offer\Security\Permission;
7
8
use CultuurNet\UDB3\Role\ValueObjects\Permission;
9
use ValueObjects\StringLiteral\StringLiteral;
10
11
/**
12
 * Delegates voting to another voter based on which permission needs checking.
13
 */
14
class PermissionSplitVoter implements PermissionVoterInterface
15
{
16
    /**
17
     * @var \CultuurNet\UDB3\Offer\Security\Permission\PermissionVoterInterface[]
18
     */
19
    private $mapping;
20
21
    public function isAllowed(
22
        Permission $permission,
23
        StringLiteral $offerId,
24
        StringLiteral $userId
25
    ) {
26
        if (!isset($this->mapping[(string)$permission])) {
27
            return false;
28
        }
29
30
        return $this->mapping[(string)$permission]->isAllowed($permission, $offerId, $userId);
31
    }
32
33
    /**
34
     * @param \CultuurNet\UDB3\Offer\Security\Permission\PermissionVoterInterface $voter
35
     * @param \CultuurNet\UDB3\Role\ValueObjects\Permission ...$permissions
36
     * @return \CultuurNet\UDB3\Offer\Security\Permission\PermissionSplitVoter
37
     */
38
    public function withVoter(PermissionVoterInterface $voter, Permission ...$permissions) {
39
        $c = clone $this;
40
41
        foreach ($permissions as $permission) {
42
            $c->mapping[(string)$permission] = $voter;
43
        }
44
45
        return $c;
46
    }
47
}
48