AuthzDecisionStatement   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 134
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 16 4
A getResource() 0 3 1
A getEvidence() 0 3 1
A getDecision() 0 3 1
A fromXML() 0 27 1
A getAction() 0 3 1
A __construct() 0 9 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\DecisionTypeValue;
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Exception\MissingElementException;
15
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
16
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
17
use SimpleSAML\XMLSchema\Type\AnyURIValue;
18
19
use function array_pop;
20
use function strval;
21
22
/**
23
 * Class representing a SAML2 AuthzDecisionStatement
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
final class AuthzDecisionStatement extends AbstractStatementType implements SchemaValidatableElementInterface
28
{
29
    use SchemaValidatableElementTrait;
30
31
32
    /**
33
     * Initialize an AuthzDecisionStatement.
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $resource
36
     * @param \SimpleSAML\SAML2\Type\DecisionTypeValue $decision
37
     * @param \SimpleSAML\SAML2\XML\saml\Action[] $action
38
     * @param \SimpleSAML\SAML2\XML\saml\Evidence|null $evidence
39
     */
40
    public function __construct(
41
        // Uses the base AnyURIValue because the SAML-specification allows this attribute to be an empty string
42
        protected AnyURIValue $resource,
43
        protected DecisionTypeValue $decision,
44
        protected array $action,
45
        protected ?Evidence $evidence = null,
46
    ) {
47
        Assert::maxCount($action, C::UNBOUNDED_LIMIT);
48
        Assert::allIsInstanceOf($action, Action::class, SchemaViolationException::class);
49
    }
50
51
52
    /**
53
     * Collect the value of the resource-property
54
     *
55
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
56
     */
57
    public function getResource(): AnyURIValue
58
    {
59
        return $this->resource;
60
    }
61
62
63
    /**
64
     * Collect the value of the decision-property
65
     *
66
     * @return \SimpleSAML\SAML2\Type\DecisionTypeValue
67
     */
68
    public function getDecision(): DecisionTypeValue
69
    {
70
        return $this->decision;
71
    }
72
73
74
    /**
75
     * Collect the value of the action-property
76
     *
77
     * @return \SimpleSAML\SAML2\XML\saml\Action[]
78
     */
79
    public function getAction(): array
80
    {
81
        return $this->action;
82
    }
83
84
85
    /**
86
     * Collect the value of the evidence-property
87
     *
88
     * @return \SimpleSAML\SAML2\XML\saml\Evidence|null
89
     */
90
    public function getEvidence(): ?Evidence
91
    {
92
        return $this->evidence;
93
    }
94
95
96
    /**
97
     * Convert XML into an AuthzDecisionStatement
98
     *
99
     * @param \DOMElement $xml The XML element we should load
100
     *
101
     * @return static
102
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
103
     *   if the qualified name of the supplied element is wrong
104
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
105
     *   if one of the mandatory child-elements is missing
106
     * @throws \Exception if the authentication instant is not a valid timestamp.
107
     */
108
    public static function fromXML(DOMElement $xml): static
109
    {
110
        Assert::same($xml->localName, 'AuthzDecisionStatement', InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, AuthzDecisionStatement::NS, InvalidDOMElementException::class);
112
113
        $action = Action::getChildrenOfClass($xml);
114
        Assert::minCount(
115
            $action,
116
            1,
117
            'Missing <saml:Action> in <saml:AuthzDecisionStatement>',
118
            MissingElementException::class,
119
        );
120
121
        $evidence = Evidence::getChildrenOfClass($xml);
122
        Assert::maxCount(
123
            $evidence,
124
            1,
125
            'Too many <saml:Evidence> in <saml:AuthzDecisionStatement>',
126
            TooManyElementsException::class,
127
        );
128
129
130
        return new static(
131
            self::getAttribute($xml, 'Resource', AnyURIValue::class),
132
            self::getAttribute($xml, 'Decision', DecisionTypeValue::class),
133
            $action,
134
            array_pop($evidence),
135
        );
136
    }
137
138
139
    /**
140
     * Convert this AuthzDecisionStatement to XML.
141
     *
142
     * @param \DOMElement|null $parent The element we should append this AuthzDecisionStatement to.
143
     * @return \DOMElement
144
     */
145
    public function toXML(?DOMElement $parent = null): DOMElement
146
    {
147
        $e = $this->instantiateParentElement($parent);
148
149
        $e->setAttribute('Resource', strval($this->getResource()));
150
        $e->setAttribute('Decision', strval($this->getDecision()));
151
152
        foreach ($this->getAction() as $action) {
153
            $action->toXML($e);
154
        }
155
156
        if ($this->getEvidence() !== null && !$this->getEvidence()->isEmptyElement()) {
157
            $this->getEvidence()->toXML($e);
158
        }
159
160
        return $e;
161
    }
162
}
163