Passed
Pull Request — master (#317)
by Tim
12:30
created

AffiliationDescriptor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 8
dl 0
loc 22
rs 9.9332
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 Exception;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Exception\ProtocolViolationException;
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
     * Generic constructor for SAML metadata documents.
27
     *
28
     * @param string $affiliationOwnerId The ID of the owner of this affiliation.
29
     * @param \SimpleSAML\SAML2\XML\md\AffiliateMember[] $affiliateMember
30
     *   A non-empty array of members of this affiliation.
31
     * @param string|null $ID The ID for this document. Defaults to null.
32
     * @param int|null $validUntil Unix time of validity for this document. Defaults to null.
33
     * @param string|null $cacheDuration Maximum time this document can be cached. Defaults to null.
34
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to an empty array.
35
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $KeyDescriptor
36
     *   An optional array of KeyDescriptors. Defaults to an empty array.
37
     * @param \DOMAttr[] $namespacedAttribute
38
     */
39
    public function __construct(
40
        protected string $affiliationOwnerId,
41
        protected array $affiliateMember,
42
        ?string $ID = null,
43
        ?int $validUntil = null,
44
        ?string $cacheDuration = null,
45
        ?Extensions $extensions = null,
46
        protected array $keyDescriptor = [],
47
        array $namespacedAttribute = [],
48
    ) {
49
        Assert::validURI($affiliationOwnerId, SchemaViolationException::class); // Covers the empty string
50
        Assert::maxLength(
51
            $affiliationOwnerId,
52
            C::ENTITYID_MAX_LENGTH,
53
            sprintf('The AffiliationOwnerID attribute cannot be longer than %d characters.', C::ENTITYID_MAX_LENGTH),
54
            ProtocolViolationException::class,
55
        );
56
        Assert::notEmpty($affiliateMember, 'List of affiliated members must not be empty.');
57
        Assert::allIsInstanceOf($affiliateMember, AffiliateMember::class);
58
        Assert::allIsInstanceOf($keyDescriptor, KeyDescriptor::class);
59
60
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions, $namespacedAttribute);
61
    }
62
63
64
    /**
65
     * Collect the value of the affiliationOwnerId-property
66
     *
67
     * @return string
68
     */
69
    public function getAffiliationOwnerId(): string
70
    {
71
        return $this->affiliationOwnerId;
72
    }
73
74
75
    /**
76
     * Collect the value of the AffiliateMember-property
77
     *
78
     * @return \SimpleSAML\SAML2\XML\md\AffiliateMember[]
79
     */
80
    public function getAffiliateMember(): array
81
    {
82
        return $this->affiliateMember;
83
    }
84
85
86
    /**
87
     * Collect the value of the KeyDescriptor-property
88
     *
89
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
90
     */
91
    public function getKeyDescriptor(): array
92
    {
93
        return $this->keyDescriptor;
94
    }
95
96
97
    /**
98
     * Initialize a AffiliationDescriptor.
99
     *
100
     * @param \DOMElement $xml The XML element we should load.
101
     * @return \SimpleSAML\SAML2\XML\md\AffiliationDescriptor
102
     *
103
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
104
     *   if the qualified name of the supplied element is wrong
105
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
106
     *   if the supplied element is missing one of the mandatory attributes
107
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
108
     *   if too many child-elements of a type are specified
109
     */
110
    public static function fromXML(DOMElement $xml): static
111
    {
112
        Assert::same($xml->localName, 'AffiliationDescriptor', InvalidDOMElementException::class);
113
        Assert::same($xml->namespaceURI, AffiliationDescriptor::NS, InvalidDOMElementException::class);
114
115
        $owner = self::getAttribute($xml, 'affiliationOwnerID');
116
        $members = AffiliateMember::getChildrenOfClass($xml);
117
        $keyDescriptors = KeyDescriptor::getChildrenOfClass($xml);
118
119
        $validUntil = self::getAttribute($xml, 'validUntil', null);
120
        $orgs = Organization::getChildrenOfClass($xml);
121
        Assert::maxCount(
122
            $orgs,
123
            1,
124
            'More than one Organization found in this descriptor',
125
            TooManyElementsException::class,
126
        );
127
128
        $extensions = Extensions::getChildrenOfClass($xml);
129
        Assert::maxCount(
130
            $extensions,
131
            1,
132
            'Only one md:Extensions element is allowed.',
133
            TooManyElementsException::class,
134
        );
135
136
        $signature = Signature::getChildrenOfClass($xml);
137
        Assert::maxCount(
138
            $signature,
139
            1,
140
            'Only one ds:Signature element is allowed.',
141
            TooManyElementsException::class,
142
        );
143
144
        $afd = new static(
145
            $owner,
146
            $members,
147
            self::getAttribute($xml, 'ID', null),
148
            $validUntil !== null ? XMLUtils::xsDateTimeToTimestamp($validUntil) : null,
149
            self::getAttribute($xml, 'cacheDuration', null),
150
            !empty($extensions) ? $extensions[0] : null,
151
            $keyDescriptors,
152
            self::getAttributesNSFromXML($xml),
153
        );
154
155
        if (!empty($signature)) {
156
            $afd->setSignature($signature[0]);
157
            $afd->setXML($xml);
158
        }
159
160
        return $afd;
161
    }
162
163
164
    /**
165
     * Convert this assertion to an unsigned XML document.
166
     * This method does not sign the resulting XML document.
167
     *
168
     * @return \DOMElement The root element of the DOM tree
169
     */
170
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
171
    {
172
        $e = parent::toUnsignedXML($parent);
173
        $e->setAttribute('affiliationOwnerID', $this->getAffiliationOwnerId());
174
175
        foreach ($this->getAffiliateMember() as $am) {
176
            $am->toXML($e);
177
        }
178
179
        foreach ($this->getKeyDescriptor() as $kd) {
180
            $kd->toXML($e);
181
        }
182
183
        return $e;
184
    }
185
}
186