Passed
Pull Request — master (#337)
by Tim
02:07
created

AbstractEndpointType   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 243
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 83
dl 0
loc 243
rs 10
c 1
b 0
f 0
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A getBinding() 0 3 1
A getResponseLocation() 0 3 1
A toXML() 0 23 5
A fromXML() 0 30 4
A getLocation() 0 3 1
A __construct() 0 13 1
A processArrayContents() 0 43 5
A toArray() 0 14 2
A fromArray() 0 10 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\SAML2\Constants as C;
10
use SimpleSAML\XML\ArrayizableElementInterface;
11
use SimpleSAML\XML\Attribute as XMLAttribute;
12
use SimpleSAML\XML\Chunk;
13
use SimpleSAML\XML\Exception\InvalidDOMElementException;
14
use SimpleSAML\XML\Exception\SchemaViolationException;
15
use SimpleSAML\XML\ExtendableAttributesTrait;
16
use SimpleSAML\XML\ExtendableElementTrait;
17
use SimpleSAML\XML\SerializableElementInterface;
18
19
use function array_change_key_case;
20
use function array_key_exists;
21
use function array_keys;
22
23
/**
24
 * Class representing SAML 2 EndpointType.
25
 *
26
 * This class can be used in two different ways:
27
 *
28
 *   - You can extend the class without extending the constructor. Then you can use the methods available and the class
29
 *     will generate an element with the same name as the extending class
30
 *     (e.g. \SimpleSAML\SAML2\XML\md\AttributeService).
31
 *
32
 *   - Alternatively, you may want to extend the type to add new attributes (e.g look at IndexedEndpointType). In that
33
 *     case, you cannot use this class normally, as if you change the signature of the constructor, you cannot call
34
 *     fromXML() in this class. In order to process an XML document, you can use the get*Attribute() static methods
35
 *     from AbstractElement, and reimplement the fromXML() method with them to suit your new constructor.
36
 *
37
 * @package simplesamlphp/saml2
38
 */
39
abstract class AbstractEndpointType extends AbstractMdElement implements ArrayizableElementInterface
40
{
41
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractEndpointType: $localName, $nodeValue, $namespaceURI, $prefix, $attributes
Loading history...
42
    use ExtendableElementTrait;
43
44
    /** The namespace-attribute for the xs:any element */
45
    public const XS_ANY_ELT_NAMESPACE = C::XS_ANY_NS_OTHER;
46
47
    /** The namespace-attribute for the xs:anyAttribute element */
48
    public const XS_ANY_ATTR_NAMESPACE = C::XS_ANY_NS_OTHER;
49
50
51
    /**
52
     * EndpointType constructor.
53
     *
54
     * @param string $binding
55
     * @param string $location
56
     * @param string|null $responseLocation
57
     * @param \SimpleSAML\XML\ElementInterface[] $children
58
     * @param list<\SimpleSAML\XML\Attribute> $attributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
59
     *
60
     * @throws \SimpleSAML\Assert\AssertionFailedException
61
     */
62
    public function __construct(
63
        protected string $binding,
64
        protected string $location,
65
        protected ?string $responseLocation = null,
66
        array $children = [],
67
        array $attributes = [],
68
    ) {
69
        Assert::validURI($binding, SchemaViolationException::class); // Covers the empty string
70
        Assert::validURI($location, SchemaViolationException::class); // Covers the empty string
71
        Assert::nullOrValidURI($responseLocation, SchemaViolationException::class); // Covers the empty string
72
73
        $this->setElements($children);
74
        $this->setAttributesNS($attributes);
75
    }
76
77
78
    /**
79
     * Collect the value of the Binding property.
80
     *
81
     * @return string
82
     */
83
    public function getBinding(): string
84
    {
85
        return $this->binding;
86
    }
87
88
89
    /**
90
     * Collect the value of the Location property.
91
     *
92
     * @return string
93
     */
94
    public function getLocation(): string
95
    {
96
        return $this->location;
97
    }
98
99
100
    /**
101
     * Collect the value of the ResponseLocation property.
102
     *
103
     * @return string|null
104
     */
105
    public function getResponseLocation(): ?string
106
    {
107
        return $this->responseLocation;
108
    }
109
110
111
    /**
112
     * Initialize an EndpointType.
113
     *
114
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
115
     *
116
     * @param \DOMElement $xml The XML element we should load.
117
     * @return static
118
     *
119
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
120
     *   if the qualified name of the supplied element is wrong
121
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
122
     *   if the supplied element is missing any of the mandatory attributes
123
     */
124
    public static function fromXML(DOMElement $xml): static
125
    {
126
        $qualifiedName = static::getClassName(static::class);
127
        Assert::eq(
128
            $xml->localName,
129
            $qualifiedName,
130
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.',
131
            InvalidDOMElementException::class,
132
        );
133
134
        $binding = self::getAttribute($xml, 'Binding');
135
        $location = self::getAttribute($xml, 'Location');
136
137
        $children = [];
138
        foreach ($xml->childNodes as $child) {
139
            if ($child->namespaceURI === C::NS_MD) {
140
                continue;
141
            } elseif (!($child instanceof DOMElement)) {
142
                continue;
143
            }
144
145
            $children[] = new Chunk($child);
146
        }
147
148
        return new static(
149
            $binding,
150
            $location,
151
            self::getOptionalAttribute($xml, 'ResponseLocation', null),
152
            $children,
153
            self::getAttributesNSFromXML($xml),
154
        );
155
    }
156
157
158
    /**
159
     * Add this endpoint to an XML element.
160
     *
161
     * @param \DOMElement $parent The element we should append this endpoint to.
162
     * @return \DOMElement
163
     */
164
    public function toXML(DOMElement $parent = null): DOMElement
165
    {
166
        $e = parent::instantiateParentElement($parent);
167
168
        $e->setAttribute('Binding', $this->getBinding());
169
        $e->setAttribute('Location', $this->getLocation());
170
171
        if ($this->getResponseLocation() !== null) {
172
            $e->setAttribute('ResponseLocation', $this->getResponseLocation());
173
        }
174
175
        foreach ($this->getAttributesNS() as $attr) {
176
            $attr->toXML($e);
177
        }
178
179
        /** @var \SimpleSAML\XML\SerializableElementInterface $child */
180
        foreach ($this->getElements() as $child) {
181
            if (!$child->isEmptyElement()) {
182
                $child->toXML($e);
183
            }
184
        }
185
186
        return $e;
187
    }
188
189
190
    /**
191
     * Create a class from an array
192
     *
193
     * @param array $data
194
     * @return static
195
     */
196
    public static function fromArray(array $data): static
197
    {
198
        $data = self::processArrayContents($data);
199
200
        return new static(
201
            $data['Binding'],
202
            $data['Location'],
203
            $data['ResponseLocation'] ?? null,
204
            $data['children'] ?? [],
205
            $data['attributes'] ?? [],
206
        );
207
    }
208
209
210
    /**
211
     * Validates an array representation of this object and returns the same array with
212
     * rationalized keys (casing) and parsed sub-elements.
213
     *
214
     * @param array $data
215
     * @return array $data
216
     */
217
    private static function processArrayContents(array $data): array
218
    {
219
        $data = array_change_key_case($data, CASE_LOWER);
220
221
        // Make sure the array keys are known for this kind of object
222
        Assert::allOneOf(
223
            array_keys($data),
224
            ['binding', 'location', 'responselocation', 'children', 'attributes'],
225
        );
226
227
        // Make sure all the mandatory items exist
228
        Assert::keyExists($data, 'binding');
229
        Assert::keyExists($data, 'location');
230
231
        // Make sure the items have the correct data type
232
        Assert::string($data['binding']);
233
        Assert::string($data['location']);
234
235
        $retval = [
236
            'Binding' => $data['binding'],
237
            'Location' => $data['location'],
238
        ];
239
240
        if (array_key_exists('responselocation', $data)) {
241
            Assert::string($data['responselocation']);
242
            $retval['ResponseLocation'] = $data['responselocation'];
243
        }
244
245
        if (array_key_exists('children', $data)) {
246
            Assert::isArray($data['children']);
247
            Assert::allIsInstanceOf($data['children'], SerializableElementInterface::class);
248
            $retval['children'] = $data['children'];
249
        }
250
251
        if (array_key_exists('attributes', $data)) {
252
            Assert::isArray($data['attributes']);
253
            Assert::allIsArray($data['attributes']);
254
            foreach ($data['attributes'] as $i => $attr) {
255
                $retval['attributes'][] = XMLAttribute::fromArray($attr);
256
            }
257
        }
258
259
        return $retval;
260
    }
261
262
263
    /**
264
     * Create an array from this class
265
     *
266
     * @return array
267
     */
268
    public function toArray(): array
269
    {
270
        $data = [
271
            'Binding' => $this->getBinding(),
272
            'Location' => $this->getLocation(),
273
            'ResponseLocation' => $this->getResponseLocation(),
274
            'children' => $this->getElements(),
275
        ];
276
277
        foreach ($this->getAttributesNS() as $a) {
278
            $data['attributes'][] = $a->toArray();
279
        }
280
281
        return array_filter($data);
282
    }
283
}
284