Passed
Push — master ( eb6d7f...017f7a )
by Tim
02:40
created

AffiliationDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 8
dl 0
loc 15
rs 10

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 Exception;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Exception\ProtovolViolationException;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\Excepti...tovolViolationException 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...
12
use SimpleSAML\XML\Exception\InvalidDOMElementException;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XML\Exception\TooManyElementsException;
15
use SimpleSAML\XML\Utils as XMLUtils;
16
use SimpleSAML\XMLSecurity\XML\ds\Signature;
17
18
/**
19
 * Class representing SAML 2 AffiliationDescriptor element.
20
 *
21
 * @package simplesamlphp/saml2
22
 */
23
final class AffiliationDescriptor extends AbstractMetadataDocument
24
{
25
    /**
26
     * The affiliationOwnerID.
27
     *
28
     * @var string
29
     */
30
    public string $affiliationOwnerID;
31
32
    /**
33
     * The AffiliateMember(s).
34
     *
35
     * Array of \SimpleSAML\SAML2\XML\md\AffiliateMember elements.
36
     *
37
     * @var \SimpleSAML\SAML2\XML\md\AffiliateMember[]
38
     */
39
    protected array $AffiliateMembers = [];
40
41
    /**
42
     * KeyDescriptor elements.
43
     *
44
     * Array of \SimpleSAML\SAML2\XML\md\KeyDescriptor elements.
45
     *
46
     * @var \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
47
     */
48
    protected array $KeyDescriptors = [];
49
50
51
    /**
52
     * Generic constructor for SAML metadata documents.
53
     *
54
     * @param string $ownerID The ID of the owner of this affiliation.
55
     * @param \SimpleSAML\SAML2\XML\md\AffiliateMember[] $members A non-empty array of members of this affiliation.
56
     * @param string|null $ID The ID for this document. Defaults to null.
57
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
58
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
59
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to an empty array.
60
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors An optional array of KeyDescriptors. Defaults to an empty array.
61
     * @param \DOMAttr[] $namespacedAttributes
62
     */
63
    public function __construct(
64
        string $ownerID,
65
        array $members,
66
        ?string $ID = null,
67
        ?int $validUntil = null,
68
        ?string $cacheDuration = null,
69
        ?Extensions $extensions = null,
70
        array $keyDescriptors = [],
71
        array $namespacedAttributes = []
72
    ) {
73
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttributes);
74
75
        $this->setAffiliationOwnerID($ownerID);
76
        $this->setAffiliateMembers($members);
77
        $this->setKeyDescriptors($keyDescriptors);
78
    }
79
80
81
    /**
82
     * Initialize a AffiliationDescriptor.
83
     *
84
     * @param \DOMElement $xml The XML element we should load.
85
     * @return \SimpleSAML\SAML2\XML\md\AffiliationDescriptor
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
88
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
89
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException if too many child-elements of a type are specified
90
     */
91
    public static function fromXML(DOMElement $xml): static
92
    {
93
        Assert::same($xml->localName, 'AffiliationDescriptor', InvalidDOMElementException::class);
94
        Assert::same($xml->namespaceURI, AffiliationDescriptor::NS, InvalidDOMElementException::class);
95
96
        $owner = self::getAttribute($xml, 'affiliationOwnerID');
97
        $members = AffiliateMember::getChildrenOfClass($xml);
98
        $keyDescriptors = KeyDescriptor::getChildrenOfClass($xml);
99
100
        $validUntil = self::getAttribute($xml, 'validUntil', null);
101
        $orgs = Organization::getChildrenOfClass($xml);
102
        Assert::maxCount($orgs, 1, 'More than one Organization found in this descriptor', TooManyElementsException::class);
103
104
        $extensions = Extensions::getChildrenOfClass($xml);
105
        Assert::maxCount($extensions, 1, 'Only one md:Extensions element is allowed.', TooManyElementsException::class);
106
107
        $signature = Signature::getChildrenOfClass($xml);
108
        Assert::maxCount($signature, 1, 'Only one ds:Signature element is allowed.', TooManyElementsException::class);
109
110
        $afd = new static(
111
            $owner,
112
            $members,
113
            self::getAttribute($xml, 'ID', null),
114
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
115
            self::getAttribute($xml, 'cacheDuration', null),
116
            !empty($extensions) ? $extensions[0] : null,
117
            $keyDescriptors,
118
            self::getAttributesNSFromXML($xml)
119
        );
120
121
        if (!empty($signature)) {
122
            $afd->setSignature($signature[0]);
123
        }
124
125
        return $afd;
126
    }
