CipherData   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 29
c 2
b 0
f 0
dl 0
loc 95
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 8 1
A __construct() 0 13 2
A getCipherValue() 0 3 1
A fromXML() 0 24 3
A getCipherReference() 0 3 1
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\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
14
use function array_pop;
15
16
/**
17
 * Class representing <xenc:CipherData>.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
class CipherData extends AbstractXencElement implements SchemaValidatableElementInterface
22
{
23
    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\CipherData: $message, $line
Loading history...
24
25
26
    /**
27
     * CipherData constructor.
28
     *
29
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherValue|null $cipherValue
30
     * @param \SimpleSAML\XMLSecurity\XML\xenc\CipherReference|null $cipherReference
31
     */
32
    final public function __construct(
33
        protected ?CipherValue $cipherValue,
34
        protected ?CipherReference $cipherReference = null,
35
    ) {
36
        Assert::oneOf(
37
            null,
38
            [$cipherValue, $cipherReference],
39
            'Can only have one of CipherValue/CipherReference',
40
        );
41
42
        Assert::false(
43
            is_null($cipherValue) && is_null($cipherReference),
44
            'You need either a CipherValue or a CipherReference',
45
        );
46
    }
47
48
49
    /**
50
     * Get the value of the $cipherValue property.
51
     *
52
     * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherValue|null
53
     */
54
    public function getCipherValue(): ?CipherValue
55
    {
56
        return $this->cipherValue;
57
    }
58
59
60
    /**
61
     * Get the CipherReference element inside this CipherData object.
62
     *
63
     * @return \SimpleSAML\XMLSecurity\XML\xenc\CipherReference|null
64
     */
65
    public function getCipherReference(): ?CipherReference
66
    {
67
        return $this->cipherReference;
68
    }
69
70
71
    /**
72
     * @inheritDoc
73
     *
74
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
75
     *   If the qualified name of the supplied element is wrong
76
     */
77
    public static function fromXML(DOMElement $xml): static
78
    {
79
        Assert::same($xml->localName, 'CipherData', InvalidDOMElementException::class);
80
        Assert::same($xml->namespaceURI, CipherData::NS, InvalidDOMElementException::class);
81
82
        $cv = CipherValue::getChildrenOfClass($xml);
83
        Assert::maxCount(
84
            $cv,
85
            1,
86
            'More than one CipherValue element in <xenc:CipherData>',
87
            TooManyElementsException::class,
88
        );
89
90
        $cr = CipherReference::getChildrenOfClass($xml);
91
        Assert::maxCount(
92
            $cr,
93
            1,
94
            'More than one CipherReference element in <xenc:CipherData>',
95
            TooManyElementsException::class,
96
        );
97
98
        return new static(
99
            empty($cv) ? null : array_pop($cv),
100
            empty($cr) ? null : array_pop($cr),
101
        );
102
    }
103
104
105
    /**
106
     * @inheritDoc
107
     */
108
    public function toXML(?DOMElement $parent = null): DOMElement
109
    {
110
        $e = $this->instantiateParentElement($parent);
111
112
        $this->getCipherValue()?->toXML($e);
113
        $this->getCipherReference()?->toXML($e);
114
115
        return $e;
116
    }
117
}
118