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

SignatureMethod   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 25
c 1
b 0
f 0
dl 0
loc 98
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlgorithm() 0 3 1
A fromXML() 0 11 1
A toXML() 0 12 2
A getHMACOutputLength() 0 3 1
A __construct() 0 17 1
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\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XML\ExtendableElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
use SimpleSAML\XMLSecurity\Constants as C;
15
use SimpleSAML\XMLSecurity\Exception\InvalidArgumentException;
16
17
use function array_keys;
18
use function array_merge;
19
use function array_pop;
20
21
/**
22
 * Class representing a ds:SignatureMethod element.
23
 *
24
 * @package simplesamlphp/xml-security
25
 */
26
final class SignatureMethod extends AbstractDsElement
27
{
28
    use ExtendableElementTrait;
29
30
    /** The namespace-attribute for the xs:any element */
31
    public const XS_ANY_ELT_NAMESPACE = NS::OTHER;
32
33
34
    /**
35
     * Initialize a SignatureMethod element.
36
     *
37
     * @param string $Algorithm
38
     * @param \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null $hmacOutputLength
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\ds\HMACOutputLength was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
39
     * @param array<\SimpleSAML\XML\SerializableElementInterface> $children
40
     */
41
    public function __construct(
42
        protected string $Algorithm,
43
        protected ?HMACOutputLength $hmacOutputLength = null,
44
        array $children = [],
45
    ) {
46
        Assert::validURI($Algorithm, SchemaViolationException::class);
47
        Assert::oneOf(
48
            $Algorithm,
49
            array_merge(
50
                array_keys(C::$RSA_DIGESTS),
51
                array_keys(C::$HMAC_DIGESTS),
52
            ),
53
            'Invalid signature method: %s',
54
            InvalidArgumentException::class,
55
        );
56
57
        $this->setElements($children);
58
    }
59
60
61
    /**
62
     * Collect the value of the Algorithm-property
63
     *
64
     * @return string
65
     */
66
    public function getAlgorithm(): string
67
    {
68
        return $this->Algorithm;
69
    }
70
71
72
    /**
73
     * Collect the value of the hmacOutputLength-property
74
     *
75
     * @return \SimpleSAML\XMLSecurity\ds\HMACOutputLength|null
76
     */
77
    public function getHMACOutputLength(): ?HMACOutputLength
78
    {
79
        return $this->hmacOutputLength;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->hmacOutputLength also could return the type SimpleSAML\XMLSecurity\XML\ds\HMACOutputLength which is incompatible with the documented return type SimpleSAML\XMLSecurity\ds\HMACOutputLength|null.
Loading history...
80
    }
81
82
83
    /**
84
     * Convert XML into a SignatureMethod
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   If the qualified name of the supplied element is wrong
91
     */
92
    public static function fromXML(DOMElement $xml): static
93
    {
94
        Assert::same($xml->localName, 'SignatureMethod', InvalidDOMElementException::class);
95
        Assert::same($xml->namespaceURI, SignatureMethod::NS, InvalidDOMElementException::class);
96
97
        $Algorithm = SignatureMethod::getAttribute($xml, 'Algorithm');
98
99
        $hmacOutputLength = HMACOutputLength::getChildrenOfClass($xml);
100
        Assert::maxCount($hmacOutputLength, 1, TooManyElementsException::class);
101
102
        return new static($Algorithm, array_pop($hmacOutputLength), self::getChildElementsFromXML($xml));
103
    }
104
105
106
    /**
107
     * Convert this SignatureMethod element to XML.
108
     *
109
     * @param \DOMElement|null $parent The element we should append this SignatureMethod element to.
110
     * @return \DOMElement
111
     */
112
    public function toXML(DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
        $e->setAttribute('Algorithm', $this->getAlgorithm());
116
117
        $this->getHMACOutputLength()?->toXML($e);
118
119
        foreach ($this->getElements() as $elt) {
120
            $elt->toXML($e);
121
        }
122
123
        return $e;
124
    }
125
}
126