Completed
Push — master ( 33ac2f...378c34 )
by Dominik
02:14
created

AuthorizationStack::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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