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

SignatureMethod::fromXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 8
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\XMLSecurity\Constants;
11
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
12
13
/**
14
 * Class representing a ds:SignatureMethod element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class SignatureMethod extends AbstractDsElement
19
{
20
    /**
21
     * The algorithm.
22
     *
23
     * @var string
24
     */
25
    protected string $Algorithm;
26
27
28
    /**
29
     * Initialize a SignatureMethod element.
30
     *
31
     * @param string $algorithm
32
     */
33
    public function __construct(string $algorithm)
34
    {
35
        $this->setAlgorithm($algorithm);
36
    }
37
38
39
    /**
40
     * Collect the value of the Algorithm-property
41
     *
42
     * @return string
43
     */
44
    public function getAlgorithm(): string
45
    {
46
        return $this->Algorithm;
47
    }
48
49
50
    /**
51
     * Set the value of the Algorithm-property
52
     *
53
     * @param string $algorithm
54
     */
55
    private function setAlgorithm(string $algorithm): void
56
    {
57
        Assert::oneOf(
58
            $algorithm,
59
            [
60
                Constants::SIG_RSA_SHA1,
61
                Constants::SIG_RSA_SHA224,
62
                Constants::SIG_RSA_SHA256,
63
                Constants::SIG_RSA_SHA384,
64
                Constants::SIG_RSA_SHA512,
65
                Constants::SIG_RSA_RIPEMD160,
66
                Constants::SIG_HMAC_SHA1,
67
                Constants::SIG_HMAC_SHA224,
68
                Constants::SIG_HMAC_SHA256,
69
                Constants::SIG_HMAC_SHA384,
70
                Constants::SIG_HMAC_SHA512,
71
                Constants::SIG_HMAC_RIPEMD160,
72
            ],
73
            'Invalid canonicalization method',
74
            InvalidArgumentException::class
75
        );
76
77
        $this->Algorithm = $algorithm;
78
    }
79
80
81
    /**
82
     * Convert XML into a SignatureMethod
83
     *
84
     * @param \DOMElement $xml The XML element we should load
85
     * @return self
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   If the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): object
91
    {
92
        Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
94
95
        $Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
96
97
        return new self($Algorithm);
98
    }
99
100
101
    /**
102
     * Convert this SignatureMethod element to XML.
103
     *
104
     * @param \DOMElement|null $parent The element we should append this KeyName element to.
105
     * @return \DOMElement
106
     */
107
    public function toXML(DOMElement $parent = null): DOMElement
108
    {
109
        $e = $this->instantiateParentElement($parent);
110
        $e->setAttribute('Algorithm', $this->Algorithm);
111
112
        return $e;
113
    }
114
}
115