Passed
Pull Request — master (#16)
by Tim
02:09
created

SignedInfo::setCanonicalizationMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
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\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Chunk;
11
12
/**
13
 * Class representing a ds:SignedInfo element.
14
 *
15
 * @package simplesamlphp/xml-security
16
 */
17
final class SignedInfo extends AbstractDsElement
18
{
19
    /** @var string */
20
    protected string $Id;
21
22
    /**
23
     * @var \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
24
     */
25
    protected CanonicalizationMethod $canonicalizationMethod;
26
27
    /**
28
     * @var \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
29
     */
30
    protected SignatureMethod $signatureMethod;
31
32
    /**
33
     * @var \SimpleSAML\XMLSecurity\XML\ds\Reference[]
34
     */
35
    protected array $references;
36
37
38
    /**
39
     * Initialize a SignedIfno.
40
     *
41
     * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod
42
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod
43
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
44
     * @param string|null $Id
45
     */
46
    public function __construct(
47
        CanonicalizationMethod $canonicalizationMethod,
48
        SignatureMethod $signatureMethod,
49
        array $references,
50
        ?string $Id
51
    ) {
52
        $this->setCanonicalizationMethod($canonicalizationMethod);
53
        $this->setSignatureMethod($signatureMethod);
54
        $this->setReferences($references);
55
        $this->setId($Id);
0 ignored issues
show
Bug introduced by
It seems like $Id can also be of type null; however, parameter $Id of SimpleSAML\XMLSecurity\XML\ds\SignedInfo::setId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

55
        $this->setId(/** @scrutinizer ignore-type */ $Id);
Loading history...
56
    }
57
58
59
    /**
60
     * Collect the value of the canonicalizationMethod-property
61
     *
62
     * @return \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod
63
     */
64
    public function getCanonicalizationMethod(): CanonicalizationMethod
65
    {
66
        return $this->canonicalizationMethod;
67
    }
68
69
70
    /**
71
     * Set the value of the canonicalizationMethod-property
72
     *
73
     * @param \SimpleSAML\XMLSecurity\XML\ds\CanonicalizationMethod $canonicalizationMethod
74
     */
75
    private function setCanonicalizationMethod(CanonicalizationMethod $canonicalizationMethod): void
76
    {
77
        $this->canonicalizationMethod = $canonicalizationMethod;
78
    }
79
80
81
    /**
82
     * Collect the value of the signatureMethod-property
83
     *
84
     * @return \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod
85
     */
86
    public function getSignatureMethod(): SignatureMethod
87
    {
88
        return $this->signatureMethod;
89
    }
90
91
92
    /**
93
     * Set the value of the signatureMethod-property
94
     *
95
     * @param \SimpleSAML\XMLSecurity\XML\ds\SignatureMethod $signatureMethod
96
     */
97
    private function setSignatureMethod(SignatureMethod $signatureMethod): void
98
    {
99
        $this->signatureMethod = $signatureMethod;
100
    }
101
102
103
    /**
104
     * Collect the value of the references-property
105
     *
106
     * @return \SimpleSAML\XMLSecurity\XML\ds\Reference[]
107
     */
108
    public function getReferences(): array
109
    {
110
        return $this->references;
111
    }
112
113
114
    /**
115
     * Set the value of the references-property
116
     *
117
     * @param \SimpleSAML\XMLSecurity\XML\ds\Reference[] $references
118
     */
119
    private function setReferences(array $references): void
120
    {
121
        Assert::allIsInstanceOf($references, Reference::class);
122
123
        $this->references = $references;
124
    }
125
126
127
    /**
128
     * Collect the value of the Id-property
129
     *
130
     * @return string|null
131
     */
132
    public function getId(): ?string
133
    {
134
        return $this->Id;
135
    }
136
137
138
    /**
139
     * Set the value of the Id-property
140
     *
141
     * @param string|null $Id
142
     */
143
    private function setId(string $Id): void
144
    {
145
        $this->Id = $Id;
146
    }
147
148
149
    /**
150
     * Convert XML into a SignedInfo instance
151
     *
152
     * @param \DOMElement $xml The XML element we should load
153
     * @return self
154
     *
155
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
156
     *   If the qualified name of the supplied element is wrong
157
     */
158
    public static function fromXML(DOMElement $xml): object
159
    {
160
        Assert::same($xml->localName, 'SignedInfo', InvalidDOMElementException::class);
161
        Assert::same($xml->namespaceURI, SignedInfo::NS, InvalidDOMElementException::class);
162
163
        $Id = self::getAttribute($xml, 'Id', null);
164
165
        $canonicalizationMethod = CanonicalizationMethod::getChildrenOfClass($xml);
166
        Assert::count($canonicalizationMethod, 1, 'A ds:SignedInfo element must contain exactly one ds:CanonicalizationMethod');
167
168
        $signatureMethod = SignatureMethod::getChildrenOfClass($xml);
169
        Assert::count($signatureMethod, 1, 'A ds:SignedInfo element must contain exactly one ds:SignatureMethod');
170
171
        $references = Reference::getChildrenOfClass($xml);
172
        Assert::minCount($references, 1, 'A ds:SignedInfo element must contain at least one ds:Reference');
173
174
        return new self(
175
            array_pop($canonicalizationMethod),
176
            array_pop($signatureMethod),
177
            $references,
178
            $Id
179
        );
180
    }
181
182
183
    /**
184
     * Convert this SignedInfo element to XML.
185
     *
186
     * @param \DOMElement|null $parent The element we should append this SignedInfo element to.
187
     * @return \DOMElement
188
     */
189
    public function toXML(DOMElement $parent = null): DOMElement
190
    {
191
        $e = $this->instantiateParentElement($parent);
192
193
        if ($this->Id !== null) {
194
            $e->setAttribute('Id', $this->Id);
195
        }
196
197
        $this->canonicalizationMethod->toXML($e);
198
        $this->signatureMethod->toXML($e);
199
200
        foreach ($this->references as $ref) {
201
            $ref->toXML($e);
202
        }
203
204
        return $e;
205
    }
206
}
207