Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

AbstractRoleDescriptor   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 52
dl 0
loc 136
rs 10
c 0
b 0
f 0
wmc 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\SAMLAnyURIListValue;
11
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
12
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
13
use SimpleSAML\SAML2\Utils;
14
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
15
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
16
use SimpleSAML\XML\Chunk;
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use SimpleSAML\XMLSchema\Constants as C_XSI;
20
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
21
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
22
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
23
use SimpleSAML\XMLSchema\Type\DurationValue;
24
use SimpleSAML\XMLSchema\Type\IDValue;
25
use SimpleSAML\XMLSchema\Type\QNameValue;
26
27
use function array_pop;
28
29
/**
30
 * Class representing a SAML2 RoleDescriptor element.
31
 *
32
 * @package simplesamlphp/saml2
33
 */
34
abstract class AbstractRoleDescriptor extends AbstractRoleDescriptorType implements
35
    ExtensionPointInterface,
36
    SchemaValidatableElementInterface
37
{
38
    use ExtensionPointTrait;
39
    use SchemaValidatableElementTrait;
40
41
42
    public const string LOCALNAME = 'RoleDescriptor';
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 42 at column 24
Loading history...
43
44
45
    /**
46
     * Initialize a md:RoleDescriptor from scratch.
47
     *
48
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
49
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIListValue $protocolSupportEnumeration
50
     *   A set of URI specifying the protocols supported.
51
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $ID The ID for this document. Defaults to null.
52
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil Unix time of validity for this document.
53
     *   Defaults to null.
54
     * @param \SimpleSAML\XMLSchema\Type\DurationValue|null $cacheDuration Maximum time this document can be cached.
55
     *   Defaults to null.
56
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
57
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $errorURL An URI where to redirect users for support.
58
     *   Defaults to null.
59
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
60
     *   An array of KeyDescriptor elements. Defaults to an empty array.
61
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
62
     *   The organization running this entity. Defaults to null.
63
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contactPerson
64
     *   An array of contacts for this entity. Defaults to an empty array.
65
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
66
     */
67
    public function __construct(
68
        protected QNameValue $type,
69
        SAMLAnyURIListValue $protocolSupportEnumeration,
70
        ?IDValue $ID = null,
71
        ?SAMLDateTimeValue $validUntil = null,
72
        ?DurationValue $cacheDuration = null,
73
        ?Extensions $extensions = null,
74
        ?SAMLAnyURIValue $errorURL = null,
75
        array $keyDescriptor = [],
76
        ?Organization $organization = null,
77
        array $contactPerson = [],
78
        array $namespacedAttributes = [],
79
    ) {
80
        parent::__construct(
81
            $protocolSupportEnumeration,
82
            $ID,
83
            $validUntil,
84
            $cacheDuration,
85
            $extensions,
86
            $errorURL,
87
            $keyDescriptor,
88
            $organization,
89
            $contactPerson,
90
            $namespacedAttributes,
91
        );
92
    }
93
94
95
    /**
96
     * Return the xsi:type value corresponding this element.
97
     *
98
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
99
     */
100
    public function getXsiType(): QNameValue
101
    {
102
        return $this->type;
103
    }
104
105
106
    /**
107
     * Convert XML into an RoleDescriptor
108
     *
109
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
110
     *   if the qualified name of the supplied element is wrong
111
     */
112
    public static function fromXML(DOMElement $xml): static
113
    {
114
        Assert::same($xml->localName, 'RoleDescriptor', InvalidDOMElementException::class);
115
        Assert::same($xml->namespaceURI, C::NS_MD, InvalidDOMElementException::class);
116
        Assert::true(
117
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
118
            'Missing required xsi:type in <md:RoleDescriptor> element.',
119
            SchemaViolationException::class,
120
        );
121
122
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
123
124
        // now check if we have a handler registered for it
125
        $handler = Utils::getContainer()->getExtensionHandler($type);
126
        if ($handler === null) {
127
            // we don't have a handler, proceed with unknown RoleDescriptor
128
            $orgs = Organization::getChildrenOfClass($xml);
129
            Assert::maxCount(
130
                $orgs,
131
                1,
132
                'More than one Organization found in this descriptor',
133
                TooManyElementsException::class,
134
            );
135
136
            $extensions = Extensions::getChildrenOfClass($xml);
137
            Assert::maxCount(
138
                $extensions,
139
                1,
140
                'Only one md:Extensions element is allowed.',
141
                TooManyElementsException::class,
142
            );
143
144
            return new UnknownRoleDescriptor(
145
                new Chunk($xml),
146
                $type,
147
                self::getAttribute($xml, 'protocolSupportEnumeration', SAMLAnyURIListValue::class),
148
                self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
149
                self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
150
                self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
151
                array_pop($extensions),
152
                self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
153
                KeyDescriptor::getChildrenOfClass($xml),
154
                array_pop($orgs),
155
                ContactPerson::getChildrenOfClass($xml),
156
                self::getAttributesNSFromXML($xml),
157
            );
158
        }
159
160
        Assert::subclassOf(
161
            $handler,
162
            AbstractRoleDescriptor::class,
163
            'Elements implementing RoleDescriptor must extend \SimpleSAML\SAML2\XML\saml\AbstractRoleDescriptor.',
164
        );
165
166
        return $handler::fromXML($xml);
167
    }
168
}
169