AbstractEncryptionMethod::getAlgorithm()   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\ExtendableElementTrait;
10
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSchema\Type\AnyURIValue;
13
use SimpleSAML\XMLSchema\XML\Constants\NS;
14
15
use function array_pop;
16
use function strval;
17
18
/**
19
 * A class implementing the xenc:AbstractEncryptionMethod element.
20
 *
21
 * @package simplesamlphp/xml-security
22
 */
23
abstract class AbstractEncryptionMethod extends AbstractXencElement
24
{
25
    use ExtendableElementTrait;
26
27
28
    /** The namespace-attribute for the xs:any element */
29
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
30
31
32
    /**
33
     * EncryptionMethod constructor.
34
     *
35
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $algorithm
36
     * @param \SimpleSAML\XMLSecurity\XML\xenc\KeySize|null $keySize
37
     * @param \SimpleSAML\XMLSecurity\XML\xenc\OAEPparams|null $oaepParams
38
     * @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...
39
     */
40
    final public function __construct(
41
        protected AnyURIValue $algorithm,
42
        protected ?KeySize $keySize = null,
43
        protected ?OAEPparams $oaepParams = null,
44
        array $children = [],
45
    ) {
46
        $this->setElements($children);
47
    }
48
49
50
    /**
51
     * Get the URI identifying the algorithm used by this encryption method.
52
     *
53
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
54
     */
55
    public function getAlgorithm(): AnyURIValue
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\XMLSchema\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
92
     *   if the supplied element is missing one of the mandatory attributes
93
     * @throws \SimpleSAML\XMLSchema\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
        $keySize = KeySize::getChildrenOfClass($xml);
102
        Assert::maxCount($keySize, 1, TooManyElementsException::class);
103
104
        $oaepParams = OAEPparams::getChildrenOfClass($xml);
105
        Assert::maxCount($oaepParams, 1, TooManyElementsException::class);
106
107
        return new static(
108
            self::getAttribute($xml, 'Algorithm', AnyURIValue::class),
109
            array_pop($keySize),
110
            array_pop($oaepParams),
111
            self::getChildElementsFromXML($xml),
112
        );
113
    }
114
115
116
    /**
117
     * Convert this EncryptionMethod object to XML.
118
     *
119
     * @param \DOMElement|null $parent The element we should append this EncryptionMethod to.
120
     * @return \DOMElement
121
     */
122
    public function toXML(?DOMElement $parent = null): DOMElement
123
    {
124
        $e = $this->instantiateParentElement($parent);
125
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
126
127
        $this->getKeySize()?->toXML($e);
128
        $this->getOAEPparams()?->toXML($e);
129
130
        foreach ($this->getElements() as $child) {
131
            $child->toXML($e);
132
        }
133
134
        return $e;
135
    }
136
}
137