Completed
Push — feature/pdp-cbac ( e99222 )
by
unknown
13:10
created

PolicyDecision::fromResponse()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 23
rs 8.5906
cc 6
eloc 12
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright 2017 SURFnet B.V.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupGateway\GatewayBundle\Pdp;
20
21
use Surfnet\StepupGateway\GatewayBundle\Pdp\Dto\Response;
22
use Surfnet\StepupGateway\GatewayBundle\Exception\RuntimeException;
23
24
final class PolicyDecision implements PolicyDecisionInterface
25
{
26
    const DECISION_DENY = 'Deny';
27
    const DECISION_INDETERMINATE = 'Indeterminate';
28
    const DECISION_NOT_APPLICABLE = 'NotApplicable';
29
    const DECISION_PERMIT = 'Permit';
30
31
    /**
32
     * @var string
33
     */
34
    private $decision;
35
36
    /**
37
     * @var string|null
38
     */
39
    private $statusMessage;
40
41
    /**
42
     * @var string
43
     */
44
    private $statusCode;
45
46
    /**
47
     * @var string[]
48
     */
49
    public $loaObligations = [];
50
51
    /**
52
     * @param Response $response
53
     * @return PolicyDecision
54
     */
55
    public static function fromResponse(Response $response)
56
    {
57
        $policyDecision = new self;
58
        $policyDecision->decision = $response->decision;
59
60
        $policyDecision->statusCode = $response->status->statusCode->value;
61
62
        if (isset($response->status->statusMessage)) {
63
            $policyDecision->statusMessage = $response->status->statusMessage;
64
        }
65
66
        if (isset($response->obligations)) {
67
            foreach ($response->obligations as $obligation) {
68
                foreach ($obligation->attributeAssignments as $assignment) {
69
                    if ($assignment->attributeId === 'urn:loa:level') {
70
                        $policyDecision->loaObligations[] = $assignment->value;
71
                    }
72
                }
73
            }
74
        }
75
76
        return $policyDecision;
77
    }
78
79
    /**
80
     * @return bool
81
     */
82
    public function permitsAccess()
83
    {
84
        return $this->decision === self::DECISION_PERMIT || $this->decision === self::DECISION_NOT_APPLICABLE;
85
    }
86
87
    /**
88
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
89
     */
90
    public function getStatusMessage()
91
    {
92
        if (!$this->hasStatusMessage()) {
93
            throw new RuntimeException('No status message found');
94
        }
95
96
        return $this->statusMessage;
97
    }
98
99
    /**
100
     * Get the status message or status code of the decision.
101
     *
102
     * If no status message was present in the response this method will
103
     * return the status code instead.
104
     *
105
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
106
     */
107
    public function getFormattedStatus()
108
    {
109
        if ($this->hasStatusMessage()) {
110
            return $this->statusMessage;
111
        }
112
113
        return $this->statusCode;
114
    }
115
116
    /**
117
     * @return bool
118
     */
119
    public function hasStatusMessage()
120
    {
121
        return isset($this->statusMessage);
122
    }
123
124
    /**
125
     * @return bool
126
     */
127
    public function hasLoaObligations()
128
    {
129
        return (bool) count($this->loaObligations);
130
    }
131
132
    /**
133
     * @return string[]
134
     */
135
    public function getLoaObligations()
136
    {
137
        return $this->loaObligations;
138
    }
139
}
140