Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

SignatureMethod::setAlgorithm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 9
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 14
rs 9.9666
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\XMLSecurity\Constants as C;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
14
/**
15
 * Class representing a ds:SignatureMethod element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class SignatureMethod extends AbstractDsElement
20
{
21
    /**
22
     * Initialize a SignatureMethod element.
23
     *
24
     * @param string $Algorithm
25
     */
26
    public function __construct(
27
        protected string $Algorithm,
28
    ) {
29
        Assert::validURI($Algorithm, SchemaViolationException::class);
30
        Assert::oneOf(
31
            $Algorithm,
32
            array_merge(
33
                array_keys(C::$RSA_DIGESTS),
34
                array_keys(C::$HMAC_DIGESTS),
35
            ),
36
            'Invalid signature method: %s',
37
            InvalidArgumentException::class,
38
        );
39
    }
40
41
42
    /**
43
     * Collect the value of the Algorithm-property
44
     *
45
     * @return string
46
     */
47
    public function getAlgorithm(): string
48
    {
49
        return $this->Algorithm;
50
    }
51
52
53
    /**
54
     * Convert XML into a SignatureMethod
55
     *
56
     * @param \DOMElement $xml The XML element we should load
57
     * @return static
58
     *
59
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
60
     *   If the qualified name of the supplied element is wrong
61
     */
62
    public static function fromXML(DOMElement $xml): static
63
    {
64
        Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
65
        Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
66
67
        /** @psalm-var string $Algorithm */
68
        $Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
69
70
        return new static($Algorithm);
71
    }
72
73
74
    /**
75
     * Convert this SignatureMethod element to XML.
76
     *
77
     * @param \DOMElement|null $parent The element we should append this SignatureMethod element to.
78
     * @return \DOMElement
79
     */
80
    public function toXML(DOMElement $parent = null): DOMElement
81
    {
82
        $e = $this->instantiateParentElement($parent);
83
        $e->setAttribute('Algorithm', $this->getAlgorithm());
84
85
        return $e;
86
    }
87
}
88