Passed
Pull Request — master (#13)
by Tim
01:56
created

DigestMethod::toXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 10
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\Chunk;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XMLSecurity\Constants;
12
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
13
14
/**
15
 * Class representing a ds:DigestMethod element.
16
 *
17
 * @package simplesamlphp/xml-security
18
 */
19
final class DigestMethod extends AbstractDsElement
20
{
21
    /**
22
     * The algorithm.
23
     *
24
     * @var string
25
     */
26
    protected string $Algorithm;
27
28
    /**
29
     * @var \SimpleSAML\XML\Chunk[]
30
     */
31
    protected array $elements;
32
33
34
    /**
35
     * Initialize a DigestMethod element.
36
     *
37
     * @param string $algorithm
38
     * @param \SimpleSAML\XML\Chunk[] $elements
39
     */
40
    public function __construct(string $algorithm, array $elements = [])
41
    {
42
        $this->setAlgorithm($algorithm);
43
        $this->setElements($elements);
44
    }
45
46
47
    /**
48
     * Collect the value of the Algorithm-property
49
     *
50
     * @return string
51
     */
52
    public function getAlgorithm(): string
53
    {
54
        return $this->Algorithm;
55
    }
56
57
58
    /**
59
     * Set the value of the Algorithm-property
60
     *
61
     * @param string $algorithm
62
     */
63
    private function setAlgorithm(string $algorithm): void
64
    {
65
        Assert::oneOf(
66
            $algorithm,
67
            [
68
                Constants::DIGEST_SHA1,
69
                Constants::DIGEST_SHA224,
70
                Constants::DIGEST_SHA256,
71
                Constants::DIGEST_SHA384,
72
                Constants::DIGEST_SHA512,
73
                Constants::DIGEST_RIPEMD160,
74
            ],
75
            'Invalid digest method',
76
            InvalidArgumentException::class
77
        );
78
79
        $this->Algorithm = $algorithm;
80
    }
81
82
83
    /**
84
     * Collect the embedded elements
85
     *
86
     * @return \SimpleSAML\XML\Chunk[]
87
     */
88
    public function getElements(): array
89
    {
90
        return $this->elements;
91
    }
92
93
94
    /**
95
     * Set the value of the elements-property
96
     *
97
     * @param \SimpleSAML\XML\Chunk[] $elements
98
     * @throws \SimpleSAML\Assert\AssertionFailedException if the supplied array contains anything other than Chunk objects
99
     */
100
    private function setElements(array $elements): void
101
    {
102
        Assert::allIsInstanceOf($elements, Chunk::class);
103
104
        $this->elements = $elements;
105
    }
106
107
108
    /**
109
     * Convert XML into a DigestMethod
110
     *
111
     * @param \DOMElement $xml The XML element we should load
112
     * @return self
113
     *
114
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
115
     *   If the qualified name of the supplied element is wrong
116
     */
117
    public static function fromXML(DOMElement $xml): object
118
    {
119
        Assert::same($xml->localName, 'DigestMethod', InvalidDOMElementException::class);
120
        Assert::same($xml->namespaceURI, DigestMethod::NS, InvalidDOMElementException::class);
121
122
        $Algorithm = DigestMethod::getAttribute($xml, 'Algorithm');
123
124
        $elements = [];
125
        foreach ($xml->childNodes as $elt) {
126
            if (!($elt instanceof DOMElement)) {
127
                continue;
128
            }
129
130
            $elements[] = new Chunk($elt);
131
        }
132
133
        return new self($Algorithm, $elements);
134
    }
135
136
137
    /**
138
     * Convert this DigestMethod element to XML.
139
     *
140
     * @param \DOMElement|null $parent The element we should append this DigestMethod element to.
141
     * @return \DOMElement
142
     */
143
    public function toXML(DOMElement $parent = null): DOMElement
144
    {
145
        $e = $this->instantiateParentElement($parent);
146
        $e->setAttribute('Algorithm', $this->Algorithm);
147
148
        foreach ($this->elements as $elt) {
149
            $e->appendChild($e->ownerDocument->importNode($elt->getXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($elt->getXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
150
        }
151
152
        return $e;
153
    }
154
}
155