AbstractEncryptionMethod::getKeySize()   A
last analyzed

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 SimpleSAML\XMLSecurity\XML\xenc;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function array_pop;
16
17
/**
18
 * A class implementing the xenc:AbstractEncryptionMethod element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
abstract class AbstractEncryptionMethod extends AbstractXencElement
23
{
24
    use ExtendableElementTrait;
25
26
    /** The namespace-attribute for the xs:any element */
27
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
28
29
30
    /**
31
     * EncryptionMethod constructor.
32
     *
33
     * @param string $algorithm
34
     * @param \SimpleSAML\XMLSecurity\XML\xenc\KeySize|null $keySize
35
     * @param \SimpleSAML\XMLSecurity\XML\xenc\OAEPparams|null $oaepParams
36
     * @param list<\SimpleSAML\XML\SerializableElementInterface> $children
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\xenc\list was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
37
     */
38
    final public function __construct(
39
        protected string $algorithm,
40
        protected ?KeySize $keySize = null,
41
        protected ?OAEPparams $oaepParams = null,
42
        protected array $children = [],
43
    ) {
44
        Assert::validURI($algorithm, SchemaViolationException::class); // Covers the empty string
45
46
        $this->setElements($children);
47
    }
48
49
50
    /**
51
     * Get the URI identifying the algorithm used by this encryption method.
52
     *
53
     * @return string
54
     */
55
    public function getAlgorithm(): string
56
    {
57
        return $this->algorithm;
58
    }
59
60
61
    /**
62
     * Get the size of the key used by this encryption method.
63
     *
64
     * @return \SimpleSAML\XMLSecurity\XML\xenc\KeySize|null
65
     */
66
    public function getKeySize(): ?KeySize
67
    {
68
        return $this->keySize;
69
    }
70
71
72
    /**
73
     * Get the OAEP parameters.
74
     *
75
     * @return \SimpleSAML\XMLSecurity\XML\xenc\OAEPparams|null
76
     */
77
    public function getOAEPParams(): ?OAEPparams
78
    {
79
        return $this->oaepParams;
80
    }
81
82
83
    /**
84
     * Initialize an EncryptionMethod object from an existing XML.
85
     *
86
     * @param \DOMElement $xml
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
92
     *   if the supplied element is missing one of the mandatory attributes
93
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
94
     *   if too many child-elements of a type are specified
95
     */
96
    public static function fromXML(DOMElement $xml): static
97
    {
98
        Assert::same($xml->localName, 'EncryptionMethod', InvalidDOMElementException::class);
99
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
100
101
        $algorithm = self::getAttribute($xml, 'Algorithm');
102
103
        $keySize = KeySize::getChildrenOfClass($xml);
104
        Assert::maxCount($keySize, 1, TooManyElementsException::class);
105
106
        $oaepParams = OAEPparams::getChildrenOfClass($xml);
107
        Assert::maxCount($oaepParams, 1, TooManyElementsException::class);
108
109
        $children = self::getChildElementsFromXML($xml);
110
111
        return new static($algorithm, array_pop($keySize), array_pop($oaepParams), $children);
112
    }
113
114
115
    /**
116
     * Convert this EncryptionMethod object to XML.
117
     *
118
     * @param \DOMElement|null $parent The element we should append this EncryptionMethod to.
119
     * @return \DOMElement
120
     */
121
    public function toXML(?DOMElement $parent = null): DOMElement
122
    {
123
        $e = $this->instantiateParentElement($parent);
124
        $e->setAttribute('Algorithm', $this->getAlgorithm());
125
126
        $this->getKeySize()?->toXML($e);
127
        $this->getOAEPparams()?->toXML($e);
128
129
        foreach ($this->getElements() as $child) {
130
            $child->toXML($e);
131
        }
132
133
        return $e;
134
    }
135
}
136