Passed
Push — master ( 08720a...234432 )
by Tim
02:02
created

AbstractEncryptedType::fromXML()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 29
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 43
rs 9.456
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\SchemaViolationException;
10
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
11
12
use function count;
13
14
/**
15
 * Abstract class representing encrypted data.
16
 *
17
 * Note: <xenc:EncryptionProperties> elements are not supported.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
abstract class AbstractEncryptedType extends AbstractXencElement
22
{
23
    /** @var \SimpleSAML\XMLSecurity\XML\xenc\CipherData */
24
    protected CipherData $cipherData;
25
26
    /** @var string|null */
27
    protected ?string $encoding;
28
29
    /** @var \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null */
30
    protected ?EncryptionMethod $encryptionMethod;
31
32
    /** @var string|null */
33
    protected ?string $id;
34
35
    /** @var \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null */
36
    protected ?KeyInfo $keyInfo;
37
38
    /** @var string|null */
39
    protected ?string $mimeType;
40
41
    /** @var string|null */
42
    protected ?string $type;
43
44
45
    /**
46
     * EncryptedData constructor.
47
     *
48
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherData $cipherData The CipherData object of this EncryptedData.
49
     * @param string|null $id The Id attribute of this object. Optional.
50
     * @param string|null $type The Type attribute of this object. Optional.
51
     * @param string|null $mimeType The MimeType attribute of this object. Optional.
52
     * @param string|null $encoding The Encoding attribute of this object. Optional.
53
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null $encryptionMethod
54
     *   The EncryptionMethod object of this EncryptedData. Optional.
55
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo The KeyInfo object of this EncryptedData. Optional.
56
     */
57
    public function __construct(
58
        CipherData $cipherData,
59
        ?string $id = null,
60
        ?string $type = null,
61
        ?string $mimeType = null,
62
        ?string $encoding = null,
63
        ?EncryptionMethod $encryptionMethod = null,
64
        ?KeyInfo $keyInfo = null,
65
    ) {
66
        $this->setCipherData($cipherData);
67
        $this->setEncoding($encoding);
68
        $this->setID($id);
69
        $this->setMimeType($mimeType);
70
        $this->setType($type);
71
        $this->setEncryptionMethod($encryptionMethod);
72
        $this->setKeyInfo($keyInfo);
73
    }
74
75
76
    /**
77
     * Get the CipherData object.
78
     *
79
     * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherData
80
     */
81
    public function getCipherData(): CipherData
82
    {
83
        return $this->cipherData;
84
    }
85
86
87
    /**
88
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherData $cipherData
89
     */
90
    protected function setCipherData(CipherData $cipherData): void
91
    {
92
        $this->cipherData = $cipherData;
93
    }
94
95
96
    /**
97
     * Get the value of the Encoding attribute.
98
     *
99
     * @return string|null
100
     */
101
    public function getEncoding(): ?string
102
    {
103
        return $this->encoding;
104
    }
105
106
107
    /**
108
     * @param string|null $encoding
109
     */
110
    protected function setEncoding(?string $encoding): void
111
    {
112
        Assert::nullOrValidURI($encoding, SchemaViolationException::class); // Covers the empty string
113
        $this->encoding = $encoding;
114
    }
115
116
117
    /**
118
     * Get the EncryptionMethod object.
119
     *
120
     * @return \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null
121
     */
122
    public function getEncryptionMethod(): ?EncryptionMethod
123
    {
124
        return $this->encryptionMethod;
125
    }
126
127
128
    /**
129
     * @param \SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod|null $encryptionMethod
130
     */
131
    protected function setEncryptionMethod(?EncryptionMethod $encryptionMethod): void
132
    {
133
        $this->encryptionMethod = $encryptionMethod;
134
    }
135
136
137
    /**
138
     * Get the value of the Id attribute.
139
     *
140
     * @return string
141
     */
142
    public function getID(): ?string
143
    {
144
        return $this->id;
145
    }
146
147
148
    /**
149
     * @param string|null $id
150
     */
151
    protected function setID(?string $id): void
152
    {
153
        Assert::nullOrValidNCName($id, SchemaViolationException::class); // Covers the empty string
154
        $this->id = $id;
155
    }
156
157
158
    /**
159
     * Get the KeyInfo object.
160
     *
161
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
162
     */
163
    public function getKeyInfo(): ?KeyInfo
164
    {
165
        return $this->keyInfo;
166
    }
167
168
169
    /**
170
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
171
     */
172
    protected function setKeyInfo(?KeyInfo $keyInfo): void
173
    {
174
        $this->keyInfo = $keyInfo;
175
    }
176
177
178
    /**
179
     * Get the value of the MimeType attribute.
180
     *
181
     * @return string
182
     */
183
    public function getMimeType(): ?string
184
    {
185
        return $this->mimeType;
186
    }
187
188
189
    /**
190
     * @param string|null $mimeType
191
     */
192
    protected function setMimeType(?string $mimeType): void
193
    {
194
        $this->mimeType = $mimeType;
195
    }
196
197
198
    /**
199
     * Get the value of the Type attribute.
200
     *
201
     * @return string|null
202
     */
203
    public function getType(): ?string
204
    {
205
        return $this->type;
206
    }
207
208
209
    /**
210
     * @param string|null $type
211
     */
212
    protected function setType(?string $type): void
213
    {
214
        Assert::nullOrValidURI($type, SchemaViolationException::class); // Covers the empty string
215
        $this->type = $type;
216
    }
217
218
219
    /**
220
     * @inheritDoc
221
     */
222
    public function toXML(DOMElement $parent = null): DOMElement
223
    {
224
        $e = $this->instantiateParentElement($parent);
225
226
        if ($this->id !== null) {
227
            $e->setAttribute('Id', $this->id);
228
        }
229
230
        if ($this->type !== null) {
231
            $e->setAttribute('Type', $this->type);
232
        }
233
234
        if ($this->mimeType !== null) {
235
            $e->setAttribute('MimeType', $this->mimeType);
236
        }
237
238
        if ($this->encoding !== null) {
239
            $e->setAttribute('Encoding', $this->encoding);
240
        }
241
242
        if ($this->encryptionMethod !== null) {
243
            $this->encryptionMethod->toXML($e);
244
        }
245
246
        if ($this->keyInfo !== null) {
247
            $this->keyInfo->toXML($e);
248
        }
249
250
        $this->cipherData->toXML($e);
251
252
        return $e;
253
    }
254
}
255