Passed
Push — master ( 7a371e...4a8d98 )
by Tim
02:17
created

KeyDescriptor::getUse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
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
14
/**
15
 * Class representing a KeyDescriptor element.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
final class KeyDescriptor extends AbstractMdElement
20
{
21
    /**
22
     * KeyDescriptor constructor.
23
     *
24
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo $keyInfo
25
     * @param string|null $use
26
     * @param \SimpleSAML\SAML2\XML\md\EncryptionMethod[] $encryptionMethod
27
     */
28
    public function __construct(
29
        protected KeyInfo $keyInfo,
30
        protected ?string $use = null,
31
        protected array $encryptionMethod = []
32
    ) {
33
        Assert::nullOrOneOf(
34
            $use,
35
            ['encryption', 'signing'],
36
            'The "use" attribute of a KeyDescriptor can only be "encryption" or "signing".',
37
        );
38
        Assert::allIsInstanceOf($encryptionMethod, EncryptionMethod::class);
39
    }
40
41
42
    /**
43
     * Collect the value of the use property.
44
     *
45
     * @return string|null
46
     */
47
    public function getUse(): ?string
48
    {
49
        return $this->use;
50
    }
51
52
53
    /**
54
     * Collect the value of the KeyInfo property.
55
     *
56
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo
57
     */
58
    public function getKeyInfo(): KeyInfo
59
    {
60
        return $this->keyInfo;
61
    }
62
63
64
    /**
65
     * Collect the value of the EncryptionMethod property.
66
     *
67
     * @return \SimpleSAML\SAML2\XML\md\EncryptionMethod[]
68
     */
69
    public function getEncryptionMethod(): array
70
    {
71
        return $this->encryptionMethod;
72
    }
73
74
75
    /**
76
     * Initialize an KeyDescriptor.
77
     *
78
     * @param \DOMElement $xml The XML element we should load.
79
     * @return \SimpleSAML\SAML2\XML\md\KeyDescriptor
80
     *
81
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
82
     *   if the qualified name of the supplied element is wrong
83
     * @throws \SimpleSAML\XML\Exception\MissingElementException
84
     *   if one of the mandatory child-elements is missing
85
     * @throws \SimpleSAML\XML\Exception\TooManyElementsException
86
     *   if too many child-elements of a type are specified
87
     */
88
    public static function fromXML(DOMElement $xml): static
89
    {
90
        Assert::same($xml->localName, 'KeyDescriptor', InvalidDOMElementException::class);
91
        Assert::same($xml->namespaceURI, KeyDescriptor::NS, InvalidDOMElementException::class);
92
93
        $keyInfoElements = KeyInfo::getChildrenOfClass($xml);
94
        Assert::minCount(
95
            $keyInfoElements,
96
            1,
97
            'No ds:KeyInfo in the KeyDescriptor.',
98
            MissingElementException::class,
99
        );
100
        Assert::maxCount(
101
            $keyInfoElements,
102
            1,
103
            'More than one ds:KeyInfo in the KeyDescriptor.',
104
            TooManyElementsException::class,
105
        );
106
107
        return new static(
108
            $keyInfoElements[0],
109
            self::getAttribute($xml, 'use', null),
110
            EncryptionMethod::getChildrenOfClass($xml),
111
        );
112
    }
113
114
115
    /**
116
     * Convert this KeyDescriptor to XML.
117
     *
118
     * @param \DOMElement|null $parent The element we should append this KeyDescriptor to.
119
     * @return \DOMElement
120
     */
121
    public function toXML(DOMElement $parent = null): DOMElement
122
    {
123
        $e = $this->instantiateParentElement($parent);
124
125
        if ($this->getUse() !== null) {
126
            $e->setAttribute('use', $this->getUse());
127
        }
128
129
        $this->getKeyInfo()->toXML($e);
130
131
        foreach ($this->getEncryptionMethod() as $em) {
132
            $em->toXML($e);
133
        }
134
135
        return $e;
136
    }
137
}
138