PolicyEnforcementPoint   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 110
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A authorize() 0 23 4
A isExecuteAdvice() 0 4 1
A enableExecuteAdvice() 0 6 1
A disableExecuteAdvice() 0 6 1
A getPip() 0 4 1
A getPdp() 0 4 1
A interpretEvaluation() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Limoncello\Auth\Authorization\PolicyEnforcement;
4
5
/**
6
 * Copyright 2015-2019 [email protected]
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 * http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
21
use Limoncello\Auth\Contracts\Authorization\PolicyAdministration\EvaluationEnum;
22
use Limoncello\Auth\Contracts\Authorization\PolicyDecision\PolicyDecisionPointInterface;
23
use Limoncello\Auth\Contracts\Authorization\PolicyEnforcement\PolicyEnforcementPointInterface;
24
use Limoncello\Auth\Contracts\Authorization\PolicyEnforcement\RequestInterface;
25
use Limoncello\Auth\Contracts\Authorization\PolicyInformation\PolicyInformationPointInterface;
26
use Psr\Log\LoggerAwareInterface;
27
use Psr\Log\LoggerAwareTrait;
28
use function call_user_func;
29
30
/**
31
 * @package Limoncello\Auth
32
 */
33
class PolicyEnforcementPoint implements PolicyEnforcementPointInterface, LoggerAwareInterface
34
{
35
    use LoggerAwareTrait;
36
37
    /**
38
     * @var PolicyInformationPointInterface
39
     */
40
    private $pip;
41
42
    /**
43
     * @var PolicyDecisionPointInterface
44
     */
45
    private $pdp;
46
47
    /**
48
     * @var bool
49
     */
50
    private $isExecuteAdvice = true;
51
52
    /**
53 8
     * @param PolicyInformationPointInterface $pip
54
     * @param PolicyDecisionPointInterface    $pdp
55 8
     */
56 8
    public function __construct(PolicyInformationPointInterface $pip, PolicyDecisionPointInterface $pdp)
57
    {
58
        $this->pip = $pip;
59
        $this->pdp = $pdp;
60
    }
61
62 8
    /**
63
     * @inheritdoc
64 8
     */
65
    public function authorize(RequestInterface $request): bool
66
    {
67
        $context = $this->getPip()->createContext($request);
68
69 8
        /** @var int $evaluation */
70
        /** @var callable[] $obligations */
71 8
        /** @var callable[] $advice */
72
        list ($evaluation, $obligations, $advice) = $this->getPdp()->evaluate($context);
73 8
74 1
        $isAuthorized = $this->interpretEvaluation($evaluation);
75
76
        foreach ($obligations as $obligation) {
77 8
            call_user_func($obligation, $context);
78 8
        }
79 1
80
        if ($this->isExecuteAdvice() === true) {
81
            foreach ($advice as $callable) {
82
                call_user_func($callable, $context);
83 8
            }
84
        }
85
86
        return $isAuthorized;
87
    }
88
89 8
    /**
90
     * @return bool
91 8
     */
92
    public function isExecuteAdvice(): bool
93
    {
94
        return $this->isExecuteAdvice;
95
    }
96
97 8
    /**
98
     * @return self
99 8
     */
100
    public function enableExecuteAdvice(): self
101 8
    {
102
        $this->isExecuteAdvice = true;
103
104
        return $this;
105
    }
106
107 8
    /**
108
     * @return self
109 8
     */
110
    public function disableExecuteAdvice(): self
111 8
    {
112
        $this->isExecuteAdvice = false;
113
114
        return $this;
115
    }
116
117 8
    /**
118
     * @return PolicyInformationPointInterface
119 8
     */
120
    protected function getPip(): PolicyInformationPointInterface
121
    {
122
        return $this->pip;
123
    }
124
125 8
    /**
126
     * @return PolicyDecisionPointInterface
127 8
     */
128
    protected function getPdp(): PolicyDecisionPointInterface
129
    {
130
        return $this->pdp;
131
    }
132
133
    /**
134
     * @param int $evaluation
135 8
     *
136
     * @return bool
137 8
     */
138
    protected function interpretEvaluation(int $evaluation): bool
139
    {
140
        return $evaluation === EvaluationEnum::PERMIT;
141
    }
142
}
143