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

EncryptedData::fromXML()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 29
c 0
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\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
13
use SimpleSAML\XMLSecurity\XML\xenc\CipherData;
14
use SimpleSAML\XMLSecurity\XML\xenc\EncryptionMethod;
15
16
/**
17
 * Class containing encrypted data.
18
 *
19
 * Note: <xenc:EncryptionProperties> elements are not supported.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
final class EncryptedData extends AbstractEncryptedType
24
{
25
    /**
26
     * @inheritDoc
27
     *
28
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
29
     *   If the qualified name of the supplied element is wrong
30
     * @throws \SimpleSAML\XML\Exception\MissingElementException
31
     *   If one of the mandatory child-elements is missing
32
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
33
     *   If too many child-elements of a type are specified
34
     */
35
    final public static function fromXML(DOMElement $xml): static
36
    {
37
        Assert::same($xml->localName, 'EncryptedData', InvalidDOMElementException::class);
38
        Assert::same($xml->namespaceURI, EncryptedData::NS, InvalidDOMElementException::class);
39
40
        $cipherData = CipherData::getChildrenOfClass($xml);
41
        Assert::minCount(
42
            $cipherData,
43
            1,
44
            'At least one CipherData element found in <xenc:EncryptedData>.',
45
            MissingElementException::class,
46
        );
47
        Assert::maxCount(
48
            $cipherData,
49
            1,
50
            'No or more than one CipherData element found in <xenc:EncryptedData>.',
51
            TooManyElementsException::class,
52
        );
53
54
        $encryptionMethod = EncryptionMethod::getChildrenOfClass($xml);
55
        Assert::maxCount(
56
            $encryptionMethod,
57
            1,
58
            'No more than one EncryptionMethod element allowed in <xenc:EncryptedData>.',
59
            TooManyElementsException::class,
60
        );
61
62
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
63
        Assert::maxCount(
64
            $keyInfo,
65
            1,
66
            'No more than one KeyInfo element allowed in <xenc:EncryptedData>.',
67
            TooManyElementsException::class,
68
        );
69
70
        return new static(
71
            $cipherData[0],
72
            self::getAttribute($xml, 'Id', null),
73
            self::getAttribute($xml, 'Type', null),
74
            self::getAttribute($xml, 'MimeType', null),
75
            self::getAttribute($xml, 'Encoding', null),
76
            count($encryptionMethod) === 1 ? $encryptionMethod[0] : null,
77
            count($keyInfo) === 1 ? $keyInfo[0] : null,
78
        );
79
    }
80
}
81