Passed
Pull Request — master (#280)
by Tim
02:24
created

EntitiesDescriptor::__construct()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 20
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 1
nop 8
dl 0
loc 20
rs 10
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\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\ProtocolViolationException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\Exception...tocolViolationException 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...
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\Utils as XMLUtils;
13
use SimpleSAML\XMLSecurity\XML\ds\Signature;
14
15
/**
16
 * Class representing SAML 2 EntitiesDescriptor element.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
final class EntitiesDescriptor extends AbstractMetadataDocument
21
{
22
    /**
23
     * The name of this entity collection.
24
     *
25
     * @var string|null
26
     */
27
    protected ?string $Name = null;
28
29
    /** @var \SimpleSAML\SAML2\XML\md\EntityDescriptor[] */
30
    protected array $entityDescriptors = [];
31
32
    /** @var \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] */
33
    protected array $entitiesDescriptors = [];
34
35
36
    /**
37
     * EntitiesDescriptor constructor.
38
     *
39
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
40
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
41
     * @param string|null $name
42
     * @param string|null $ID
43
     * @param int|null $validUntil
44
     * @param string|null $cacheDuration
45
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions
46
     * @param \DOMAttr[] $namespacedAttributes
47
     */
48
    public function __construct(
49
        array $entityDescriptors = [],
50
        array $entitiesDescriptors = [],
51
        ?string $name = null,
52
        ?string $ID = null,
53
        ?int $validUntil = null,
54
        ?string $cacheDuration = null,
55
        ?Extensions $extensions = null,
56
        array $namespacedAttributes = []
57
    ) {
58
        Assert::true(
59
            !empty($entitiesDescriptors) || !empty($entityDescriptors),
60
            'At least one md:EntityDescriptor or md:EntitiesDescriptor element is required.'
61
        );
62
63
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
64
65
        $this->setName($name);
66
        $this->setEntityDescriptors($entityDescriptors);
67
        $this->setEntitiesDescriptors($entitiesDescriptors);
68
    }
69
70
71
    /**
72
     * Initialize an EntitiesDescriptor from an existing XML document.
73
     *
74
     * @param \DOMElement $xml The XML element we should load.
75
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor
76
     *
77
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
78
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
79
     */
80
    public static function fromXML(DOMElement $xml): object
81
    {
82
        Assert::same($xml->localName, 'EntitiesDescriptor', InvalidDOMElementException::class);
83
        Assert::same($xml->namespaceURI, EntitiesDescriptor::NS, InvalidDOMElementException::class);
84
85
        $validUntil = self::getAttribute($xml, 'validUntil', null);
86
        $orgs = Organization::getChildrenOfClass($xml);
87
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
88
89
        $extensions = Extensions::getChildrenOfClass($xml);
90
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
91
92
        $signature = Signature::getChildrenOfClass($xml);
93
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
94
95
        $entities = new self(
96
            EntityDescriptor::getChildrenOfClass($xml),
97
            EntitiesDescriptor::getChildrenOfClass($xml),
98
            self::getAttribute($xml, 'Name', null),
99
            self::getAttribute($xml, 'ID', null),
100
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
101
            self::getAttribute($xml, 'cacheDuration', null),
102
            !empty($extensions) ? $extensions[0] : null,
103
            self::getAttributesNSFromXML($xml)
104
        );
105
106
        if (!empty($signature)) {
107
            $entities->setSignature($signature[0]);
108
        }
109
110
        $entities->setXML($xml);
111
112
        return $entities;
113
    }
114
115
116
    /**
117
     * Get the EntitiesDescriptor children objects
118
     *
119
     * @return \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[]
120
     */
121
    public function getEntitiesDescriptors(): array
122
    {
123
        return $this->entitiesDescriptors;
124
    }
125
126
127
    /**
128
     * Set the EntitiesDescriptor children objects
129
     *
130
     * @param \SimpleSAML\SAML2\XML\md\EntitiesDescriptor[] $entitiesDescriptors
131
     */
132
    protected function setEntitiesDescriptors(array $entitiesDescriptors): void
133
    {
134
        Assert::allIsInstanceOf($entitiesDescriptors, EntitiesDescriptor::class);
135
        $this->entitiesDescriptors = $entitiesDescriptors;
136
    }
137
138
139
    /**
140
     * Get the EntityDescriptor children objects
141
     *
142
     * @return \SimpleSAML\SAML2\XML\md\EntityDescriptor[]
143
     */
144
    public function getEntityDescriptors(): array
145
    {
146
        return $this->entityDescriptors;
147
    }
148
149
150
151
    /**
152
     * Set the EntityDescriptor children objects
153
     *
154
     * @param \SimpleSAML\SAML2\XML\md\EntityDescriptor[] $entityDescriptors
155
     */
156
    protected function setEntityDescriptors(array $entityDescriptors): void
157
    {
158
        Assert::allIsInstanceOf($entityDescriptors, EntityDescriptor::class);
159
        $this->entityDescriptors = $entityDescriptors;
160
    }
161
162
163
164
    /**
165
     * Collect the value of the Name property.
166
     *
167
     * @return string|null
168
     */
169
    public function getName(): ?string
170
    {
171
        return $this->Name;
172
    }
173
174
175
    /**
176
     * Set the value of the Name property.
177
     *
178
     * @param string|null $name
179
     */
180
    protected function setName(?string $name = null): void
181
    {
182
        if ($name === null) {
183
            return;
184
        }
185
        $this->Name = $name;
186
    }
187
188
189
    /**
190
     * Convert this descriptor to an unsigned XML document.
191
     * This method does not sign the resulting XML document.
192
     *
193
     * @param \DOMElement|null $parent
194
     * @return \DOMElement The root element of the DOM tree
195
     */
196
    protected function toUnsignedXML(DOMElement $parent = null): DOMElement
197
    {
198
        $e = parent::toUnsignedXML($parent);
199
200
        foreach ($this->getAttributesNS() as $attr) {
201
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
202
        }
203
204
        if ($this->Name !== null) {
205
            $e->setAttribute('Name', $this->Name);
206
        }
207
208
        foreach ($this->entitiesDescriptors as $entitiesDescriptor) {
209
            $entitiesDescriptor->toXML($e);
210
        }
211
212
        foreach ($this->entityDescriptors as $entityDescriptor) {
213
            $entityDescriptor->toXML($e);
214
        }
215
216
        return $e;
217
    }
218
}
219