Passed
Push — master ( a3fda0...7c0590 )
by Tim
14:58 queued 13:28
created

AffiliationDescriptor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 171
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 171
rs 10
c 0
b 0
f 0
wmc 11
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\EntityIDValue;
11
use SimpleSAML\SAML2\Type\SAMLDateTimeValue;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
16
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
17
use SimpleSAML\XMLSchema\Type\DurationValue;
18
use SimpleSAML\XMLSchema\Type\IDValue;
19
use SimpleSAML\XMLSchema\XML\Constants\NS;
20
use SimpleSAML\XMLSecurity\XML\ds\Signature;
21
22
/**
23
 * Class representing SAML 2 AffiliationDescriptor element.
24
 *
25
 * @package simplesamlphp/saml2
26
 */
27
final class AffiliationDescriptor extends AbstractMetadataDocument implements SchemaValidatableElementInterface
28
{
29
    use ExtendableAttributesTrait;
30
    use SchemaValidatableElementTrait;
31
32
33
    /** The namespace-attribute for the xs:anyAttribute element */
34
    public const string XS_ANY_ATTR_NAMESPACE = NS::OTHER;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected T_STRING, expecting '=' on line 34 at column 24
Loading history...
35
36
37
    /**
38
     * Generic constructor for SAML metadata documents.
39
     *
40
     * @param \SimpleSAML\SAML2\Type\EntityIDValue $affiliationOwnerId The ID of the owner of this affiliation.
41
     * @param \SimpleSAML\SAML2\XML\md\AffiliateMember[] $affiliateMember
42
     *   A non-empty array of members of this affiliation.
43
     * @param \SimpleSAML\XMLSchema\Type\IDValue|null $ID The ID for this document. Defaults to null.
44
     * @param \SimpleSAML\SAML2\Type\SAMLDateTimeValue|null $validUntil Unix time of validity for this document.
45
     *   Defaults to null.
46
     * @param \SimpleSAML\XMLSchema\Type\DurationValue|null $cacheDuration Maximum time this document can be cached.
47
     *   Defaults to null.
48
     * @param \SimpleSAML\SAML2\XML\md\Extensions|null $extensions An array of extensions. Defaults to an empty array.
49
     * @param \SimpleSAML\SAML2\XML\md\KeyDescriptor[] $keyDescriptor
50
     *   An optional array of KeyDescriptors. Defaults to an empty array.
51
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttribute
52
     */
53
    public function __construct(
54
        protected EntityIDValue $affiliationOwnerId,
55
        protected array $affiliateMember,
56
        ?IDValue $ID = null,
57
        ?SAMLDateTimeValue $validUntil = null,
58
        ?DurationValue $cacheDuration = null,
59
        ?Extensions $extensions = null,
60
        protected array $keyDescriptor = [],
61
        array $namespacedAttribute = [],
62
    ) {
63
        Assert::notEmpty($affiliateMember, 'List of affiliated members must not be empty.');
64
        Assert::maxCount($affiliateMember, C::UNBOUNDED_LIMIT);
65
        Assert::allIsInstanceOf($affiliateMember, AffiliateMember::class);
66
        Assert::maxCount($keyDescriptor, C::UNBOUNDED_LIMIT);
67
        Assert::allIsInstanceOf($keyDescriptor, KeyDescriptor::class);
68
69
        parent::__construct($ID, $validUntil, $cacheDuration, $extensions);
70
71
        $this->setAttributesNS($namespacedAttribute);
72
    }
73
74
75
    /**
76
     * Collect the value of the affiliationOwnerId-property
77
     *
78
     * @return \SimpleSAML\SAML2\Type\EntityIDValue
79
     */
80
    public function getAffiliationOwnerId(): EntityIDValue
81
    {
82
        return $this->affiliationOwnerId;
83
    }
84
85
86
    /**
87
     * Collect the value of the AffiliateMember-property
88
     *
89
     * @return \SimpleSAML\SAML2\XML\md\AffiliateMember[]
90
     */
91
    public function getAffiliateMember(): array
92
    {
93
        return $this->affiliateMember;
94
    }
95
96
97
    /**
98
     * Collect the value of the KeyDescriptor-property
99
     *
100
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor[]
101
     */
102
    public function getKeyDescriptor(): array
103
    {
104
        return $this->keyDescriptor;
105
    }
106
107
108
    /**
109
     * Initialize a AffiliationDescriptor.
110
     *
111
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
112
     *   if the qualified name of the supplied element is wrong
113
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
114
     *   if the supplied element is missing one of the mandatory attributes
115
     * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException
116
     *   if too many child-elements of a type are specified
117
     */
118
    public static function fromXML(DOMElement $xml): static
119
    {
120
        Assert::same($xml->localName, 'AffiliationDescriptor', InvalidDOMElementException::class);
121
        Assert::same($xml->namespaceURI, AffiliationDescriptor::NS, InvalidDOMElementException::class);
122
123
        $owner = self::getAttribute($xml, 'affiliationOwnerID', EntityIDValue::class);
124
        $members = AffiliateMember::getChildrenOfClass($xml);
125
        $keyDescriptors = KeyDescriptor::getChildrenOfClass($xml);
126
127
        $orgs = Organization::getChildrenOfClass($xml);
128
        Assert::maxCount(
129
            $orgs,
130
            1,
131
            'More than one Organization found in this descriptor',
132
            TooManyElementsException::class,
133
        );
134
135
        $extensions = Extensions::getChildrenOfClass($xml);
136
        Assert::maxCount(
137
            $extensions,
138
            1,
139
            'Only one md:Extensions element is allowed.',
140
            TooManyElementsException::class,
141
        );
142
143
        $signature = Signature::getChildrenOfClass($xml);
144
        Assert::maxCount(
145
            $signature,
146
            1,
147
            'Only one ds:Signature element is allowed.',
148
            TooManyElementsException::class,
149
        );
150
151
        $afd = new static(
152
            $owner,
153
            $members,
154
            self::getOptionalAttribute($xml, 'ID', IDValue::class, null),
155
            self::getOptionalAttribute($xml, 'validUntil', SAMLDateTimeValue::class, null),
156
            self::getOptionalAttribute($xml, 'cacheDuration', DurationValue::class, null),
157
            !empty($extensions) ? $extensions[0] : null,
158
            $keyDescriptors,
159
            self::getAttributesNSFromXML($xml),
160
        );
161
162
        if (!empty($signature)) {
163
            $afd->setSignature($signature[0]);
164
            $afd->setXML($xml);
165
        }
166
167
        return $afd;
168
    }
169
170
171
    /**
172
     * Convert this assertion to an unsigned XML document.
173
     * This method does not sign the resulting XML document.
174
     */
175
    public function toUnsignedXML(?DOMElement $parent = null): DOMElement
176
    {
177
        $e = parent::toUnsignedXML($parent);
178
        $e->setAttribute('affiliationOwnerID', $this->getAffiliationOwnerId()->getValue());
179
180
        foreach ($this->getAttributesNS() as $attr) {
181
            $attr->toXML($e);
182
        }
183
184
        foreach ($this->getAffiliateMember() as $am) {
185
            $am->toXML($e);
186
        }
187
188
        foreach ($this->getKeyDescriptor() as $kd) {
189
            $kd->toXML($e);
190
        }
191
192
        return $e;
193
    }
194
}
195