Completed
Push — master ( da4b66...3ae217 )
by Tim
19s queued 15s
created

AbstractStatusType   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 28
c 1
b 0
f 0
dl 0
loc 113
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 11 1
A fromXML() 0 18 1
A getStatusMessage() 0 3 1
A getStatusCode() 0 3 1
A __construct() 0 15 1
A getStatusDetail() 0 3 1
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\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\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(),
38
            [
39
                C::STATUS_SUCCESS,
40
                C::STATUS_REQUESTER,
41
                C::STATUS_RESPONDER,
42
                C::STATUS_VERSION_MISMATCH,
43
            ],
44
            'Invalid top-level status code:  %s',
45
            ProtocolViolationException::class,
46
        );
47
    }
48
49
50
    /**
51
     * Collect the StatusCode
52
     *
53
     * @return \SimpleSAML\SAML11\XML\samlp\StatusCode
54
     */
55
    public function getStatusCode(): StatusCode
56
    {
57
        return $this->statusCode;
58
    }
59
60
61
    /**
62
     * Collect the value of the statusMessage
63
     *
64
     * @return \SimpleSAML\SAML11\XML\samlp\StatusMessage|null
65
     */
66
    public function getStatusMessage(): ?StatusMessage
67
    {
68
        return $this->statusMessage;
69
    }
70
71
72
    /**
73
     * Collect the value of the statusDetails property
74
     *
75
     * @return \SimpleSAML\SAML11\XML\samlp\StatusDetail|null
76
     */
77
    public function getStatusDetail(): ?StatusDetail
78
    {
79
        return $this->statusDetail;
80
    }
81
82
83
    /**
84
     * Convert XML into a Status
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
92
     *   if too many child-elements of a type are specified
93
     * @throws \SimpleSAML\XML\Exception\MissingElementException
94
     *   if one of the mandatory child-elements is missing
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, 'Status', InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, Status::NS, InvalidDOMElementException::class);
100
101
        $statusCode = StatusCode::getChildrenOfClass($xml);
102
        Assert::minCount($statusCode, 1, MissingElementException::class);
103
        Assert::maxCount($statusCode, 1, TooManyElementsException::class);
104
105
        $statusMessage = StatusMessage::getChildrenOfClass($xml);
106
        Assert::maxCount($statusMessage, 1, TooManyElementsException::class);
107
108
        $statusDetail = StatusDetail::getChildrenOfClass($xml);
109
110
        return new static(
111
            array_pop($statusCode),
112
            array_pop($statusMessage),
113
            array_pop($statusDetail),
114
        );
115
    }
116
117
118
    /**
119
     * Convert this Status to XML.
120
     *
121
     * @param \DOMElement|null $parent The element we are converting to XML.
122
     * @return \DOMElement The XML element after adding the data corresponding to this Status.
123
     */
124
    public function toXML(?DOMElement $parent = null): DOMElement
125
    {
126
        $e = $this->instantiateParentElement($parent);
127
128
        $this->getStatusCode()->toXML($e);
129
130
        $this->getStatusMessage()?->toXML($e);
131
132
        $this->getStatusDetail()?->toXML($e);
133
134
        return $e;
135
    }
136
}
137