Passed
Pull Request — master (#225)
by Jaime Pérez
02:23
created

EncryptedData::getEncryptionMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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