Passed
Push — master ( 0f9cd8...cbd28b )
by Tim
02:05
created

KeyInfo::fromXML()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 37
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 27
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 37
rs 8.8657
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\XMLSecurity\Constants as C;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
use SimpleSAML\XMLSecurity\XML\dsig11\KeyInfoReference;
14
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedData;
15
use SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey;
16
17
/**
18
 * Class representing a ds:KeyInfo element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class KeyInfo extends AbstractDsElement
23
{
24
    /**
25
     * The Id attribute on this element.
26
     *
27
     * @var string|null
28
     */
29
    protected ?string $Id = null;
30
31
    /**
32
     * The various key information elements.
33
     *
34
     * Array with various elements describing this key.
35
     * Unknown elements will be represented by \SimpleSAML\XML\Chunk.
36
     *
37
     * @var list<\SimpleSAML\XML\Chunk|
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\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...
38
     *       \SimpleSAML\XMLSecurity\XML\ds\KeyName|
39
     *       \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
40
     *       \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
41
     *       \SimpleSAML\XMLSecurity\XML\ds\X509Data|
42
     *       \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
43
     *       \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey>
44
     */
45
    protected array $info = [];
46
47
48
    /**
49
     * Initialize a KeyInfo element.
50
     *
51
     * @param list<\SimpleSAML\XML\Chunk|
52
     *         \SimpleSAML\XMLSecurity\XML\ds\KeyName|
53
     *         \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
54
     *         \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
55
     *         \SimpleSAML\XMLSecurity\XML\ds\X509Data|
56
     *         \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
57
     *         \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey> $info
58
     * @param string|null $Id
59
     */
60
    public function __construct(array $info, ?string $Id = null)
61
    {
62
        $this->setInfo($info);
63
        $this->setId($Id);
64
    }
65
66
67
    /**
68
     * Collect the value of the Id-property
69
     *
70
     * @return string|null
71
     */
72
    public function getId(): ?string
73
    {
74
        return $this->Id;
75
    }
76
77
78
    /**
79
     * Set the value of the Id-property
80
     *
81
     * @param string|null $id
82
     */
83
    private function setId(string $Id = null): void
84
    {
85
        Assert::nullOrValidNCName($Id);
86
        $this->Id = $Id;
87
    }
88
89
90
    /**
91
     * Collect the value of the info-property
92
     *
93
     * @return (\SimpleSAML\XML\Chunk|
0 ignored issues
show
Documentation Bug introduced by
The doc comment (\SimpleSAML\XML\Chunk| at position 3 could not be parsed: the token is null at position 3.
Loading history...
94
     *          \SimpleSAML\XMLSecurity\XML\ds\KeyName|
95
     *          \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
96
     *          \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
97
     *          \SimpleSAML\XMLSecurity\XML\ds\X509Data|
98
     *          \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
99
     *          \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey)[]
100
     */
101
    public function getInfo(): array
102
    {
103
        return $this->info;
104
    }
105
106
107
    /**
108
     * Set the value of the info-property
109
     *
110
     * @param (\SimpleSAML\XML\Chunk|
111
     *         \SimpleSAML\XMLSecurity\XML\ds\KeyName|
112
     *         \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
113
     *         \SimpleSAML\XMLSecurity\XML\ds\KeyValue|
114
     *         \SimpleSAML\XMLSecurity\XML\ds\RetrievalMethod|
115
     *         \SimpleSAML\XMLSecurity\XML\ds\X509Data|
116
     *         \SimpleSAML\XMLSecurity\XML\xenc\EncryptedData|
117
     *         \SimpleSAML\XMLSecurity\XML\xenc\EncryptedKey)[] $info
118
     * @throws \SimpleSAML\Assert\AssertionFailedException  if $info contains
119
     *   anything other than KeyName, KeyValue, RetrievalMethod, X509Data, EncryptedData, EncryptedKey or Chunk
120
     */
121
    private function setInfo(array $info): void
122
    {
123
        Assert::notEmpty($info, 'ds:KeyInfo cannot be empty', InvalidArgumentException::class);
124
        Assert::allIsInstanceOfAny(
125
            $info,
126
            [
127
                Chunk::class,
128
                KeyName::class,
129
                KeyValue::class,
130
                RetrievalMethod::class,
131
                X509Data::class,
132
                EncryptedData::class,
133
                EncryptedKey::class,
134
            ],
135
            'KeyInfo can only contain instances of KeyName, X509Data, EncryptedKey or Chunk.',
136
            InvalidArgumentException::class,
137
        );
138
        $this->info = $info;
0 ignored issues
show
Documentation Bug introduced by
It seems like $info of type array is incompatible with the declared type SimpleSAML\XMLSecurity\XML\ds\list of property $info.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
139
    }
140
141
142
    /**
143
     * Convert XML into a KeyInfo
144
     *
145
     * @param \DOMElement $xml The XML element we should load
146
     * @return static
147
     *
148
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
149
     *   If the qualified name of the supplied element is wrong
150
     */
151
    public static function fromXML(DOMElement $xml): static
152
    {
153
        Assert::same($xml->localName, 'KeyInfo', InvalidDOMElementException::class);
154
        Assert::same($xml->namespaceURI, KeyInfo::NS, InvalidDOMElementException::class);
155
156
        $Id = self::getAttribute($xml, 'Id', null);
157
        $info = [];
158
159
        foreach ($xml->childNodes as $n) {
160
            if (!($n instanceof DOMElement)) {
161
                continue;
162
            } elseif ($n->namespaceURI === C::NS_XDSIG) {
163
                $info[] = match ($n->localName) {
164
                    'KeyName' => KeyName::fromXML($n),
165
                    'KeyValue' => KeyValue::fromXML($n),
166
                    'RetrievalMethod' => RetrievalMethod::fromXML($n),
167
                    'X509Data' => X509Data::fromXML($n),
168
                    default => new Chunk($n),
169
                };
170
            } elseif ($n->namespaceURI === C::NS_XDSIG11) {
171
                $info[] = match ($n->localName) {
172
                    'KeyInfoReference' => KeyInfoReference::fromXML($n),
173
                    default => new Chunk($n),
174
                };
175
            } elseif ($n->namespaceURI === C::NS_XENC) {
176
                $info[] = match ($n->localName) {
177
                    'EncryptedData' => EncryptedData::fromXML($n),
178
                    'EncryptedKey' => EncryptedKey::fromXML($n),
179
                    default => new Chunk($n),
180
                };
181
            } else {
182
                $info[] = new Chunk($n);
183
                break;
184
            }
185
        }
186
187
        return new static($info, $Id);
188
    }
189
190
    /**
191
     * Convert this KeyInfo to XML.
192
     *
193
     * @param \DOMElement|null $parent The element we should append this KeyInfo to.
194
     * @return \DOMElement
195
     */
196
    public function toXML(DOMElement $parent = null): DOMElement
197
    {
198
        $e = $this->instantiateParentElement($parent);
199
200
        if ($this->Id !== null) {
201
            $e->setAttribute('Id', $this->Id);
202
        }
203
204
        foreach ($this->info as $n) {
205
            $n->toXML($e);
206
        }
207
208
        return $e;
209
    }
210
}
211