Passed
Pull Request — master (#64)
by Tim
02:05
created

KeyValue::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
rs 10
c 1
b 0
f 0
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
use SimpleSAML\XML\SerializableElementInterface;
16
use SimpleSAML\XML\XsNamespace as NS;
17
use SimpleSAML\XMLSecurity\Constants as C;
18
19
use function array_merge;
20
use function array_pop;
21
22
/**
23
 * Class representing a ds:KeyValue element.
24
 *
25
 * @package simplesamlphp/xml-security
26
 */
27
final class KeyValue extends AbstractDsElement implements SchemaValidatableElementInterface
28
{
29
    use ExtendableElementTrait {
30
        // We use our own getter instead of the trait's one
31
        getElements as private;
32
        setElements as private;
33
    }
34
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\ds\KeyValue: $message, $line
Loading history...
35
36
37
    /** The namespace-attribute for the xs:any element */
38
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
39
40
41
    /**
42
     * Initialize an KeyValue.
43
     *
44
     * @param \SimpleSAML\XML\SerializableElementInterface $keyValue
45
     */
46
    final public function __construct(
47
        protected RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface $keyValue,
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\ECKeyValue 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...
48
    ) {
49
        if (!(
50
            $keyValue instanceof RSAKeyValue
51
            || $keyValue instanceof DSAKeyValue
52
            || $keyValue instanceof ECKeyValue
53
        )) {
54
            Assert::true(
55
                (
56
                    ($keyValue instanceof Chunk)
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\Chunk 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...
57
                    ? $keyValue->getNamespaceURI()
58
                    : $keyValue::getNameSpaceURI()
59
                ) !== C::NS_XDSIG,
60
                'A <ds:KeyValue> requires either a RSAKeyValue, DSAKeyValue, ECKeyValue '
61
                . 'or an element in namespace ##other',
62
                SchemaViolationException::class,
63
            );
64
        }
65
    }
66
67
68
    /**
69
     * Collect the value of the RSAKeyValue-property
70
     *
71
     * @return \SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue|
72
     *         \SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue|
73
     *         \SimpleSAML\XMLSecurity\XML\dsig11\ECKeyValue|
74
     *         \SimpeSAML\XML\SerializableElementInterface
75
     */
76
    public function getKeyValue(): RSAKeyValue|DSAKeyValue|ECKeyValue|SerializableElementInterface
77
    {
78
        return $this->keyValue;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->keyValue also could return the type SimpleSAML\XMLSecurity\XML\ds\DSAKeyValue which is incompatible with the documented return type SimpleSAML\XMLSecurity\XML\ds\RSAKeyValue.
Loading history...
79
    }
80
81
82
    /**
83
     * Convert XML into a KeyValue
84
     *
85
     * @param \DOMElement $xml The XML element we should load
86
     * @return static
87
     *
88
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
89
     *   If the qualified name of the supplied element is wrong
90
     */
91
    public static function fromXML(DOMElement $xml): static
92
    {
93
        Assert::same($xml->localName, 'KeyValue', InvalidDOMElementException::class);
94
        Assert::same($xml->namespaceURI, KeyValue::NS, InvalidDOMElementException::class);
95
96
        $keyValue = array_merge(
97
            RSAKeyValue::getChildrenOfClass($xml),
98
            DSAKeyValue::getChildrenOfClass($xml),
99
            self::getChildElementsFromXML($xml),
100
        );
101
102
        Assert::count(
103
            $keyValue,
104
            1,
105
            'A <ds:KeyValue> must contain exactly one child element',
106
            TooManyElementsException::class,
107
        );
108
109
        return new static(array_pop($keyValue));
110
    }
111
112
113
    /**
114
     * Convert this KeyValue element to XML.
115
     *
116
     * @param \DOMElement|null $parent The element we should append this KeyValue element to.
117
     * @return \DOMElement
118
     */
119
    public function toXML(?DOMElement $parent = null): DOMElement
120
    {
121
        $e = $this->instantiateParentElement($parent);
122
123
        $this->getKeyValue()->toXML($e);
124
125
        return $e;
126
    }
127
}
128