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

AuthorizationStack   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 43
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A addAuthorization() 0 4 1
A isGranted() 0 10 3
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