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

AbstractRoleDescriptor::fromXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 55
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

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