Passed
Push — master ( 768e90...be6a83 )
by Tim
01:37
created

Attributes::fromXML()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 50
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 33
nc 7
nop 1
dl 0
loc 50
rs 8.4586
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML\cas;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\CAS\Constants as C;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
15
/**
16
 * Class for CAS attributes
17
 *
18
 * @package simplesamlphp/cas
19
 */
20
final class Attributes extends AbstractCasElement
21
{
22
    use ExtendableElementTrait;
23
24
    /** @var string */
25
    public const LOCALNAME = 'attributes';
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const NAMESPACE = C::XS_ANY_NS_ANY;
29
30
31
    /**
32
     * Initialize a cas:attributes element
33
     *
34
     * @param \SimpleSAML\CAS\XML\cas\AuthenticationDate $authenticationDate
35
     * @param \SimpleSAML\CAS\XML\cas\LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed
36
     * @param \SimpleSAML\CAS\XML\cas\IsFromNewLogin $isFromNewLogin
37
     * @param \SimpleSAML\XML\Chunk[] $elts
38
     */
39
    final public function __construct(
40
        protected AuthenticationDate $authenticationDate,
41
        protected LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed,
42
        protected IsFromNewLogin $isFromNewLogin,
43
        array $elts = [],
44
    ) {
45
        $this->setElements($elts);
46
    }
47
48
49
    /**
50
     * @return \SimpleSAML\CAS\XML\cas\AuthenticationDate
51
     */
52
    public function getAuthenticationDate(): AuthenticationDate
53
    {
54
        return $this->authenticationDate;
55
    }
56
57
58
    /**
59
     * @return \SimpleSAML\CAS\XML\cas\LongTermAuthenticationRequestTokenUsed
60
     */
61
    public function getLongTermAuthenticationRequestTokenUsed(): LongTermAuthenticationRequestTokenUsed
62
    {
63
        return $this->longTermAuthenticationRequestTokenUsed;
64
    }
65
66
67
    /**
68
     * @return \SimpleSAML\CAS\XML\cas\IsFromNewLogin
69
     */
70
    public function getIsFromNewLogin(): IsFromNewLogin
71
    {
72
        return $this->isFromNewLogin;
73
    }
74
75
76
    /**
77
     * Convert XML into a cas:attributes-element
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\MissingAttributeException
85
     *   if the supplied element is missing one of the mandatory attributes
86
     */
87
    public static function fromXML(DOMElement $xml): static
88
    {
89
        Assert::same($xml->localName, 'attributes', InvalidDOMElementException::class);
90
        Assert::same($xml->namespaceURI, Attributes::NS, InvalidDOMElementException::class);
91
92
        $authenticationDate = AuthenticationDate::getChildrenOfClass($xml);
93
        Assert::count(
94
            $authenticationDate,
95
            1,
96
            'Exactly one <cas:authenticationDate> must be specified.',
97
            MissingElementException::class,
98
        );
99
100
        $longTermAuthenticationRequestTokenUsed = LongTermAuthenticationRequestTokenUsed::getChildrenOfClass($xml);
101
        Assert::count(
102
            $longTermAuthenticationRequestTokenUsed,
103
            1,
104
            'Exactly one <cas:longTermAuthenticationRequestTokenUsed> must be specified.',
105
            MissingElementException::class,
106
        );
107
108
        $isFromNewLogin = IsFromNewLogin::getChildrenOfClass($xml);
109
        Assert::count(
110
            $isFromNewLogin,
111
            1,
112
            'Exactly least one <cas:isFromNewLogin> must be specified.',
113
            MissingElementException::class,
114
        );
115
116
        $elts = [];
117
        foreach ($xml->childNodes as $elt) {
118
            if (!($elt instanceof DOMElement)) {
119
                continue;
120
            } elseif ($elt->namespaceURI === C::NS_CAS) {
121
                switch ($elt->localName) {
122
                    case 'authenticationDate':
123
                    case 'longTermAuthenticationRequestTokenUsed':
124
                    case 'isFromNewLogin':
125
                        continue 2;
126
                }
127
            }
128
129
            $elts[] = new Chunk($elt);
130
        }
131
132
        return new static(
133
            array_pop($authenticationDate),
134
            array_pop($longTermAuthenticationRequestTokenUsed),
135
            array_pop($isFromNewLogin),
136
            $elts,
137
        );
138
    }
139
140
141
    /**
142
     * Convert this Attributes to XML.
143
     *
144
     * @param \DOMElement|null $parent The element we should append this Attributes to.
145
     * @return \DOMElement
146
     */
147
    public function toXML(DOMElement $parent = null): DOMElement
148
    {
149
        $e = $this->instantiateParentElement($parent);
150
151
        $this->getAuthenticationDate()->toXML($e);
152
        $this->getLongTermAuthenticationRequestTokenUsed()->toXML($e);
153
        $this->getIsFromNewLogin()->toXML($e);
154
155
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
156
        foreach ($this->elements as $elt) {
157
            if (!$elt->isEmptyElement()) {
158
                $elt->toXML($e);
159
            }
160
        }
161
162
        return $e;
163
    }
164
}
165