X509Digest   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
c 0
b 0
f 0
dl 0
loc 72
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 8 1
A toXML() 0 7 1
A __construct() 0 13 1
A getAlgorithm() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\dsig11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Base64ElementTrait;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\SchemaViolationException;
12
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
17
/**
18
 * Class representing a dsig11:X509Digest element.
19
 *
20
 * @package simplesaml/xml-security
21
 */
22
final class X509Digest extends AbstractDsig11Element implements SchemaValidatableElementInterface
23
{
24
    use Base64ElementTrait;
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\XMLSecurity\XML\dsig11\X509Digest: $message, $line
Loading history...
26
27
28
    /**
29
     * Initialize a X509Digest element.
30
     *
31
     * @param string $digest
32
     * @param string $algorithm
33
     */
34
    public function __construct(
35
        string $digest,
36
        protected string $algorithm,
37
    ) {
38
        Assert::validURI($algorithm, SchemaViolationException::class);
39
        Assert::oneOf(
40
            $algorithm,
41
            array_keys(C::$DIGEST_ALGORITHMS),
42
            'Invalid digest method: %s',
43
            InvalidArgumentException::class,
44
        );
45
46
        $this->setContent($digest);
47
    }
48
49
50
    /**
51
     * Collect the value of the algorithm-property
52
     *
53
     * @return string
54
     */
55
    public function getAlgorithm(): string
56
    {
57
        return $this->algorithm;
58
    }
59
60
61
    /**
62
     * Convert XML into a X509Digest
63
     *
64
     * @param \DOMElement $xml The XML element we should load
65
     * @return static
66
     *
67
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
68
     *   If the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        Assert::same($xml->localName, 'X509Digest', InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, X509Digest::NS, InvalidDOMElementException::class);
74
75
        $algorithm = self::getAttribute($xml, 'Algorithm');
76
77
        return new static($xml->textContent, $algorithm);
78
    }
79
80
81
    /**
82
     * Convert this X509Digest element to XML.
83
     *
84
     * @param \DOMElement|null $parent The element we should append this X509Digest element to.
85
     * @return \DOMElement
86
     */
87
    public function toXML(?DOMElement $parent = null): DOMElement
88
    {
89
        $e = $this->instantiateParentElement($parent);
90
        $e->textContent = $this->getContent();
91
        $e->setAttribute('Algorithm', $this->getAlgorithm());
92
93
        return $e;
94
    }
95
}
96