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