Passed
Push — master ( 54bedc...fe2750 )
by Tim
02:40
created

AbstractContextItemType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 36
dl 0
loc 131
rs 10
c 1
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getScope() 0 3 1
A toXML() 0 20 4
A __construct() 0 18 2
A getName() 0 3 1
A getValue() 0 3 1
A fromXML() 0 17 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\auth;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableAttributesTrait;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\SerializableElementInterface;
15
use SimpleSAML\XML\XsNamespace as NS;
16
17
use function array_pop;
18
19
/**
20
 * Class defining the ContextItemType element
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractContextItemType extends AbstractAuthElement
25
{
26
    use ExtendableAttributesTrait;
27
    use ExtendableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\ExtendableElementTrait requires some properties which are not provided by SimpleSAML\WSSecurity\XM...AbstractContextItemType: $namespaceURI, $localName, $childNodes
Loading history...
28
29
    /** The namespace-attribute for the xs:anyAttribute */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
    /** The namespace-attribute for the xs:any */
33
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
34
35
36
    /**
37
     * AbstractContextItemType constructor
38
     *
39
     * @param string $Name
40
     * @param string|null $Scope
41
     * @param \SimpleSAML\WSSecurity\XML\auth\Value|null $value
42
     * @param \SimpleSAML\XML\SerializableElementInterface|null $child
43
     * @param list<\SimpleSAML\XML\Attribute> $namespacedAttributes
0 ignored issues
show
Bug introduced by
The type SimpleSAML\WSSecurity\XML\auth\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...
44
     */
45
    final public function __construct(
46
        protected string $Name,
47
        protected ?string $Scope = null,
48
        protected ?Value $value = null,
49
        ?SerializableElementInterface $child = null,
50
        array $namespacedAttributes = [],
51
    ) {
52
        Assert::validURI($Name);
53
        Assert::nullOrValidURI($Scope);
54
55
        // One of both must exist, they can't be both null
56
        Assert::inArray(null, [$value, $child], SchemaViolationException::class);
57
        Assert::notSame($value, $child, SchemaViolationException::class);
58
59
        if ($child !== null) {
60
            $this->setElements([$child]);
61
        }
62
        $this->setAttributesNS($namespacedAttributes);
63
    }
64
65
66
    /**
67
     * Get the value of the $value property.
68
     *
69
     * @return \SimpleSAML\WSSecurity\XML\auth\Value
70
     */
71
    public function getValue(): ?Value
72
    {
73
        return $this->value;
74
    }
75
76
77
    /**
78
     * Get the value of the Name property.
79
     *
80
     * @return string
81
     */
82
    public function getName(): string
83
    {
84
        return $this->Name;
85
    }
86
87
88
    /**
89
     * Get the value of the Scope property.
90
     *
91
     * @return string|null
92
     */
93
    public function getScope(): ?string
94
    {
95
        return $this->Scope;
96
    }
97
98
99
    /**
100
     * Create an instance of this object from its XML representation.
101
     *
102
     * @param \DOMElement $xml
103
     * @return static
104
     *
105
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
106
     *   if the qualified name of the supplied element is wrong
107
     */
108
    public static function fromXML(DOMElement $xml): static
109
    {
110
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
112
113
        $value = Value::getChildrenOfClass($xml);
114
        Assert::maxCount($value, 1, TooManyElementsException::class);
115
116
        $children = self::getChildElementsFromXML($xml);
117
        Assert::maxCount($children, 1, TooManyElementsException::class);
118
119
        return new static(
120
            self::getAttribute($xml, 'Name'),
121
            self::getOptionalAttribute($xml, 'Scope'),
122
            array_pop($value),
123
            array_pop($children),
124
            self::getAttributesNSFromXML($xml),
125
        );
126
    }
127
128
129
    /**
130
     * Add this ContextItemType to an XML element.
131
     *
132
     * @param \DOMElement $parent The element we should append this username token to.
133
     * @return \DOMElement
134
     */
135
    public function toXML(DOMElement $parent = null): DOMElement
136
    {
137
        $e = $this->instantiateParentElement($parent);
138
139
        $e->setAttribute('Name', $this->getName());
140
        if ($this->getScope() !== null) {
141
            $e->setAttribute('Scope', $this->getScope());
142
        }
143
144
        foreach ($this->getAttributesNS() as $attr) {
145
            $attr->toXML($e);
146
        }
147
148
        $this->getValue()?->toXML($e);
149
150
        foreach ($this->getElements() as $elt) {
151
            $elt->toXML($e);
152
        }
153
154
        return $e;
155
    }
156
}
157