Attributes   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 42
dl 0
loc 135
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getIsFromNewLogin() 0 3 1
A getLongTermAuthenticationRequestTokenUsed() 0 3 1
A __construct() 0 7 1
A toXML() 0 16 3
A getAuthenticationDate() 0 3 1
A fromXML() 0 34 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML;
6
7
use DOMElement;
8
use SimpleSAML\CAS\Assert\Assert;
9
use SimpleSAML\CAS\Constants as C;
10
use SimpleSAML\XML\ExtendableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
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
25
    /** @var string */
26
    final public const LOCALNAME = 'attributes';
27
28
    /** The namespace-attribute for the xs:any element */
29
    final public const XS_ANY_ELT_NAMESPACE = NS::ANY;
30
31
    /** The exclusions for the xs:any element */
32
    final public const XS_ANY_ELT_EXCLUSIONS = [
33
        [C::NS_CAS, 'authenticationDate'],
34
        [C::NS_CAS, 'longTermAuthenticationRequestTokenUsed'],
35
        [C::NS_CAS, 'isFromNewLogin'],
36
    ];
37
38
39
    /**
40
     * Initialize a cas:attributes element
41
     *
42
     * @param \SimpleSAML\CAS\XML\AuthenticationDate $authenticationDate
43
     * @param \SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed
44
     * @param \SimpleSAML\CAS\XML\IsFromNewLogin $isFromNewLogin
45
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $elts
46
     */
47
    final public function __construct(
48
        protected AuthenticationDate $authenticationDate,
49
        protected LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed,
50
        protected IsFromNewLogin $isFromNewLogin,
51
        array $elts = [],
52
    ) {
53
        $this->setElements($elts);
54
    }
55
56
57
    /**
58
     * @return \SimpleSAML\CAS\XML\AuthenticationDate
59
     */
60
    public function getAuthenticationDate(): AuthenticationDate
61
    {
62
        return $this->authenticationDate;
63
    }
64
65
66
    /**
67
     * @return \SimpleSAML\CAS\XML\LongTermAuthenticationRequestTokenUsed
68
     */
69
    public function getLongTermAuthenticationRequestTokenUsed(): LongTermAuthenticationRequestTokenUsed
70
    {
71
        return $this->longTermAuthenticationRequestTokenUsed;
72
    }
73
74
75
    /**
76
     * @return \SimpleSAML\CAS\XML\IsFromNewLogin
77
     */
78
    public function getIsFromNewLogin(): IsFromNewLogin
79
    {
80
        return $this->isFromNewLogin;
81
    }
82
83
84
    /**
85
     * Convert XML into a cas:attributes-element
86
     *
87
     * @param \DOMElement $xml The XML element we should load
88
     * @return static
89
     *
90
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
91
     *   if the qualified name of the supplied element is wrong
92
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
93
     *   if the supplied element is missing one of the mandatory attributes
94
     */
95
    public static function fromXML(DOMElement $xml): static
96
    {
97
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
98
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
99
100
        $authenticationDate = AuthenticationDate::getChildrenOfClass($xml);
101
        Assert::count(
102
            $authenticationDate,
103
            1,
104
            'Exactly one <cas:authenticationDate> must be specified.',
105
            MissingElementException::class,
106
        );
107
108
        $longTermAuthenticationRequestTokenUsed = LongTermAuthenticationRequestTokenUsed::getChildrenOfClass($xml);
109
        Assert::count(
110
            $longTermAuthenticationRequestTokenUsed,
111
            1,
112
            'Exactly one <cas:longTermAuthenticationRequestTokenUsed> must be specified.',
113
            MissingElementException::class,
114
        );
115
116
        $isFromNewLogin = IsFromNewLogin::getChildrenOfClass($xml);
117
        Assert::count(
118
            $isFromNewLogin,
119
            1,
120
            'Exactly least one <cas:isFromNewLogin> must be specified.',
121
            MissingElementException::class,
122
        );
123
124
        return new static(
125
            $authenticationDate[0],
126
            $longTermAuthenticationRequestTokenUsed[0],
127
            $isFromNewLogin[0],
128
            self::getChildElementsFromXML($xml),
129
        );
130
    }
131
132
133
    /**
134
     * Convert this Attributes to XML.
135
     *
136
     * @param \DOMElement|null $parent The element we should append this Attributes to.
137
     * @return \DOMElement
138
     */
139
    public function toXML(?DOMElement $parent = null): DOMElement
140
    {
141
        $e = $this->instantiateParentElement($parent);
142
143
        $this->getAuthenticationDate()->toXML($e);
144
        $this->getLongTermAuthenticationRequestTokenUsed()->toXML($e);
145
        $this->getIsFromNewLogin()->toXML($e);
146
147
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
148
        foreach ($this->elements as $elt) {
149
            if (!$elt->isEmptyElement()) {
150
                $elt->toXML($e);
151
            }
152
        }
153
154
        return $e;
155
    }
156
}
157