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

X509Digest::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
nc 1
nop 1
dl 0
loc 8
rs 10
c 1
b 0
f 0
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
use SimpleSAML\XML\XMLStringElementTrait;
13
14
/**
15
 * Class representing a ds:X509Digest element.
16
 *
17
 * @package simplesaml/xml-security
18
 */
19
final class X509Digest extends AbstractDsElement
20
{
21
    use XMLStringElementTrait {
22
        __construct as __parentConstruct;
23
        toXML as parentToXML;
24
    }
25
26
27
    /**
28
     * The digest algorithm.
29
     *
30
     * @var string
31
     */
32
    protected string $algorithm;
33
34
35
    /**
36
     * Initialize a X509Digest element.
37
     *
38
     * @param string $digest
39
     * @param string $algorithm
40
     */
41
    public function __construct(string $digest, string $algorithm)
42
    {
43
        $this->__parentConstruct($digest);
44
45
        $this->setAlgorithm($algorithm);
46
    }
47
48
49
    /**
50
     * Collect the value of the algorithm-property
51
     *
52
     * @return string
53
     */
54
    public function getAlgorithm(): string
55
    {
56
        return $this->algorithm;
57
    }
58
59
60
    /**
61
     * Set the value of the algorithm-property
62
     *
63
     * @param string $algorithm
64
     */
65
    private function setAlgorithm(string $algorithm): void
66
    {
67
        Assert::oneOf(
68
            $algorithm,
69
            [
70
                Constants::DIGEST_SHA1,
71
                Constants::DIGEST_SHA224,
72
                Constants::DIGEST_SHA256,
73
                Constants::DIGEST_SHA384,
74
                Constants::DIGEST_SHA512,
75
                Constants::DIGEST_RIPEMD160,
76
            ],
77
            'Invalid digest method',
78
            InvalidArgumentException::class
79
        );
80
81
        $this->algorithm = $algorithm;
82
    }
83
84
85
    /**
86
     * Validate the content of the element.
87
     *
88
     * @param string $content  The value to go in the XML textContent
89
     * @throws \Exception on failure
90
     * @return void
91
     */
92
    private function validateContent(string $content): void
0 ignored issues
show
Unused Code introduced by
The method validateContent() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
93
    {
94
        Assert::notEmpty($content, 'X509Digest cannot be empty');
95
        Assert::stringPlausibleBase64($content, 'ds:X509Digest is not a valid Base64 encoded string');
96
    }
97
98
99
    /**
100
     * Convert XML into a X509Digest
101
     *
102
     * @param \DOMElement $xml The XML element we should load
103
     * @return self
104
     *
105
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
106
     *   If the qualified name of the supplied element is wrong
107
     */
108
    public static function fromXML(DOMElement $xml): object
109
    {
110
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
111
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
112
113
        $algorithm = self::getAttribute($xml, 'Algorithm');
114
115
        return new self($xml->textContent, $algorithm);
116
    }
117
118
119
    /**
120
     * Convert this X509Digest element to XML.
121
     *
122
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
123
     * @return \DOMElement
124
     */
125
    public function toXML(DOMElement $parent = null): DOMElement
126
    {
127
        $e = $this->parentToXML($parent);
128
        $e->setAttribute('Algorithm', $this->algorithm);
129
130
        return $e;
131
    }
132
}
133