Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

AttributeConsumingService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 153
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 55
dl 0
loc 153
rs 10
c 0
b 0
f 0
wmc 11

6 Methods

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