Passed
Push — master ( 939ee5...33b650 )
by Tim
02:12
created

Attribute   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 221
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 45
c 1
b 0
f 1
dl 0
loc 221
rs 10
wmc 17

13 Methods

Rating   Name   Duplication   Size   Complexity  
A getAttributeValues() 0 3 1
A setFriendlyName() 0 4 1
A getEncryptionBackend() 0 5 1
A __construct() 0 12 1
A setName() 0 4 1
A fromXML() 0 11 1
A getFriendlyName() 0 3 1
A getName() 0 3 1
A getBlacklistedAlgorithms() 0 4 1
A toXML() 0 22 5
A setAttributeValues() 0 4 1
A getNameFormat() 0 3 1
A setNameFormat() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use Exception;
9
use SimpleSAML\Assert\Assert;
10
use SimpleSAML\SAML2\Constants;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
14
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
15
16
/**
17
 * Class representing SAML 2 Attribute.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
class Attribute extends AbstractSamlElement implements IdentifierInterface, EncryptableElementInterface
22
{
23
    use EncryptableElementTrait;
24
    use ExtendableAttributesTrait;
1 ignored issue
show
introduced by
The trait SimpleSAML\XML\ExtendableAttributesTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\saml\Attribute: $namespaceURI, $nodeName, $attributes, $value
Loading history...
25
26
    /**
27
     * The Name of this attribute.
28
     *
29
     * @var string
30
     */
31
    protected string $Name;
32
33
    /**
34
     * The NameFormat of this attribute.
35
     *
36
     * @var string|null
37
     */
38
    protected ?string $NameFormat = null;
39
40
    /**
41
     * The FriendlyName of this attribute.
42
     *
43
     * @var string|null
44
     */
45
    protected ?string $FriendlyName = null;
46
47
    /**
48
     * List of attribute values.
49
     *
50
     * Array of \SimpleSAML\SAML2\XML\saml\AttributeValue elements.
51
     *
52
     * @var \SimpleSAML\SAML2\XML\saml\AttributeValue[]
53
     */
54
    protected array $AttributeValues = [];
55
56
57
    /**
58
     * Initialize an Attribute.
59
     *
60
     * @param string $Name
61
     * @param string|null $NameFormat
62
     * @param string|null $FriendlyName
63
     * @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $AttributeValues
64
     * @param \DOMAttr[] $namespacedAttributes
65
     */
66
    public function __construct(
67
        string $Name,
68
        ?string $NameFormat = null,
69
        ?string $FriendlyName = null,
70
        array $AttributeValues = [],
71
        array $namespacedAttributes = []
72
    ) {
73
        $this->setName($Name);
74
        $this->setNameFormat($NameFormat);
75
        $this->setFriendlyName($FriendlyName);
76
        $this->setAttributeValues($AttributeValues);
77
        $this->setAttributesNS($namespacedAttributes);
78
    }
79
80
81
    /**
82
     * Collect the value of the Name-property
83
     *
84
     * @return string
85
     */
86
    public function getName(): string
87
    {
88
        return $this->Name;
89
    }
90
91
92
    /**
93
     * Set the value of the Name-property
94
     *
95
     * @param string $name
96
     */
97
    protected function setName(string $name): void
98
    {
99
        Assert::notWhitespaceOnly($name, 'Cannot specify an empty name for an Attribute.');
100
        $this->Name = $name;
101
    }
102
103
104
    /**
105
     * Collect the value of the NameFormat-property
106
     *
107
     * @return string|null
108
     */
109
    public function getNameFormat(): ?string
110
    {
111
        return $this->NameFormat;
112
    }
113
114
115
    /**
116
     * Set the value of the NameFormat-property
117
     *
118
     * @param string|null $NameFormat
119
     * @throws \SimpleSAML\Assert\AssertionFailedException if the NameFormat is empty
120
     */
121
    protected function setNameFormat(?string $NameFormat): void
