Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

Signature   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 49
c 3
b 0
f 0
dl 0
loc 159
rs 10
wmc 11

8 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 17 3
A fromXML() 0 51 2
A getKeyInfo() 0 3 1
A __construct() 0 9 1
A getObjects() 0 3 1
A getSignedInfo() 0 3 1
A getId() 0 3 1
A getSignatureValue() 0 3 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\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
use SimpleSAML\XMLSecurity\Constants as C;
13
14
use function array_pop;
15
16
/**
17
 * Class representing a ds:Signature element.
18
 *
19
 * @package simplesamlphp/xml-security
20
 */
21
final class Signature extends AbstractDsElement
22
{
23
    /**
24
     * Signature constructor.
25
     *
26
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignedInfo $signedInfo
27
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureValue $signatureValue
28
     * @param \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null $keyInfo
29
     * @param \SimpleSAML\XMLSecurity\XML\ds\DsObject[] $objects
30
     * @param string|null $Id
31
     */
32
    public function __construct(
33
        protected SignedInfo $signedInfo,
34
        protected SignatureValue $signatureValue,
35
        protected ?KeyInfo $keyInfo,
36
        protected array $objects = [],
37
        protected ?string $Id = null,
38
    ) {
39
        Assert::allIsInstanceOf($objects, DsObject::class);
40
        Assert::nullOrValidNCName($Id);
41
    }
42
43
44
    /**
45
     * Get the Id used for this signature.
46
     *
47
     * @return string|null
48
     */
49
    public function getId(): ?string
50
    {
51
        return $this->Id;
52
    }
53
54
55
    /**
56
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignedInfo
57
     */
58
    public function getSignedInfo(): SignedInfo
59
    {
60
        return $this->signedInfo;
61
    }
62
63
64
    /**
65
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureValue
66
     */
67
    public function getSignatureValue(): SignatureValue
68
    {
69
        return $this->signatureValue;
70
    }
71
72
73
    /**
74
     * @return \SimpleSAML\XMLSecurity\XML\ds\KeyInfo|null
75
     */
76
    public function getKeyInfo(): ?KeyInfo
77
    {
78
        return $this->keyInfo;
79
    }
80
81
82
    /**
83
     * Get the array of ds:Object elements attached to this signature.
84
     *
85
     * @return \SimpleSAML\XMLSecurity\XML\ds\DsObject[]
86
     */
87
    public function getObjects(): array
88
    {
89
        return $this->objects;
90
    }
91
92
93
    /**
94
     * Convert XML into a Signature element
95
     *
96
     * @param \DOMElement $xml
97
     * @return static
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
100
     *   If the qualified name of the supplied element is wrong
101
     */
102
    public static function fromXML(DOMElement $xml): static
103
    {
104
        Assert::same($xml->localName, 'Signature', InvalidDOMElementException::class);
105
        Assert::same($xml->namespaceURI, Signature::NS, InvalidDOMElementException::class);
106
107
        $Id = self::getAttribute($xml, 'Id', null);
108
109
        $signedInfo = SignedInfo::getChildrenOfClass($xml);
110
        Assert::minCount(
111
            $signedInfo,
112
            1,
113
            'ds:Signature needs exactly one ds:SignedInfo element.',
114
            MissingElementException::class,
115
        );
116
        Assert::maxCount(
117
            $signedInfo,
118
            1,
119
            'ds:Signature needs exactly one ds:SignedInfo element.',
120
            TooManyElementsException::class,
121
        );
122
123
        $signatureValue = SignatureValue::getChildrenOfClass($xml);
124
        Assert::minCount(
125
            $signatureValue,
126
            1,
127
            'ds:Signature needs exactly one ds:SignatureValue element.',
128
            MissingElementException::class,
129
        );
130
        Assert::maxCount(
131
            $signatureValue,
132
            1,
133
            'ds:Signature needs exactly one ds:SignatureValue element.',
134
            TooManyElementsException::class,
135
        );
136
137
        $keyInfo = KeyInfo::getChildrenOfClass($xml);
138
        Assert::maxCount(
139
            $keyInfo,
140
            1,
141
            'ds:Signature can hold a maximum of one ds:KeyInfo element.',
142
            TooManyElementsException::class,
143
        );
144
145
        $objects = DsObject::getChildrenOfClass($xml);
146
147
        return new static(
148
            array_pop($signedInfo),
149
            array_pop($signatureValue),
150
            empty($keyInfo) ? null : array_pop($keyInfo),
151
            $objects,
152
            $Id,
153
        );
154
    }
155
156
157
    /**
158
     * Convert this Signature element to XML.
159
     *
160
     * @param \DOMElement|null $parent The element we should append this Signature element to.
161
     * @return \DOMElement
162
     */
163
    public function toXML(DOMElement $parent = null): DOMElement
164
    {
165
        $e = $this->instantiateParentElement($parent);
166
167
        if ($this->getId() !== null) {
168
            $e->setAttribute('Id', $this->getId());
169
        }
170
171
        $this->getSignedInfo()->toXML($e);
172
        $this->getSignatureValue()->toXML($e);
173
        $this->getKeyInfo()?->toXML($e);
174
175
        foreach ($this->getObjects() as $o) {
176
            $o->toXML($e);
177
        }
178
179
        return $e;
180
    }
181
}
182