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

AffiliationDescriptor::fromXML()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 35
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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