122
    {
123
        Assert::nullOrNotWhitespaceOnly($NameFormat, 'Cannot specify an empty NameFormat for an Attribute.');
124
        $this->NameFormat = $NameFormat;
125
    }
126
127
128
    /**
129
     * Collect the value of the FriendlyName-property
130
     *
131
     * @return string|null
132
     */
133
    public function getFriendlyName(): ?string
134
    {
135
        return $this->FriendlyName;
136
    }
137
138
139
    /**
140
     * Set the value of the FriendlyName-property
141
     *
142
     * @param string|null $friendlyName
143
     * @throws \SimpleSAML\Assert\AssertionFailedException if the FriendlyName is empty
144
     */
145
    private function setFriendlyName(?string $friendlyName): void
146
    {
147
        Assert::nullOrNotWhitespaceOnly($friendlyName, 'FriendlyName cannot be an empty string.');
148
        $this->FriendlyName = $friendlyName;
149
    }
150
151
152
    /**
153
     * Collect the value of the attributeValues-property
154
     *
155
     * @return \SimpleSAML\SAML2\XML\saml\AttributeValue[]
156
     */
157
    public function getAttributeValues(): array
158
    {
159
        return $this->AttributeValues;
160
    }
161
162
163
    /**
164
     * Set the value of the AttributeValues-property
165
     *
166
     * @param \SimpleSAML\SAML2\XML\saml\AttributeValue[] $attributeValues
167
     */
168
    protected function setAttributeValues(array $attributeValues): void
169
    {
170
        Assert::allIsInstanceOf($attributeValues, AttributeValue::class, 'Invalid AttributeValue.');
171
        $this->AttributeValues = $attributeValues;
172
    }
173
174
175
    public function getBlacklistedAlgorithms(): ?array
176
    {
177
        // return an array with the algorithms you don't want to allow to be used
178
        return [];
179
    }
180
181
182
    public function getEncryptionBackend(): ?EncryptionBackend
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\saml\EncryptionBackend 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...
183
    {
184
        // return the encryption backend you want to use,
185
        // or null if you are fine with the default
186
        return null;
187
    }
188
189
190
    /**
191
     * Convert XML into a Attribute
192
     *
193
     * @param \DOMElement $xml The XML element we should load
194
     * @return \SimpleSAML\SAML2\XML\saml\Attribute
195
     *
196
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
197
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException if the supplied element is missing one of the mandatory attributes
198
     */
199
    public static function fromXML(DOMElement $xml): object
200
    {
201
        Assert::same($xml->localName, 'Attribute', InvalidDOMElementException::class);
202
        Assert::same($xml->namespaceURI, Attribute::NS, InvalidDOMElementException::class);
203
204
        return new self(
205
            self::getAttribute($xml, 'Name'),
206
            self::getAttribute($xml, 'NameFormat', null),
207
            self::getAttribute($xml, 'FriendlyName', null),
208
            AttributeValue::getChildrenOfClass($xml),
209
            self::getAttributesNSFromXML($xml)
210
        );
211
    }
212
213
214
    /**
215
     * Convert this Attribute to XML.
216
     *
217
     * @param \DOMElement|null $parent The element we should append this Attribute to.
218
     * @return \DOMElement
219
     */
220
    public function toXML(DOMElement $parent = null): DOMElement
221
    {
222
        $e = $this->instantiateParentElement($parent);
223
        $e->setAttribute('Name', $this->Name);
224
225
        if ($this->NameFormat !== null) {
226
            $e->setAttribute('NameFormat', $this->NameFormat);
227
        }
228
229
        if ($this->FriendlyName !== null) {
230
            $e->setAttribute('FriendlyName', $this->FriendlyName);
231
        }
232
233
        foreach ($this->getAttributesNS() as $attr) {
234
            $e->setAttributeNS($attr['namespaceURI'], $attr['qualifiedName'], $attr['value']);
235
        }
236
237
        foreach ($this->AttributeValues as $av) {
238
            $av->toXML($e);
239
        }
240
241
        return $e;
242
    }
243
}
244