EncryptedData::fromXML()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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