127
128
129
    /**
130
     * Collect the value of the affiliationOwnerId-property
131
     *
132
     * @return string
133
     */
134
    public function getAffiliationOwnerID(): string
135
    {
136
        return $this->affiliationOwnerID;
137
    }
138
139
140
    /**
141
     * Set the value of the affiliationOwnerId-property
142
     *
143
     * @param string $affiliationOwnerId
144
     * @throws \SimpleSAML\Assert\AssertionFailedException
145
     */
146
    protected function setAffiliationOwnerID(string $affiliationOwnerId): void
147
    {
148
        Assert::validURI($affiliationOwnerId, SchemaViolationException::class); // Covers the empty string
149
        Assert::maxLength(
150
            $affiliationOwnerId,
151
            C::ENTITYID_MAX_LENGTH,
152
            sprintf('The AffiliationOwnerID attribute cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),
153
            ProtocolViolationException::class
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\ProtocolViolationException 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...
154
        );
155
        $this->affiliationOwnerID = $affiliationOwnerId;
156
    }
157
158
159
    /**
160
     * Collect the value of the AffiliateMember-property
161
     *
162
     * @return \SimpleSAML\SAML2\XML\md\AffiliateMember[]
163
     */
164
    public function getAffiliateMembers(): array
165
    {
166
        return $this->AffiliateMembers;
167
    }
168
169
170
    /**
171
     * Set the value of the AffiliateMember-property
172
     *
173
     * @param \SimpleSAML\SAML2\XML\md\AffiliateMember[] $affiliateMembers
174
     * @throws \SimpleSAML\Assert\AssertionFailedException
175
     */
176
    protected function setAffiliateMembers(array $affiliateMembers): void
177
    {
178
        Assert::notEmpty(
179
            $affiliateMembers,
180
            'List of affiliated members must not be empty.',
181
        );
182
        Assert::allIsInstanceOf(
183
            $affiliateMembers,
184
            AffiliateMember::class,
185
        );
186
        $this->AffiliateMembers = $affiliateMembers;
187
    }
188
189
190
    /**
191
     * Collect the value of the KeyDescriptor-property
192
     *
193
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
194
     */
195
    public function getKeyDescriptors(): array
196
    {
197
        return $this->KeyDescriptors;
198
    }
199
200
201
    /**
202
     * Set the value of the KeyDescriptor-property
203
     *
204
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptors
205
     */
206
    protected function setKeyDescriptors(array $keyDescriptors): void
207
    {
208
        Assert::allIsInstanceOf(
209
            $keyDescriptors,
210
            KeyDescriptor::class,
211
        );
212
        $this->KeyDescriptors = $keyDescriptors;
213
    }
214
215
216
    /**
217
     * Convert this assertion to an unsigned XML document.
218
     * This method does not sign the resulting XML document.
219
     *
220
     * @return \DOMElement The root element of the DOM tree
221
     */
222
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
223
    {
224
        $e = parent::toUnsignedXML($parent);
225
        $e->setAttribute('affiliationOwnerID', $this->affiliationOwnerID);
226
227
        foreach ($this->AffiliateMembers as $am) {
228
            $am->toXML($e);
229
        }
230
231
        foreach ($this->KeyDescriptors as $kd) {
232
            $kd->toXML($e);
233
        }
234
235
        return $e;
236
    }
237
}
238