Passed
Push — master ( 6db269...70aff8 )
by Jaime Pérez
02:49
created

AbstractEndpointType::getBinding()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\md;
6
7
use DOMElement;
8
use SAML2\XML\ExtendableAttributesTrait;
9
use Webmozart\Assert\Assert;
10
11
/**
12
 * Class representing SAML 2 EndpointType.
13
 *
14
 * This class can be used in two different ways:
15
 *
16
 *   - You can extend the class without extending the constructor. Then you can use the methods available and the
17
 *     class will generate an element with the same name as the extending class (e.g. SAML2\XML\md\AttributeService).
18
 *
19
 *   - Alternatively, you may want to extend the type to add new attributes (e.g look at IndexedEndpointType). In that
20
 *     case, you cannot use this class normally, as if you change the signature of the constructor, you cannot call
21
 *     fromXML() in this class. In order to process an XML document, you can use the get*Attribute() static methods
22
 *     from AbstractXMLElement, and reimplement the fromXML() method with them to suit your new constructor.
23
 *
24
 * @package simplesamlphp/saml2
25
 */
26
abstract class AbstractEndpointType extends AbstractMdElement
27
{
28
    use ExtendableAttributesTrait;
0 ignored issues
show
introduced by
The trait SAML2\XML\ExtendableAttributesTrait requires some properties which are not provided by SAML2\XML\md\AbstractEndpointType: $namespaceURI, $nodeName, $attributes, $value
Loading history...
29
30
    /**
31
     * The binding for this endpoint.
32
     *
33
     * @var string
34
     */
35
    protected $Binding;
36
37
    /**
38
     * The URI to this endpoint.
39
     *
40
     * @var string
41
     */
42
    protected $Location;
43
44
    /**
45
     * The URI where responses can be delivered.
46
     *
47
     * @var string|null
48
     */
49
    protected $ResponseLocation = null;
50
51
52
    /**
53
     * EndpointType constructor.
54
     *
55
     * @param string      $binding
56
     * @param string      $location
57
     * @param string|null $responseLocation
58
     * @param array       $attributes
59
     *
60
     * @throws \InvalidArgumentException
61
     */
62
    public function __construct(
63
        string $binding,
64
        string $location,
65
        ?string $responseLocation = null,
66
        array $attributes = []
67
    ) {
68
        $this->setBinding($binding);
69
        $this->setLocation($location);
70
        $this->setResponseLocation($responseLocation);
71
        $this->setAttributesNS($attributes);
72
    }
73
74
75
    /**
76
     * Initialize an EndpointType.
77
     *
78
     * Note: this method cannot be used when extending this class, if the constructor has a different signature.
79
     *
80
     * @param \DOMElement $xml The XML element we should load.
81
     * @return static
82
     * @throws \InvalidArgumentException if the qualified name of the supplied element is wrong
83
     */
84
    public static function fromXML(DOMElement $xml): object
85
    {
86
        $qualifiedName = static::getClassName(static::class);
87
        Assert::eq(
88
            $xml->localName,
89
            $qualifiedName,
90
            'Unexpected name for endpoint: ' . $xml->localName . '. Expected: ' . $qualifiedName . '.'
91
        );
92
93
        return new static(
94
            self::getAttribute($xml, 'Binding'),
95
            self::getAttribute($xml, 'Location'),
96
            self::getAttribute($xml, 'ResponseLocation', null),
97
            self::getAttributesNSFromXML($xml)
0 ignored issues
show
Bug introduced by
It seems like self::getAttributesNSFromXML($xml) can also be of type null; however, parameter $attributes of SAML2\XML\md\AbstractEndpointType::__construct() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

97
            /** @scrutinizer ignore-type */ self::getAttributesNSFromXML($xml)
Loading history...
98
        );
99
    }
100
101
102
    /**
103
     * Collect the value of the Binding property.
104
     *
105
     * @return string
106
     */
107
    public function getBinding(): string
108
    {
109
        return $this->Binding;
110
    }
111
112
113
    /**
114
     * Set the value of the Binding property.
115
     *
116
     * @param string $binding
117
     * @throws \InvalidArgumentException if the Binding is empty
118
     */
119
    protected function setBinding(string $binding): void
120
    {
121
        Assert::notEmpty($binding, 'The Binding of an endpoint cannot be empty.');
122
        $this->Binding = $binding;
123
    }
124
125
126
    /**
127
     * Collect the value of the Location property.
128
     *
129
     * @return string
130
     */
131
    public function getLocation(): string
132
    {
133
        return $this->Location;
134
    }
135
136
137
    /**
138
     * Set the value of the Location property.
139
     *
140
     * @param string $location
141
     * @throws \InvalidArgumentException if the Location is empty
142
     */
143
    protected function setLocation(string $location): void
144
    {
145
        Assert::notEmpty($location, 'The Location of an endpoint cannot be empty.');
146
        $this->Location = $location;
147
    }
148
149
150
    /**
151
     * Collect the value of the ResponseLocation property.
152
     *
153
     * @return string|null
154
     */
155
    public function getResponseLocation(): ?string
156
    {
157
        return $this->ResponseLocation;
158
    }
159
160
161
    /**
162
     * Set the value of the ResponseLocation property.
163
     *
164
     * @param string|null $responseLocation
165
     * @throws \InvalidArgumentException if the ResponseLocation is empty
166
     */
167
    protected function setResponseLocation(?string $responseLocation = null): void
168
    {
169
        Assert::nullOrNotEmpty($responseLocation, 'The ResponseLocation of an endpoint cannot be empty.');
170
        $this->ResponseLocation = $responseLocation;
171
    }
172
173
174
    /**
175
     * Add this endpoint to an XML element.
176
     *
177
     * @param \DOMElement $parent The element we should append this endpoint to.
178
     * @return \DOMElement
179
     */
180
    public function toXML(DOMElement $parent = null): DOMElement
181
    {
182
        $e = parent::instantiateParentElement($parent);
183
184
        $e->setAttribute('Binding', $this->Binding);
185
        $e->setAttribute('Location', $this->Location);
186
187
        if ($this->ResponseLocation !== null) {
188
            $e->setAttribute('ResponseLocation', $this->ResponseLocation);
189
        }
190
191
        foreach ($this->getAttributesNS() as $a) {
192
            $e->setAttributeNS($a['namespaceURI'], $a['qualifiedName'], $a['value']);
193
        }
194
195
        return $e;
196
    }
197
}
198