Passed
Pull Request — master (#38)
by Tim
01:57
created

KeyValue::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 6
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\ExtendableElementTrait;
14
use SimpleSAML\XML\XMLElementInterface;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
17
18
/**
19
 * Class representing a ds:KeyValue element.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
final class KeyValue extends AbstractDsElement
24
{
25
    use ExtendableElementTrait;
26
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
30
31
    /** @var \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue */
32
    protected ?RSAKeyValue $RSAKeyValue;
33
34
    // DSA is not supported
35
    //protected ?DSAKeyValue $DSAKeyValue;
36
37
38
    /**
39
     * Initialize an KeyValue.
40
     *
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null $RSAKeyValue
42
     * @param \SimpleSAML\XML\XMLElementInterface|null $element
43
     */
44
    public function __construct(?RSAKeyValue $RSAKeyValue, ?XMLElementInterface $element = null)
45
    {
46
        Assert::false(
47
            is_null($RSAKeyValue) && is_null($element),
48
            'A <ds:KeyValue> requires either a RSAKeyValue or an element in namespace ##other'
49
        );
50
51
        $this->setRSAKeyValue($RSAKeyValue);
52
53
        if ($element !== null) {
54
            $this->setElements([$element]);
55
        }
56
    }
57
58
59
    /**
60
     * Collect the value of the RSAKeyValue-property
61
     *
62
     * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|null
63
     */
64
    public function getRSAKeyValue(): ?RSAKeyValue
65
    {
66
        return $this->RSAKeyValue;
67
    }
68
69
70
    /**
71
     * Set the value of the RSAKeyValue-property
72
     *
73
     * @param \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue $RSAKeyValue
74
     */
75
    private function setRSAKeyValue(?RSAKeyValue $RSAKeyValue): void
76
    {
77
        $this->RSAKeyValue = $RSAKeyValue;
78
    }
79
80
81
    /**
82
     * Convert XML into a KeyValue
83
     *
84
     * @param \DOMElement $xml The XML element we should load
85
     * @return self
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): self
91
    {
92
        Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class);
94
95
        $RSAKeyValue = RSAKeyValue::getChildrenOfClass($xml);
96
        Assert::maxCount(
97
            $RSAKeyValue,
98
            1,
99
            'A <ds:KeyValue> can contain exactly one <ds:RSAKeyValue>',
100
            TooManyElementsException::class
101
        );
102
103
        $elements = [];
104
        foreach ($xml->childNodes as $element) {
105
            if (!($element instanceof DOMElement) || $element->namespaceURI === KeyValue::NS) {
106
                continue;
107
            }
108
109
            $elements[] = new Chunk($element);
110
        }
111
        Assert::maxCount(
112
            $elements,
113
            1,
114
            'A <ds:KeyValue> can contain exactly one element in namespace ##other',
115
            TooManyElementsException::class
116
        );
117
118
        return new self(array_pop($RSAKeyValue), array_pop($elements));
119
    }
120
121
122
    /**
123
     * Convert this KeyValue element to XML.
124
     *
125
     * @param \DOMElement|null $parent The element we should append this KeyValue element to.
126
     * @return \DOMElement
127
     */
128
    public function toXML(DOMElement $parent = null): DOMElement
129
    {
130
        $e = $this->instantiateParentElement($parent);
131
132
        if ($this->RSAKeyValue !== null) {
133
            $this->RSAKeyValue->toXML($e);
134
        }
135
136
        foreach ($this->elements as $element) {
137
            $e->appendChild($e->ownerDocument->importNode($element->getXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

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

137
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($element->getXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        }
139
140
        return $e;
141
    }
142
}
143