AbstractStatusType::getStatusDetail()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML11\Constants as C;
10
use SimpleSAML\SAML11\Exception\ProtocolViolationException;
11
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
12
13
use function array_pop;
14
15
/**
16
 * SAML Status data type.
17
 *
18
 * @package simplesamlphp/saml11
19
 */
20
abstract class AbstractStatusType extends AbstractSamlpElement
21
{
22
    /**
23
     * Initialize a samlp:Status
24
     *
25
     * @param \SimpleSAML\SAML11\XML\samlp\StatusCode $statusCode
26
     * @param \SimpleSAML\SAML11\XML\samlp\StatusMessage|null $statusMessage
27
     * @param \SimpleSAML\SAML11\XML\samlp\StatusDetail|null $statusDetail
28
     */
29
    final public function __construct(
30
        protected StatusCode $statusCode,
31
        protected ?StatusMessage $statusMessage = null,
32
        protected ?StatusDetail $statusDetail = null,
33
    ) {
34
        Assert::oneOf(
35
            $statusCode->getValue()->getValue(),
36
            C::$STATUS_CODES,
37
            'Invalid top-level status code:  %s',
38
            ProtocolViolationException::class,
39
        );
40
    }
41
42
43
    /**
44
     * Collect the StatusCode
45
     *
46
     * @return \SimpleSAML\SAML11\XML\samlp\StatusCode
47
     */
48
    public function getStatusCode(): StatusCode
49
    {
50
        return $this->statusCode;
51
    }
52
53
54
    /**
55
     * Collect the value of the statusMessage
56
     *
57
     * @return \SimpleSAML\SAML11\XML\samlp\StatusMessage|null
58
     */
59
    public function getStatusMessage(): ?StatusMessage
60
    {
61
        return $this->statusMessage;
62
    }
63
64
65
    /**
66
     * Collect the value of the statusDetails property
67
     *
68
     * @return \SimpleSAML\SAML11\XML\samlp\StatusDetail|null
69
     */
70
    public function getStatusDetail(): ?StatusDetail
71
    {
72
        return $this->statusDetail;
73
    }
74
75
76
    /**
77
     * Convert XML into a Status
78
     *
79
     * @param \DOMElement $xml The XML element we should load
80
     * @return static
81
     *
82
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
83
     *   if the qualified name of the supplied element is wrong
84
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
85
     *   if too many child-elements of a type are specified
86
     * @throws \SimpleSAML\XML\Exception\MissingElementException
87
     *   if one of the mandatory child-elements is missing
88
     */
89
    public static function fromXML(DOMElement $xml): static
90
    {
91
        Assert::same($xml->localName, 'Status', InvalidDOMElementException::class);
92
        Assert::same($xml->namespaceURI, Status::NS, InvalidDOMElementException::class);
93
94
        $statusCode = StatusCode::getChildrenOfClass($xml);
95
        Assert::minCount($statusCode, 1, MissingElementException::class);
96
        Assert::maxCount($statusCode, 1, TooManyElementsException::class);
97
98
        $statusMessage = StatusMessage::getChildrenOfClass($xml);
99
        Assert::maxCount($statusMessage, 1, TooManyElementsException::class);
100
101
        $statusDetail = StatusDetail::getChildrenOfClass($xml);
102
103
        return new static(
104
            array_pop($statusCode),
105
            array_pop($statusMessage),
106
            array_pop($statusDetail),
107
        );
108
    }
109
110
111
    /**
112
     * Convert this Status to XML.
113
     *
114
     * @param \DOMElement|null $parent The element we are converting to XML.
115
     * @return \DOMElement The XML element after adding the data corresponding to this Status.
116
     */
117
    public function toXML(?DOMElement $parent = null): DOMElement
118
    {
119
        $e = $this->instantiateParentElement($parent);
120
121
        $this->getStatusCode()->toXML($e);
122
123
        $this->getStatusMessage()?->toXML($e);
124
125
        $this->getStatusDetail()?->toXML($e);
126
127
        return $e;
128
    }
129
}
130