SecurityTokenServiceType::fromXML()   B
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 131
Code Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 90
c 2
b 0
f 0
dl 0
loc 131
rs 8.2181
cc 3
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 DateTimeImmutable;
8
use DOMElement;
9
use SimpleSAML\SAML2\Assert\Assert as SAMLAssert;
10
use SimpleSAML\SAML2\XML\md\{ContactPerson, Extensions, KeyDescriptor, Organization};
11
use SimpleSAML\WSSecurity\Assert\Assert;
12
use SimpleSAML\WSSecurity\Constants as C;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
use SimpleSAML\XML\Exception\TooManyElementsException;
16
use SimpleSAML\XMLSecurity\XML\ds\Signature;
17
18
use function preg_split;
19
20
/**
21
 * Class representing WS-federation SecurityTokenServiceType RoleDescriptor.
22
 *
23
 * @package simplesamlphp/ws-security
24
 */
25
final class SecurityTokenServiceType extends AbstractSecurityTokenServiceType
26
{
27
    /**
28
     * Convert XML into a SecurityTokenServiceType RoleDescriptor
29
     *
30
     * @param \DOMElement $xml The XML element we should load
31
     * @return static
32
     *
33
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
34
     *   if the qualified name of the supplied element is wrong
35
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
36
     *   if too many child-elements of a type are specified
37
     */
38
    public static function fromXML(DOMElement $xml): static
39
    {
40
        Assert::same($xml->localName, 'RoleDescriptor', InvalidDOMElementException::class);
41
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
42
43
        Assert::true(
44
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
45
            'Missing required xsi:type in <saml:RoleDescriptor> element.',
46
            SchemaViolationException::class,
47
        );
48
49
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
50
        Assert::validQName($type, SchemaViolationException::class);
51
        Assert::same($type, static::XSI_TYPE_PREFIX . ':' . static::XSI_TYPE_NAME,);
52
53
        $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
54
        $validUntil = self::getOptionalAttribute($xml, 'validUntil', null);
55
        SAMLAssert::nullOrValidDateTime($validUntil);
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
            preg_split('/[\s]+/', trim($protocols)),
139
            self::getOptionalAttribute($xml, 'ID', null),
140
            $validUntil !== null ? new DateTimeImmutable($validUntil) : null,
141
            self::getOptionalAttribute($xml, 'cacheDuration', null),
142
            array_pop($extensions),
143
            self::getOptionalAttribute($xml, 'errorURL', null),
144
            KeyDescriptor::getChildrenOfClass($xml),
145
            array_pop($orgs),
146
            ContactPerson::getChildrenOfClass($xml),
147
            self::getAttributesNSFromXML($xml),
148
            array_pop($logicalServiceNamesOffered),
149
            array_pop($tokenTypesOffered),
150
            array_pop($claimDialectsOffered),
151
            array_pop($claimTypesOffered),
152
            array_pop($claimTypesRequested),
153
            array_pop($automaticPseudonyms),
154
            array_pop($targetScopes),
155
            self::getOptionalAttribute($xml, 'ServiceDisplayName', null),
156
            self::getOptionalAttribute($xml, 'ServiceDescription', null),
157
            SecurityTokenServiceEndpoint::getChildrenOfClass($xml),
158
            SingleSignOutSubscriptionEndpoint::getChildrenOfClass($xml),
159
            SingleSignOutNotificationEndpoint::getChildrenOfClass($xml),
160
            PassiveRequestorEndpoint::getChildrenOfClass($xml),
161
        );
162
163
        if (!empty($signature)) {
164
            $securityTokenServiceType->setSignature($signature[0]);
165
            $securityTokenServiceType->setXML($xml);
166
        }
167
168
        return $securityTokenServiceType;
169
    }
170
}
171