Completed
Push — master ( 6a434e...499da7 )
by Tim
16s queued 14s
created

Attributes::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 22
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 34
rs 9.568
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\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_pop;
16
17
/**
18
 * Class for CAS attributes
19
 *
20
 * @package simplesamlphp/cas
21
 */
22
final class Attributes extends AbstractCasElement
23
{
24
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\CAS\XML\cas\Attributes: $namespaceURI, $localName, $childNodes
Loading history...
25
26
    /** @var string */
27
    final public const LOCALNAME = 'attributes';
28
29
    /** The namespace-attribute for the xs:any element */
30
    final public const XS_ANY_ELT_NAMESPACE = NS::ANY;
31
32
    /** The exclusions for the xs:any element */
33
    final public const XS_ANY_ELT_EXCLUSIONS = [
34
        [C::NS_CAS, 'authenticationDate'],
35
        [C::NS_CAS, 'longTermAuthenticationRequestTokenUsed'],
36
        [C::NS_CAS, 'isFromNewLogin'],
37
    ];
38
39
40
    /**
41
     * Initialize a cas:attributes element
42
     *
43
     * @param \SimpleSAML\CAS\XML\cas\AuthenticationDate $authenticationDate
44
     * @param \SimpleSAML\CAS\XML\cas\LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed
45
     * @param \SimpleSAML\CAS\XML\cas\IsFromNewLogin $isFromNewLogin
46
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $elts
0 ignored issues
show
Bug introduced by
The type SimpleSAML\CAS\XML\cas\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
47
     */
48
    final public function __construct(
49
        protected AuthenticationDate $authenticationDate,
50
        protected LongTermAuthenticationRequestTokenUsed $longTermAuthenticationRequestTokenUsed,
51
        protected IsFromNewLogin $isFromNewLogin,
52
        array $elts = [],
53
    ) {
54
        $this->setElements($elts);
55
    }
56
57
58
    /**
59
     * @return \SimpleSAML\CAS\XML\cas\AuthenticationDate
60
     */
61
    public function getAuthenticationDate(): AuthenticationDate
62
    {
63
        return $this->authenticationDate;
64
    }
65
66
67
    /**
68
     * @return \SimpleSAML\CAS\XML\cas\LongTermAuthenticationRequestTokenUsed
69
     */
70
    public function getLongTermAuthenticationRequestTokenUsed(): LongTermAuthenticationRequestTokenUsed
71
    {
72
        return $this->longTermAuthenticationRequestTokenUsed;
73
    }
74
75
76
    /**
77
     * @return \SimpleSAML\CAS\XML\cas\IsFromNewLogin
78
     */
79
    public function getIsFromNewLogin(): IsFromNewLogin
80
    {
81
        return $this->isFromNewLogin;
82
    }
83
84
85
    /**
86
     * Convert XML into a cas:attributes-element
87
     *
88
     * @param \DOMElement $xml The XML element we should load
89
     * @return static
90
     *
91
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
92
     *   if the qualified name of the supplied element is wrong
93
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
94
     *   if the supplied element is missing one of the mandatory attributes
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
100
101
        $authenticationDate = AuthenticationDate::getChildrenOfClass($xml);
102
        Assert::count(
103
            $authenticationDate,
104
            1,
105
            'Exactly one <cas:authenticationDate> must be specified.',
106
            MissingElementException::class,
107
        );
108
109
        $longTermAuthenticationRequestTokenUsed = LongTermAuthenticationRequestTokenUsed::getChildrenOfClass($xml);
110
        Assert::count(
111
            $longTermAuthenticationRequestTokenUsed,
112
            1,
113
            'Exactly one <cas:longTermAuthenticationRequestTokenUsed> must be specified.',
114
            MissingElementException::class,
115
        );
116
117
        $isFromNewLogin = IsFromNewLogin::getChildrenOfClass($xml);
118
        Assert::count(
119
            $isFromNewLogin,
120
            1,
121
            'Exactly least one <cas:isFromNewLogin> must be specified.',
122
            MissingElementException::class,
123
        );
124
125
        return new static(
126
            array_pop($authenticationDate),
127
            array_pop($longTermAuthenticationRequestTokenUsed),
128
            array_pop($isFromNewLogin),
129
            self::getChildElementsFromXML($xml),
130
        );
131
    }
132
133
134
    /**
135
     * Convert this Attributes to XML.
136
     *
137
     * @param \DOMElement|null $parent The element we should append this Attributes to.
138
     * @return \DOMElement
139
     */
140
    public function toXML(DOMElement $parent = null): DOMElement
141
    {
142
        $e = $this->instantiateParentElement($parent);
143
144
        $this->getAuthenticationDate()->toXML($e);
145
        $this->getLongTermAuthenticationRequestTokenUsed()->toXML($e);
146
        $this->getIsFromNewLogin()->toXML($e);
147
148
        /** @psalm-var \SimpleSAML\XML\SerializableElementInterface $elt */
149
        foreach ($this->elements as $elt) {
150
            if (!$elt->isEmptyElement()) {
151
                $elt->toXML($e);
152
            }
153
        }
154
155
        return $e;
156
    }
157
}
158