Passed
Push — master ( 2d22c8...049d3a )
by Tim
02:04
created

AbstractIndexedEndpointType::toXML()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 28
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 14
nc 24
nop 1
dl 0
loc 28
rs 8.8333
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 SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\XML\Chunk;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
13
use function strval;
14
15
/**
16
 * Class representing a SAML2 IndexedEndpointType.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
abstract class AbstractIndexedEndpointType extends AbstractEndpointType
21
{
22
    use IndexedElementTrait;
23
24
25
    /**
26
     * IndexedEndpointType constructor.
27
     *
28
     * Note: if you extend this class, the constructor must retain its signature. You cannot extend this class and
29
     * modify the signature of the constructor, unless you implement fromXML() yourself. This class provides
30
     * static methods to get its properties from a given \DOMElement for your convenience. Look at the implementation
31
     * of fromXML() to know how to use them.
32
     *
33
     * @param int $index
34
     * @param string $binding
35
     * @param string $location
36
     * @param bool|null $isDefault
37
     * @param string|null $responseLocation
38
     * @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...
39
>>>>>>> 905e2061 (Fix: allow AbstractIndexedEndpointType to pass arbitrary child-elements)
40
     */
41
    public function __construct(
42
        int $index,
43
        string $binding,
44
        string $location,
45
        ?bool $isDefault = null,
46
        ?string $responseLocation = null,
47
        array $attributes = [],
48
        array $children = [],
49
    ) {
50
        parent::__construct($binding, $location, $responseLocation, $attributes, $children);
51
52
        $this->setIndex($index);
53
        $this->setIsDefault($isDefault);
54
    }
55
56
57
    /**
58
     * Initialize an IndexedEndpointType.
59
     *
60
     * @param \DOMElement $xml The XML element we should load.
61
     * @return static
62
     *
63
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
64
     *   if the qualified name of the supplied element is wrong
65
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
66
     *   if the supplied element is missing any of the mandatory attributes
67
     */
68
    public static function fromXML(DOMElement $xml): static
69
    {
70
        $qualifiedName = static::getClassName(static::class);
71
        Assert::eq(
72
            $xml->localName,
73
            $qualifiedName,
74
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.',
75
            InvalidDOMElementException::class,
76
        );
77
78
        $index = self::getIntegerAttribute($xml, 'index');
79
        $binding = self::getAttribute($xml, 'Binding');
80
        $location = self::getAttribute($xml, 'Location');
81
82
        $children = [];
83
        foreach ($xml->childNodes as $child) {
84
            if ($child->namespaceURI === C::NS_MD) {
85
                continue;
86
            } elseif (!($child instanceof DOMElement)) {
87
                continue;
88
            }
89
90
            $children[] = new Chunk($child);
91
        }
92
93
        return new static(
94
            $index,
95
            $binding,
96
            $location,
97
            self::getOptionalBooleanAttribute($xml, 'isDefault', null),
98
            self::getOptionalAttribute($xml, 'ResponseLocation', null),
99
            self::getAttributesNSFromXML($xml),
100
            $children,
101
        );
102
    }
103
104
105
    /**
106
     * Add this endpoint to an XML element.
107
     *
108
     * @param \DOMElement $parent The element we should append this endpoint to.
109
     * @return \DOMElement
110
     */
111
    public function toXML(DOMElement $parent = null): DOMElement
112
    {
113
        $e = parent::instantiateParentElement($parent);
114
115
        $e->setAttribute('Binding', $this->getBinding());
116
        $e->setAttribute('Location', $this->getLocation());
117
        if ($this->getResponseLocation() !== null) {
118
            $e->setAttribute('ResponseLocation', $this->getResponseLocation());
119
        }
120
121
        $e->setAttribute('index', strval($this->getIndex()));
122
123
        if (is_bool($this->getIsDefault())) {
124
            $e->setAttribute('isDefault', $this->getIsDefault() ? 'true' : 'false');
125
        }
126
127
        foreach ($this->getAttributesNS() as $attr) {
128
            $attr->toXML($e);
129
        }
130
131
        /** @var \SimpleSAML\XML\SerializableElementInterface $child */
132
        foreach ($this->getElements() as $child) {
133
            if (!$child->isEmptyElement()) {
134
                $child->toXML($e);
135
            }
136
        }
137
138
        return $e;
139
    }
140
}
141