AuthorizationStack::addAuthorization()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Security\Authorization;
6
7
use Chubbyphp\Security\UserInterface;
8
9
final class AuthorizationStack implements AuthorizationInterface
10
{
11
    /**
12
     * @var AuthorizationInterface[]
13
     */
14
    private $authorizations = [];
15
16
    /**
17
     * @param AuthorizationInterface[] $authorizations
18
     */
19 2
    public function __construct(array $authorizations)
20
    {
21 2
        foreach ($authorizations as $authorization) {
22 2
            $this->addAuthorization($authorization);
23
        }
24 2
    }
25
26
    /**
27
     * @param AuthorizationInterface $authorization
28
     */
29 2
    private function addAuthorization(AuthorizationInterface $authorization)
30
    {
31 2
        $this->authorizations[] = $authorization;
32 2
    }
33
34
    /**
35
     * @param UserInterface                  $user
36
     * @param mixed                          $attributes
37
     * @param OwnedByUserModelInterface|null $model
38
     *
39
     * @return bool
40
     */
41 2
    public function isGranted(UserInterface $user, $attributes, OwnedByUserModelInterface $model = null): bool
42
    {
43 2
        foreach ($this->authorizations as $authorization) {
44 2
            if ($authorization->isGranted($user, $attributes, $model)) {
45 2
                return true;
46
            }
47
        }
48
49 1
        return false;
50
    }
51
}
52