Passed
Pull Request — master (#306)
by Tim
02:07
created

AbstractRoleDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 11
nc 1
nop 11
dl 0
loc 24
rs 9.9
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Utils;
11
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
12
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
13
use SimpleSAML\XML\Attribute as XMLAttribute;
14
use SimpleSAML\XML\Chunk;
15
use SimpleSAML\XML\Exception\InvalidDOMElementException;
16
use SimpleSAML\XML\Exception\SchemaViolationException;
17
use SimpleSAML\XML\Exception\TooManyElementsException;
18
use SimpleSAML\XML\Utils as XMLUtils;
19
20
use function array_pop;
21
use function count;
22
use function implode;
23
24
/**
25
 * Class representing a SAML2 RoleDescriptor element.
26
 *
27
 * @package simplesamlphp/saml2
28
 */
29
abstract class AbstractRoleDescriptor extends AbstractRoleDescriptorType implements ExtensionPointInterface
30
{
31
    use ExtensionPointTrait;
32
33
    /** @var string */
34
    public const LOCALNAME = 'RoleDescriptor';
35
36
37
    /**
38
     * Initialize a md:RoleDescriptor from scratch.
39
     *
40
     * @param string $type
41
     * @param string[] $protocolSupportEnumeration A set of URI specifying the protocols supported.
42
     * @param string|null $ID The ID for this document. Defaults to null.
43
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
44
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An Extensions object. Defaults to null.
46
     * @param string|null $errorURL An URI where to redirect users for support. Defaults to null.
47
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
48
     *   An array of KeyDescriptor elements. Defaults to an empty array.
49
     * @param \SimpleSAML\SAML2\XML\md\Organization|null $organization
50
     *   The organization running this entity. Defaults to null.
51
     * @param \SimpleSAML\SAML2\XML\md\ContactPerson[] $contactPerson
52
     *   An array of contacts for this entity. Defaults to an empty array.
53
     * @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...
54
     */
55
    public function __construct(
56
        protected string $type,
57
        array $protocolSupportEnumeration,
58
        ?string $ID = null,
59
        ?int $validUntil = null,
60
        ?string $cacheDuration = null,
61
        ?Extensions $extensions = null,
62
        ?string $errorURL = null,
63
        array $keyDescriptor = [],
64
        ?Organization $organization = null,
65
        array $contactPerson = [],
66
        array $namespacedAttributes = [],
67
    ) {
68
        parent::__construct(
69
            $protocolSupportEnumeration,
70
            $ID,
71
            $validUntil,
72
            $cacheDuration,
73
            $extensions,
74
            $errorURL,
75
            $keyDescriptor,
76
            $organization,
77
            $contactPerson,
78
            $namespacedAttributes
79
        );
80
    }
81
82
83
    /**
84
     * Return the xsi:type value corresponding this element.
85
     *
86
     * @return string
87
     */
88
    public function getXsiType(): string
89
    {
90
        return $this->type;
91
    }
92
93
94
    /**
95
     * Convert XML into an RoleDescriptor
96
     * @param \DOMElement $xml The XML element we should load
97
     * @return static
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
100
     */
101
    public static function fromXML(DOMElement $xml): static
102
    {
103
        Assert::same($xml->localName, 'RoleDescriptor', InvalidDOMElementException::class);
104
        Assert::same($xml->namespaceURI, C::NS_MD, InvalidDOMElementException::class);
105
        Assert::true(
106
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
107
            'Missing required xsi:type in <saml:RoleDescriptor> element.',
108
            SchemaViolationException::class
109
        );
110
111
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
112
        Assert::validQName($type, SchemaViolationException::class);
113
114
        // first, try to resolve the type to a full namespaced version
115
        $qname = explode(':', $type, 2);
116
        if (count($qname) === 2) {
117
            list($prefix, $element) = $qname;
118
        } else {
119
            $prefix = null;
120
            list($element) = $qname;
121
        }
122
        $ns = $xml->lookupNamespaceUri($prefix);
123
        $type = ($ns === null) ? $element : implode(':', [$ns, $element]);
124
125
        // now check if we have a handler registered for it
126
        $handler = Utils::getContainer()->getExtensionHandler($type);
127
        if ($handler === null) {
128
            // we don't have a handler, proceed with unknown RoleDescriptor
129
            $protocols = self::getAttribute($xml, 'protocolSupportEnumeration');
130
131
            $validUntil = self::getOptionalAttribute($xml, 'validUntil', null);
132
            $orgs = Organization::getChildrenOfClass($xml);
133
            Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
134
135
            $extensions = Extensions::getChildrenOfClass($xml);
136
            Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
137
138
            return new UnknownRoleDescriptor(
139
                new Chunk($xml),
140
                $type,
141
                preg_split('/[\s]+/', trim($protocols)),
142
                self::getOptionalAttribute($xml, 'ID', null),
143
                $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
144
                self::getOptionalAttribute($xml, 'cacheDuration', null),
145
                array_pop($extensions),
146
                self::getOptionalAttribute($xml, 'errorURL', null),
147
                KeyDescriptor::getChildrenOfClass($xml),
148
                array_pop($orgs),
149
                ContactPerson::getChildrenOfClass($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
163
    /**
164
     * Convert this RoleDescriptor to XML.
165
     *
166
     * @param \DOMElement|null $parent The element we are converting to XML.
167
     * @return \DOMElement The XML element after adding the data corresponding to this RoleDescriptor.
168
     */
169
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
170
    {
171
        $e = parent::toUnsignedXML($parent);
172
173
        $type = new XMLAttribute(C::NS_XSI, 'xsi', 'type', $this->getXsiType());
174
        $type->toXML($e);
175
176
        return $e;
177
    }
178
}
179