Passed
Push — master ( c3368a...54de3d )
by Tim
02:23
created

SecurityTokenServiceType::fromXML()   B

Complexity

Conditions 3
Paths 2

Size

Total Lines 121
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

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