SecurityTokenServiceType::fromXML()   B
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 126
Code Lines 86

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 86
c 3
b 0
f 0
dl 0
loc 126
rs 8.3054
cc 2
nc 2
nop 1

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\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Type\SAMLAnyURIListValue;
9
use SimpleSAML\SAML2\Type\SAMLAnyURIValue;
10
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
11
use SimpleSAML\SAML2\Type\SAMLStringValue;
12
use SimpleSAML\SAML2\XML\md\ContactPerson;
13
use SimpleSAML\SAML2\XML\md\Extensions;
14
use SimpleSAML\SAML2\XML\md\KeyDescriptor;
15
use SimpleSAML\SAML2\XML\md\Organization;
16
use SimpleSAML\WSSecurity\Assert\Assert;
17
use SimpleSAML\XMLSchema\Constants as C;
18
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
19
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
20
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
21
use SimpleSAML\XMLSchema\Type\DurationValue;
22
use SimpleSAML\XMLSchema\Type\IDValue;
23
use SimpleSAML\XMLSchema\Type\QNameValue;
24
use SimpleSAML\XMLSecurity\XML\ds\Signature;
25
26
/**
27
 * Class representing WS-federation SecurityTokenServiceType RoleDescriptor.
28
 *
29
 * @package simplesamlphp/ws-security
30
 */
31
final class SecurityTokenServiceType extends AbstractSecurityTokenServiceType
32
{
33
    /**
34
     * Convert XML into a SecurityTokenServiceType RoleDescriptor
35
     *
36
     * @param \DOMElement $xml The XML element we should load
37
     * @return static
38
     *
39
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
40
     *   if the qualified name of the supplied element is wrong
41
     * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException
42
     *   if too many child-elements of a type are specified
43
     */
44
    public static function fromXML(DOMElement $xml): static
45
    {
46
        Assert::same($xml->localName, 'RoleDescriptor', InvalidDOMElementException::class);
47
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
48
49
        Assert::true(
50
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
51
            'Missing required xsi:type in <saml:RoleDescriptor> element.',
52
            SchemaViolationException::class,
53
        );
54
55
        $type = QNameValue::fromDocument($xml->getAttributeNS(C::NS_XSI, 'type'), $xml);
56
57
        $orgs = Organization::getChildrenOfClass($xml);
58
        Assert::maxCount(
59
            $orgs,
60
            1,
61
            'More than one Organization found in this descriptor',
62
            TooManyElementsException::class,
63
        );
64
65
        $extensions = Extensions::getChildrenOfClass($xml);
66
        Assert::maxCount(
67
            $extensions,
68
            1,
69
            'Only one md:Extensions element is allowed.',
70
            TooManyElementsException::class,
71
        );
72
73
        $logicalServiceNamesOffered = LogicalServiceNamesOffered::getChildrenOfClass($xml);
74
        Assert::maxCount(
75
            $logicalServiceNamesOffered,
76
            1,
77
            'Only one fed:LogicalServiceNamesOffered is allowed.',
78
            TooManyElementsException::class,
79
        );
80
81
        $tokenTypesOffered = TokenTypesOffered::getChildrenOfClass($xml);
82
        Assert::maxCount(
83
            $tokenTypesOffered,
84
            1,
85
            'Only one fed:TokenTypesOffered is allowed.',
86
            TooManyElementsException::class,
87
        );
88
89
        $claimDialectsOffered = ClaimDialectsOffered::getChildrenOfClass($xml);
90
        Assert::maxCount(
91
            $claimDialectsOffered,
92
            1,
93
            'Only one fed:ClaimDialectsOffered is allowed.',
94
            TooManyElementsException::class,
95
        );
96
97
        $claimTypesOffered = ClaimTypesOffered::getChildrenOfClass($xml);
98
        Assert::maxCount(
99
            $claimTypesOffered,
100
            1,
101
            'Only one fed:ClaimTypesOffered is allowed.',
102
            TooManyElementsException::class,
103
        );
104
105
        $claimTypesRequested = ClaimTypesRequested::getChildrenOfClass($xml);
106
        Assert::maxCount(
107
            $claimTypesRequested,
108
            1,
109
            'Only one fed:ClaimTypesRequested is allowed.',
110
            TooManyElementsException::class,
111
        );
112
113
        $automaticPseudonyms = AutomaticPseudonyms::getChildrenOfClass($xml);
114
        Assert::maxCount(
115
            $automaticPseudonyms,
116
            1,
117
            'Only one fed:AutomaticPseudonyms is allowed.',
118
            TooManyElementsException::class,
119
        );
120
121
        $targetScopes = TargetScopes::getChildrenOfClass($xml);
122
        Assert::maxCount(
123
            $targetScopes,
124
            1,
125
            'Only one fed:TargetScopes is allowed.',
126
            TooManyElementsException::class,
127
        );
128
129
        $signature = Signature::getChildrenOfClass($xml);
130
        Assert::maxCount(
131
            $signature,
132
            1,
133
            'Only one ds:Signature element is allowed.',
134
            TooManyElementsException::class,
135
        );
136
137
        $securityTokenServiceType = new static(
138
            $type,
139
            self::getAttribute($xml, 'protocolSupportEnumeration', SAMLAnyURIListValue::class),
140
            self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
141
            self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
142
            self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
143
            array_pop($extensions),
144
            self::getOptionalAttribute($xml, 'errorURL', SAMLAnyURIValue::class, null),
145
            KeyDescriptor::getChildrenOfClass($xml),
146
            array_pop($orgs),
147
            ContactPerson::getChildrenOfClass($xml),
148
            self::getAttributesNSFromXML($xml),
149
            array_pop($logicalServiceNamesOffered),
150
            array_pop($tokenTypesOffered),
151
            array_pop($claimDialectsOffered),
152
            array_pop($claimTypesOffered),
153
            array_pop($claimTypesRequested),
154
            array_pop($automaticPseudonyms),
155
            array_pop($targetScopes),
156
            self::getOptionalAttribute($xml, 'ServiceDisplayName', SAMLStringValue::class, null),
157
            self::getOptionalAttribute($xml, 'ServiceDescription', SAMLStringValue::class, null),
158
            SecurityTokenServiceEndpoint::getChildrenOfClass($xml),
159
            SingleSignOutSubscriptionEndpoint::getChildrenOfClass($xml),
160
            SingleSignOutNotificationEndpoint::getChildrenOfClass($xml),
161
            PassiveRequestorEndpoint::getChildrenOfClass($xml),
162
        );
163
164
        if (!empty($signature)) {
165
            $securityTokenServiceType->setSignature($signature[0]);
166
            $securityTokenServiceType->setXML($xml);
167
        }
168
169
        return $securityTokenServiceType;
170
    }
171
}
172