Passed
Pull Request — master (#64)
by Tim
03:03 queued 01:02
created

KeyValue::__construct()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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