Passed
Push — master ( 2d3781...f48916 )
by Tim
11:07 queued 06:44
created

KeyLength   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 52
rs 10
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 7 1
A __construct() 0 4 1
A getKeyLength() 0 3 1
A toXML() 0 6 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\XML\xenc11;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
12
use function intval;
13
use function strval;
14
15
/**
16
 * Class representing a xenc11:KeyLength element.
17
 *
18
 * @package simplesamlphp/xml-security
19
 */
20
final class KeyLength extends AbstractXenc11Element
21
{
22
    /**
23
     * @param int $keyLength
24
     */
25
    public function __construct(
26
        protected int $keyLength,
27
    ) {
28
        Assert::positiveInteger($keyLength, SchemaViolationException::class);
29
    }
30
31
32
    /**
33
     * @return int
34
     */
35
    public function getKeyLength(): int
36
    {
37
        return $this->keyLength;
38
    }
39
40
41
    /**
42
     * Convert XML into a class instance
43
     *
44
     * @param \DOMElement $xml The XML element we should load
45
     * @return static
46
     *
47
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
48
     *   If the qualified name of the supplied element is wrong
49
     */
50
    public static function fromXML(DOMElement $xml): static
51
    {
52
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
53
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
54
        Assert::numeric($xml->textContent);
55
56
        return new static(intval($xml->textContent));
57
    }
58
59
60
    /**
61
     * Convert this element to XML.
62
     *
63
     * @param \DOMElement|null $parent The element we should append this element to.
64
     * @return \DOMElement
65
     */
66
    public function toXML(?DOMElement $parent = null): DOMElement
67
    {
68
        $e = $this->instantiateParentElement($parent);
69
        $e->textContent = strval($this->getKeyLength());
70
71
        return $e;
72
    }
73
}
74