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

AbstractRoleDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
        /** @scrutinizer ignore-call */ 
176
        $xsiType = $e->ownerDocument->createAttributeNS(C::NS_XSI, 'xsi:type');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
        $xsiType->value = $this->getXsiType();
177
        $e->setAttributeNodeNS($xsiType);
178
179
        return $e;
180
    }
181
}
182