KeyDescriptor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 7
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\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Type\KeyTypesValue;
10
use SimpleSAML\XML\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XML\Constants 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...
11
use SimpleSAML\XML\SchemaValidatableElementInterface;
12
use SimpleSAML\XML\SchemaValidatableElementTrait;
13
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
14
use SimpleSAML\XMLSchema\Exception\MissingElementException;
15
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
16
use SimpleSAML\XMLSecurity\XML\ds\KeyInfo;
17
18
use function array_pop;
19
20
/**
21
 * Class representing a KeyDescriptor element.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
final class KeyDescriptor extends AbstractMdElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\AbstractMdElement 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...
26
{
27
    use SchemaValidatableElementTrait;
28
29
30
    /**
31
     * KeyDescriptor constructor.
32
     *
33
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo $keyInfo
34
     * @param \SimpleSAML\SAML2\Type\KeyTypesValue|null $use
35
     * @param \SimpleSAML\SAML2\XML\md\EncryptionMethod[] $encryptionMethod
36
     */
37
    public function __construct(
38
        protected KeyInfo $keyInfo,
39
        protected ?KeyTypesValue $use = null,
40
        protected array $encryptionMethod = [],
41
    ) {
42
        Assert::maxCount($encryptionMethod, C::UNBOUNDED_LIMIT);
43
        Assert::allIsInstanceOf($encryptionMethod, EncryptionMethod::class);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\md\EncryptionMethod 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...
44
    }
45
46
47
    /**
48
     * Collect the value of the use property.
49
     *
50
     * @return \SimpleSAML\SAML2\Type\KeyTypesValue|null
51
     */
52
    public function getUse(): ?KeyTypesValue
53
    {
54
        return $this->use;
55
    }
56
57
58
    /**
59
     * Collect the value of the KeyInfo property.
60
     *
61
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo
62
     */
63
    public function getKeyInfo(): KeyInfo
64
    {
65
        return $this->keyInfo;
66
    }
67
68
69
    /**
70
     * Collect the value of the EncryptionMethod property.
71
     *
72
     * @return \SimpleSAML\SAML2\XML\md\EncryptionMethod[]
73
     */
74
    public function getEncryptionMethod(): array
75
    {
76
        return $this->encryptionMethod;
77
    }
78
79
80
    /**
81
     * Initialize an KeyDescriptor.
82
     *
83
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
84
     *   if the qualified name of the supplied element is wrong
85
     * @throws \SimpleSAML\XMLSchema\Exception\MissingElementException
86
     *   if one of the mandatory child-elements is missing
87
     * @throws \SimpleSAML\XMLSchema\Exception\TooManyElementsException
88
     *   if too many child-elements of a type are specified
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, 'KeyDescriptor', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, KeyDescriptor::NS, InvalidDOMElementException::class);
94
95
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
96
        Assert::minCount($keyInfo, 1, 'No ds:KeyInfo in the KeyDescriptor.', MissingElementException::class);
97
        Assert::maxCount($keyInfo, 1, 'Too many ds:KeyInfo in the KeyDescriptor.', TooManyElementsException::class);
98
99
        return new static(
100
            array_pop($keyInfo),
101
            self::getOptionalAttribute($xml, 'use', KeyTypesValue::class, null),
102
            EncryptionMethod::getChildrenOfClass($xml),
103
        );
104
    }
105
106
107
    /**
108
     * Convert this KeyDescriptor to XML.
109
     */
110
    public function toXML(?DOMElement $parent = null): DOMElement
111
    {
112
        $e = $this->instantiateParentElement($parent);
113
114
        if ($this->getUse() !== null) {
115
            $e->setAttribute('use', $this->getUse()->getValue());
116
        }
117
118
        $this->getKeyInfo()->toXML($e);
119
120
        foreach ($this->getEncryptionMethod() as $em) {
121
            $em->toXML($e);
122
        }
123
124
        return $e;
125
    }
126
}
127