AttributeConsumingService   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 56
dl 0
loc 155
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceDescription() 0 3 1
A __construct() 0 40 1
A toXML() 0 20 5
A fromXML() 0 23 1
A getRequestedAttribute() 0 3 1
A getServiceName() 0 3 1
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\XML\Constants as C;
10
use SimpleSAML\XML\SchemaValidatableElementInterface;
11
use SimpleSAML\XML\SchemaValidatableElementTrait;
12
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
13
use SimpleSAML\XMLSchema\Exception\MissingElementException;
14
use SimpleSAML\XMLSchema\Type\BooleanValue;
15
use SimpleSAML\XMLSchema\Type\UnsignedShortValue;
16
17
use function var_export;
18
19
/**
20
 * Class representing SAML 2 Metadata AttributeConsumingService element.
21
 *
22
 * @package simplesamlphp/saml2
23
 */
24
final class AttributeConsumingService extends AbstractMdElement implements SchemaValidatableElementInterface
25
{
26
    use IndexedElementTrait;
27
    use SchemaValidatableElementTrait;
28
29
30
    /**
31
     * AttributeConsumingService constructor.
32
     *
33
     * @param \SimpleSAML\XMLSchema\Type\UnsignedShortValue $index
34
     * @param \SimpleSAML\SAML2\XML\md\ServiceName[] $serviceName
35
     * @param \SimpleSAML\SAML2\XML\md\RequestedAttribute[] $requestedAttribute
36
     * @param \SimpleSAML\XMLSchema\Type\BooleanValue|null $isDefault
37
     * @param \SimpleSAML\SAML2\XML\md\ServiceDescription[] $serviceDescription
38
     */
39
    public function __construct(
40
        UnsignedShortValue $index,
41
        protected array $serviceName,
42
        protected array $requestedAttribute,
43
        ?BooleanValue $isDefault = null,
44
        protected array $serviceDescription = [],
45
    ) {
46
        Assert::maxCount($serviceName, C::UNBOUNDED_LIMIT);
47
        Assert::minCount(
48
            $serviceName,
49
            1,
50
            'Missing at least one ServiceName in AttributeConsumingService.',
51
            MissingElementException::class,
52
        );
53
        Assert::allIsInstanceOf(
54
            $serviceName,
55
            ServiceName::class,
56
            'Service names must be specified as ServiceName objects.',
57
        );
58
        Assert::maxCount($serviceDescription, C::UNBOUNDED_LIMIT);
59
        Assert::allIsInstanceOf(
60
            $serviceDescription,
61
            ServiceDescription::class,
62
            'Service descriptions must be specified as ServiceDescription objects.',
63
        );
64
        Assert::maxCount($requestedAttribute, C::UNBOUNDED_LIMIT);
65
        Assert::allIsInstanceOf(
66
            $requestedAttribute,
67
            RequestedAttribute::class,
68
            'Requested attributes must be specified as RequestedAttribute objects.',
69
        );
70
        Assert::minCount(
71
            $requestedAttribute,
72
            1,
73
            'Missing at least one RequestedAttribute in AttributeConsumingService.',
74
            MissingElementException::class,
75
        );
76
77
        $this->setIndex($index);
78
        $this->setIsDefault($isDefault);
79
    }
80
81
82
    /**
83
     * Initialize / parse an AttributeConsumingService.
84
     *
85
     * @param \DOMElement $xml The XML element we should load.
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
89
     *   if the qualified name of the supplied element is wrong
90
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
91
     *   if one of the mandatory child-elements is missing
92
     */
93
    public static function fromXML(DOMElement $xml): static
94
    {
95
        Assert::same($xml->localName, 'AttributeConsumingService', InvalidDOMElementException::class);
96
        Assert::same($xml->namespaceURI, AttributeConsumingService::NS, InvalidDOMElementException::class);
97
98
        $names = ServiceName::getChildrenOfClass($xml);
99
        Assert::minCount(
100
            $names,
101
            1,
102
            'Missing at least one ServiceName in AttributeConsumingService.',
103
            MissingElementException::class,
104
        );
105
106
        $descriptions = ServiceDescription::getChildrenOfClass($xml);
107
108
        $requestedAttrs = RequestedAttribute::getChildrenOfClass($xml);
109
110
        return new static(
111
            self::getAttribute($xml, 'index', UnsignedShortValue::class),
112
            $names,
113
            $requestedAttrs,
114
            self::getOptionalAttribute($xml, 'isDefault', BooleanValue::class, null),
115
            $descriptions,
116
        );
117
    }
118
119
120
    /**
121
     * Get the localized names of this service.
122
     *
123
     * @return \SimpleSAML\SAML2\XML\md\ServiceName[]
124
     */
125
    public function getServiceName(): array
126
    {
127
        return $this->serviceName;
128
    }
129
130
131
    /**
132
     * Collect the value of the ServiceDescription-property
133
     *
134
     * @return \SimpleSAML\SAML2\XML\md\ServiceDescription[]
135
     */
136
    public function getServiceDescription(): array
137
    {
138
        return $this->serviceDescription;
139
    }
140
141
142
    /**
143
     * Collect the value of the RequestedAttribute-property
144
     *
145
     * @return \SimpleSAML\SAML2\XML\md\RequestedAttribute[]
146
     */
147
    public function getRequestedAttribute(): array
148
    {
149
        return $this->requestedAttribute;
150
    }
151
152
153
    /**
154
     * Convert to \DOMElement.
155
     *
156
     * @param \DOMElement $parent The element we should append this AttributeConsumingService to.
157
     * @return \DOMElement
158
     */
159
    public function toXML(?DOMElement $parent = null): DOMElement
160
    {
161
        $e = $this->instantiateParentElement($parent);
162
        $e->setAttribute('index', $this->getIndex()->getValue());
163
164
        if ($this->getIsDefault() !== null) {
165
            $e->setAttribute('isDefault', var_export($this->getIsDefault()->toBoolean(), true));
166
        }
167
168
        foreach ($this->getServiceName() as $name) {
169
            $name->toXML($e);
170
        }
171
        foreach ($this->getServiceDescription() as $description) {
172
            $description->toXML($e);
173
        }
174
        foreach ($this->getRequestedAttribute() as $ra) {
175
            $ra->toXML($e);
176
        }
177
178
        return $e;
179
    }
180
}
181