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