Passed
Pull Request — master (#36)
by Tim
01:53
created

RetrievalMethod::getTransforms()   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 0
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\Exception\SchemaViolationException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
13
/**
14
 * Class representing a ds:RetrievalMethod element.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
final class RetrievalMethod extends AbstractDsElement
19
{
20
    protected ?Transforms $transforms;
21
22
    /** @var string $URI */
23
    protected string $URI;
24
25
    /** @var string|null $Type */
26
    protected ?string $Type;
27
28
29
    /**
30
     * Initialize a ds:RetrievalMethod
31
     *
32
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
33
     * @param string $URI
34
     * @param string|null $Type
35
     */
36
    public function __construct(
37
        ?Transforms $transforms,
38
        string $URI,
39
        ?string $Type = null
40
    ) {
41
        $this->setTransforms($transforms);
42
        $this->setURI($URI);
43
        $this->setType($Type);
44
    }
45
46
47
    /**
48
     * @return \SimpleSAML\XMLSecurity\XML\ds\Transforms|null
49
     */
50
    public function getTransforms(): ?Transforms
51
    {
52
        return $this->transforms;
53
    }
54
55
56
    /**
57
     * @param \SimpleSAML\XMLSecurity\XML\ds\Transforms|null $transforms
58
     */
59
    protected function setTransforms(?Transforms $transforms): void
60
    {
61
        $this->transforms = $transforms;
62
    }
63
64
65
    /**
66
     * @return string
67
     */
68
    public function getURI(): string
69
    {
70
        return $this->URI;
71
    }
72
73
74
    /**
75
     * @param string $URI
76
     */
77
    private function setURI(string $URI): void
78
    {
79
        Assert::validURI($URI, SchemaViolationException::class); // Covers the empty string
80
        $this->URI = $URI;
81
    }
82
83
84
    /**
85
     * @return string|null
86
     */
87
    public function getType(): ?string
88
    {
89
        return $this->Type;
90
    }
91
92
93
    /**
94
     * @param string|null $Type
95
     */
96
    private function setType(?string $Type): void
97
    {
98
        Assert::validURI($Type, SchemaViolationException::class); // Covers the empty string
0 ignored issues
show
Bug introduced by
It seems like $Type can also be of type null; however, parameter $value of SimpleSAML\Assert\Assert::validURI() 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

98
        Assert::validURI(/** @scrutinizer ignore-type */ $Type, SchemaViolationException::class); // Covers the empty string
Loading history...
99
        $this->Type = $Type;
100
    }
101
102
103
    /**
104
     * Convert XML into a RetrievalMethod element
105
     *
106
     * @param \DOMElement $xml The XML element we should load
107
     * @return self
108
     *
109
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
110
     *   If the qualified name of the supplied element is wrong
111
     */
112
    public static function fromXML(DOMElement $xml): self
113
    {
114
        Assert::same($xml->localName, 'RetrievalMethod', InvalidDOMElementException::class);
115
        Assert::same($xml->namespaceURI, RetrievalMethod::NS, InvalidDOMElementException::class);
116
117
        /** @psalm-var string $URI */
118
        $URI = self::getAttribute($xml, 'URI');
119
        $Type = self::getAttribute($xml, 'Type', null);
120
121
        /** @psalm-var \SimpleSAML\XMLSecurity\XML\ds\Transforms[] $transforms */
122
        $transforms = Transforms::getChildrenOfClass($xml);
123
        Assert::maxCount(
124
            $transforms,
125
            1,
126
            'A <ds:RetrievalMethod> may contain a maximum of one <ds:Transforms>.',
127
            TooManyElementsException::class,
128
        );
129
130
        return new self(
131
            array_pop($transforms),
132
            $URI,
133
            $Type
134
        );
135
    }
136
137
138
    /**
139
     * Convert this RetrievalMethod element to XML.
140
     *
141
     * @param \DOMElement|null $parent The element we should append this RetrievalMethod element to.
142
     * @return \DOMElement
143
     */
144
    public function toXML(DOMElement $parent = null): DOMElement
145
    {
146
        $e = $this->instantiateParentElement($parent);
147
        $e->setAttribute('URI', $this->URI);
148
149
        if ($this->Type !== null) {
150
            $e->setAttribute('Type', $this->Type);
151
        }
152
153
        if ($this->transforms !== null) {
154
            $this->transforms->toXML($e);
155
        }
156
157
        return $e;
158
    }
159
}
160