Passed
Pull Request — master (#374)
by Tim
02:41
created

AttributeAuthorityDescriptor::fromXML()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 64
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 46
nc 2
nop 1
dl 0
loc 64
rs 9.1781
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Type\{AnyURIListValue, SAMLAnyURIValue, SAMLDateTimeValue};
10
use SimpleSAML\SAML2\XML\saml\Attribute;
11
use SimpleSAML\XML\Constants as C;
12
use SimpleSAML\XML\Exception\{InvalidDOMElementException, MissingElementException, TooManyElementsException};
13
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
14
use SimpleSAML\XML\Type\{DurationValue, IDValue};
15
use SimpleSAML\XMLSecurity\XML\ds\Signature;
16
17
use function preg_split;
18
19
/**
20
 * Class representing SAML 2 metadata AttributeAuthorityDescriptor.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class AttributeAuthorityDescriptor extends AbstractRoleDescriptorType implements
25
    SchemaValidatableElementInterface
26
{
27
    use SchemaValidatableElementTrait;
28
29
    /**
30
     * AttributeAuthorityDescriptor constructor.
31
     *
32
     * @param \SimpleSAML\SAML2\XML\md\AttributeService[] $attributeService
33
     * @param \SimpleSAML\SAML2\Type\AnyURIListValue $protocolSupportEnumeration
34
     * @param \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[] $assertionIDRequestService
35
     * @param \SimpleSAML\SAML2\XML\md\NameIDFormat[] $nameIDFormat
36
     * @param \SimpleSAML\SAML2\XML\md\AttributeProfile[] $attributeProfile
37
     * @param \SimpleSAML\SAML2\XML\saml\Attribute[] $attribute
38
     * @param \SimpleSAML\XML\Type\IDValue|null $ID
39
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil
40
     * @param \SimpleSAML\XML\Type\DurationValue|null $cacheDuration
41
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
42
     * @param \SimpleSAML\SAML2\Type\SAMLAnyURIValue|null $errorURL
43
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
44
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
45
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contact
46
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\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
    public function __construct(
49
        protected array $attributeService,
50
        AnyURIListValue $protocolSupportEnumeration,
51
        protected array $assertionIDRequestService = [],
52
        protected array $nameIDFormat = [],
53
        protected array $attributeProfile = [],
54
        protected array $attribute = [],
55
        ?IDValue $ID = null,
56
        ?SAMLDateTimeValue $validUntil = null,
57
        ?DurationValue $cacheDuration = null,
58
        ?Extensions $extensions = null,
59
        ?SAMLAnyURIValue $errorURL = null,
60
        ?Organization $organization = null,
61
        array $keyDescriptor = [],
62
        array $contact = [],
63
        array $namespacedAttributes = [],
64
    ) {
65
        Assert::maxCount($attributeService, C::UNBOUNDED_LIMIT);
66
        Assert::minCount(
67
            $attributeService,
68
            1,
69
            'AttributeAuthorityDescriptor must contain at least one AttributeService.',
70
            MissingElementException::class,
71
        );
72
        Assert::allIsInstanceOf(
73
            $attributeService,
74
            AttributeService::class,
75
            'AttributeService is not an instance of EndpointType.',
76
            InvalidDOMElementException::class,
77
        );
78
        Assert::maxCount($nameIDFormat, C::UNBOUNDED_LIMIT);
79
        Assert::allIsInstanceOf($nameIDFormat, NameIDFormat::class);
80
        Assert::maxCount($assertionIDRequestService, C::UNBOUNDED_LIMIT);
81
        Assert::allIsInstanceOf($assertionIDRequestService, AssertionIDRequestService::class);
82
        Assert::maxCount($attributeProfile, C::UNBOUNDED_LIMIT);
83
        Assert::allIsInstanceOf($attributeProfile, AttributeProfile::class);
84
        Assert::maxCount($attribute, C::UNBOUNDED_LIMIT);
85
        Assert::allIsInstanceOf($attribute, Attribute::class);
86
87
        parent::__construct(
88
            $protocolSupportEnumeration,
89
            $ID,
90
            $validUntil,
91
            $cacheDuration,
92
            $extensions,
93
            $errorURL,
94
            $keyDescriptor,
95
            $organization,
96
            $contact,
97
            $namespacedAttributes,
98
        );
99
    }
100
101
102
    /**
103
     * Collect the value of the AttributeService-property
104
     *
105
     * @return \SimpleSAML\SAML2\XML\md\AttributeService[]
106
     */
107
    public function getAttributeService(): array
108
    {
109
        return $this->attributeService;
110
    }
111
112
113
    /**
114
     * Collect the value of the NameIDFormat-property
115
     *
116
     * @return \SimpleSAML\SAML2\XML\md\NameIDFormat[]
117
     */
118
    public function getNameIDFormat(): array
119
    {
120
        return $this->nameIDFormat;
121
    }
122
123
124
    /**
125
     * Collect the value of the AssertionIDRequestService-property
126
     *
127
     * @return \SimpleSAML\SAML2\XML\md\AssertionIDRequestService[]
128
     */
129
    public function getAssertionIDRequestService(): array
130
    {
131
        return $this->assertionIDRequestService;
132
    }
133
134
135
    /**
136
     * Collect the value of the AttributeProfile-property
137
     *
138
     * @return \SimpleSAML\SAML2\XML\md\AttributeProfile[]
139
     */
140
    public function getAttributeProfile(): array
141
    {
142
        return $this->attributeProfile;
143
    }
144
145
146
    /**
147
     * Collect the value of the Attribute-property
148
     *
149
     * @return \SimpleSAML\SAML2\XML\saml\Attribute[]
150
     */
151
    public function getAttributes(): array
152
    {
153
        return $this->attribute;
154
    }
155
156
157
    /**
158
     * Initialize an IDPSSODescriptor.
159
     *
160
     * @param \DOMElement $xml The XML element we should load.
161
     * @return static
162
     *
163
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
164
     *   if the qualified name of the supplied element is wrong
165
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
166
     *   if the supplied element is missing one of the mandatory attributes
167
     * @throws \SimpleSAML\XML\Exception\MissingElementException
168
     *   if one of the mandatory child-elements is missing
169
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
170
     *   if too many child-elements of a type are specified
171
     */
172
    public static function fromXML(DOMElement $xml): static
173
    {
174
        Assert::same($xml->localName, 'AttributeAuthorityDescriptor', InvalidDOMElementException::class);
175
        Assert::same($xml->namespaceURI, AttributeAuthorityDescriptor::NS, InvalidDOMElementException::class);
176
177
        $attrServices = AttributeService::getChildrenOfClass($xml);
178
        Assert::notEmpty(
179
            $attrServices,
180
            'Must have at least one AttributeService in AttributeAuthorityDescriptor.',
181
            MissingElementException::class,
182
        );
183
184
        $assertIDReqServices = AssertionIDRequestService::getChildrenOfClass($xml);
185
        $nameIDFormats = NameIDFormat::getChildrenOfClass($xml);
186
        $attrProfiles = AttributeProfile::getChildrenOfClass($xml);
187
        $attributes = Attribute::getChildrenOfClass($xml);
188
189
        $orgs = Organization::getChildrenOfClass($xml);
190
        Assert::maxCount(
191
            $orgs,
192
            1,
193
            'More than one Organization found in this descriptor',
194
            TooManyElementsException::class,
195
        );
196
197
        $extensions = Extensions::getChildrenOfClass($xml);
198
        Assert::maxCount(
199
            $extensions,
200
            1,
201
            'Only one md:Extensions element is allowed.',
202
            TooManyElementsException::class,
203
        );
204
205
        $signature = Signature::getChildrenOfClass($xml);
206
        Assert::maxCount(
207
            $signature,
208
            1,
209
            'Only one ds:Signature element is allowed.',
210
            TooManyElementsException::class,
211
        );
212
213
        $authority = new static(
214
            $attrServices,
215
            self::getAttribute($xml, 'protocolSupportEnumeration', AnyURIListValue::class),
216
            $assertIDReqServices,
217
            $nameIDFormats,
218
            $attrProfiles,
219
            $attributes,
220
            self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
221
            self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
222
            self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
223
            !empty($extensions) ? $extensions[0] : null,
224
            self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
225
            !empty($orgs) ? $orgs[0] : null,
226
            KeyDescriptor::getChildrenOfClass($xml),
227
            ContactPerson::getChildrenOfClass($xml),
228
            self::getAttributesNSFromXML($xml),
229
        );
230
231
        if (!empty($signature)) {
232
            $authority->setSignature($signature[0]);
233
            $authority->setXML($xml);
234
        }
235
        return $authority;
236
    }
237
238
239
    /**
240
     * Convert this assertion to an unsigned XML document.
241
     * This method does not sign the resulting XML document.
242
     *
243
     * @return \DOMElement The root element of the DOM tree
244
     */
245
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
246
    {
247
        $e = parent::toUnsignedXML($parent);
248
249
        foreach ($this->getAttributeService() as $ep) {
250
            $ep->toXML($e);
251
        }
252
253
        foreach ($this->getAssertionIDRequestService() as $ep) {
254
            $ep->toXML($e);
255
        }
256
257
        foreach ($this->getNameIDFormat() as $nidFormat) {
258
            $nidFormat->toXML($e);
259
        }
260
261
        foreach ($this->getAttributeProfile() as $ap) {
262
            $ap->toXML($e);
263
        }
264
265
        foreach ($this->getAttributes() as $a) {
266
            $a->toXML($e);
267
        }
268
269
        return $e;
270
    }
271
}
272