Passed
Pull Request — master (#58)
by Tim
02:14
created

SignatureMethod::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 11
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\ds;
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\SerializableElementInterface;
14
use SimpleSAML\XML\XsNamespace as NS;
15
use SimpleSAML\XMLSecurity\Constants as C;
16
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
17
18
use function array_keys;
19
use function array_merge;
20
use function array_pop;
21
22
/**
23
 * Class representing a ds:SignatureMethod element.
24
 *
25
 * @package simplesamlphp/xml-security
26
 */
27
final class SignatureMethod extends AbstractDsElement
28
{
29
    use ExtendableElementTrait;
30
31
    /** The namespace-attribute for the xs:any element */
32
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
33
34
35
    /**
36
     * Initialize a SignatureMethod element.
37
     *
38
     * @param string $Algorithm
39
     * @param \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null $hmacOutputLength
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\ds\HMACOutputLength 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...
40
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
41
     */
42
    public function __construct(
43
        protected string $Algorithm,
44
        protected ?HMACOutputLength $hmacOutputLength = null,
45
        array $children = [],
46
    ) {
47
        Assert::validURI($Algorithm, SchemaViolationException::class);
48
        Assert::oneOf(
49
            $Algorithm,
50
            array_merge(
51
                array_keys(C::$RSA_DIGESTS),
52
                array_keys(C::$HMAC_DIGESTS),
53
            ),
54
            'Invalid signature method: %s',
55
            InvalidArgumentException::class,
56
        );
57
58
        $this->setElements($children);
59
    }
60
61
62
    /**
63
     * Collect the value of the Algorithm-property
64
     *
65
     * @return string
66
     */
67
    public function getAlgorithm(): string
68
    {
69
        return $this->Algorithm;
70
    }
71
72
73
    /**
74
     * Collect the value of the hmacOutputLength-property
75
     *
76
     * @return \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null
77
     */
78
    public function getHMACOutputLength(): ?HMACOutputLength
79
    {
80
        return $this->hmacOutputLength;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hmacOutputLength also could return the type SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength which is incompatible with the documented return type SimpleSAML\XMLSecurity\ds\HMACOutputLength|null.
Loading history...
81
    }
82
83
84
    /**
85
     * Convert XML into a SignatureMethod
86
     *
87
     * @param \DOMElement $xml The XML element we should load
88
     * @return static
89
     *
90
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
91
     *   If the qualified name of the supplied element is wrong
92
     */
93
    public static function fromXML(DOMElement $xml): static
94
    {
95
        Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
96
        Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
97
98
        $Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
99
100
        $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml);
101
        Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class);
102
103
        return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml));
104
    }
105
106
107
    /**
108
     * Convert this SignatureMethod element to XML.
109
     *
110
     * @param \DOMElement|null $parent The element we should append this SignatureMethod element to.
111
     * @return \DOMElement
112
     */
113
    public function toXML(DOMElement $parent = null): DOMElement
114
    {
115
        $e = $this->instantiateParentElement($parent);
116
        $e->setAttribute('Algorithm', $this->getAlgorithm());
117
118
        $this->getHMACOutputLength()?->toXML($e);
119
120
        foreach ($this->getElements() as $elt) {
121
            $elt->toXML($e);
122
        }
123
124
        return $e;
125
    }
126
}
127