AuthzDecisionStatement::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
12
use SimpleSAML\SAML2\XML\Decision;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\MissingElementException;
15
use SimpleSAML\XML\Exception\SchemaViolationException;
16
use SimpleSAML\XML\Exception\TooManyElementsException;
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use ValueError;
20
21
use function array_pop;
22
use function sprintf;
23
24
/**
25
 * Class representing a SAML2 AuthzDecisionStatement
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
final class AuthzDecisionStatement extends AbstractStatementType implements SchemaValidatableElementInterface
30
{
31
    use SchemaValidatableElementTrait;
32
33
    /**
34
     * Initialize an AuthzDecisionStatement.
35
     *
36
     * @param string $resource
37
     * @param \SimpleSAML\SAML2\XML\Decision $decision
38
     * @param \SimpleSAML\SAML2\XML\saml\Action[] $action
39
     * @param \SimpleSAML\SAML2\XML\saml\Evidence|null $evidence
40
     */
41
    public function __construct(
42
        protected string $resource,
43
        protected Decision $decision,
44
        protected array $action,
45
        protected ?Evidence $evidence = null,
46
    ) {
47
        SAMLAssert::validURI($resource);
48
        Assert::maxCount($action, C::UNBOUNDED_LIMIT);
49
        Assert::allIsInstanceOf($action, Action::class, SchemaViolationException::class);
50
    }
51
52
53
    /**
54
     * Collect the value of the resource-property
55
     *
56
     * @return string
57
     */
58
    public function getResource(): string
59
    {
60
        return $this->resource;
61
    }
62
63
64
    /**
65
     * Collect the value of the decision-property
66
     *
67
     * @return \SimpleSAML\SAML2\XML\Decision
68
     */
69
    public function getDecision(): Decision
70
    {
71
        return $this->decision;
72
    }
73
74
75
    /**
76
     * Collect the value of the action-property
77
     *
78
     * @return array
79
     */
80
    public function getAction(): array
81
    {
82
        return $this->action;
83
    }
84
85
86
    /**
87
     * Collect the value of the evidence-property
88
     *
89
     * @return \SimpleSAML\SAML2\XML\saml\Evidence|null
90
     */
91
    public function getEvidence(): ?Evidence
92
    {
93
        return $this->evidence;
94
    }
95
96
97
    /**
98
     * Convert XML into an AuthzDecisionStatement
99
     *
100
     * @param \DOMElement $xml The XML element we should load
101
     *
102
     * @return static
103
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
104
     *   if the qualified name of the supplied element is wrong
105
     * @throws \SimpleSAML\XML\Exception\MissingElementException
106
     *   if one of the mandatory child-elements is missing
107
     * @throws \Exception if the authentication instant is not a valid timestamp.
108
     */
109
    public static function fromXML(DOMElement $xml): static
110
    {
111
        Assert::same($xml->localName, 'AuthzDecisionStatement', InvalidDOMElementException::class);
112
        Assert::same($xml->namespaceURI, AuthzDecisionStatement::NS, InvalidDOMElementException::class);
113
114
        $action = Action::getChildrenOfClass($xml);
115
        Assert::minCount(
116
            $action,
117
            1,
118
            'Missing <saml:Action> in <saml:AuthzDecisionStatement>',
119
            MissingElementException::class,
120
        );
121
122
        $evidence = Evidence::getChildrenOfClass($xml);
123
        Assert::maxCount(
124
            $evidence,
125
            1,
126
            'Too many <saml:Evidence> in <saml:AuthzDecisionStatement>',
127
            TooManyElementsException::class,
128
        );
129
130
        $decision = self::getAttribute($xml, 'Decision');
131
        try {
132
            $decision = Decision::from($decision);
133
        } catch (ValueError) {
134
            throw new ProtocolViolationException(sprintf('Unknown value \'%s\' for Decision attribute.', $decision));
0 ignored issues
show
Bug introduced by
$decision of type SimpleSAML\SAML2\XML\Decision is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

134
            throw new ProtocolViolationException(sprintf('Unknown value \'%s\' for Decision attribute.', /** @scrutinizer ignore-type */ $decision));
Loading history...
135
        }
136
137
        return new static(
138
            self::getAttribute($xml, 'Resource'),
139
            $decision,
140
            $action,
141
            array_pop($evidence),
142
        );
143
    }
144
145
146
    /**
147
     * Convert this AuthzDecisionStatement to XML.
148
     *
149
     * @param \DOMElement|null $parent The element we should append this AuthzDecisionStatement to.
150
     * @return \DOMElement
151
     */
152
    public function toXML(?DOMElement $parent = null): DOMElement
153
    {
154
        $e = $this->instantiateParentElement($parent);
155
156
        $e->setAttribute('Resource', $this->getResource());
157
        $e->setAttribute('Decision', $this->getDecision()->value);
158
159
        foreach ($this->getAction() as $action) {
160
            $action->toXML($e);
161
        }
162
163
        if ($this->getEvidence() !== null && !$this->getEvidence()->isEmptyElement()) {
164
            $this->getEvidence()->toXML($e);
165
        }
166
167
        return $e;
168
    }
169
}
170