Transform   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 42
c 2
b 0
f 0
dl 0
loc 125
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getAlgorithm() 0 3 1
A __construct() 0 24 3
A getInclusiveNamespaces() 0 3 1
A getXPath() 0 3 1
A toXML() 0 16 4
A fromXML() 0 20 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\SchemaValidatableElementInterface;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
13
use SimpleSAML\XMLSchema\Type\AnyURIValue;
14
use SimpleSAML\XMLSecurity\Constants as C;
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\Constants 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...
15
use SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces;
16
use SimpleSAML\XPath\Constants as XPATH_C;
17
18
use function array_pop;
19
use function strval;
20
21
/**
22
 * Class representing transforms.
23
 *
24
 * @package simplesamlphp/xml-security
25
 */
26
class Transform extends AbstractDsElement implements SchemaValidatableElementInterface
0 ignored issues
show
Bug introduced by
The type SimpleSAML\XMLSecurity\XML\ds\AbstractDsElement 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...
27
{
28
    use SchemaValidatableElementTrait;
29
30
31
    /**
32
     * Initialize the Transform element.
33
     *
34
     * @param \SimpleSAML\XMLSchema\Type\AnyURIValue $algorithm
35
     * @param \SimpleSAML\XMLSecurity\XML\ds\XPath|null $xpath
36
     * @param \SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces|null $inclusiveNamespaces
37
     */
38
    final public function __construct(
39
        protected AnyURIValue $algorithm,
40
        protected ?XPath $xpath = null,
41
        protected ?InclusiveNamespaces $inclusiveNamespaces = null,
42
    ) {
43
        if ($xpath !== null) {
44
            Assert::nullOrEq(
45
                $algorithm->getValue(),
46
                XPATH_C::XPATH10_URI,
47
                sprintf('Transform algorithm "%s" required if XPath provided.', XPATH_C::XPATH10_URI),
48
            );
49
        }
50
51
        if ($inclusiveNamespaces !== null) {
52
            Assert::oneOf(
53
                $algorithm->getValue(),
54
                [
55
                    C::C14N_INCLUSIVE_WITH_COMMENTS,
56
                    C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
57
                ],
58
                sprintf(
59
                    'Transform algorithm "%s" or "%s" required if InclusiveNamespaces provided.',
60
                    C::C14N_EXCLUSIVE_WITH_COMMENTS,
61
                    C::C14N_EXCLUSIVE_WITHOUT_COMMENTS,
62
                ),
63
            );
64
        }
65
    }
66
67
68
    /**
69
     * Get the algorithm associated with this transform.
70
     *
71
     * @return \SimpleSAML\XMLSchema\Type\AnyURIValue
72
     */
73
    public function getAlgorithm(): AnyURIValue
74
    {
75
        return $this->algorithm;
76
    }
77
78
79
    /**
80
     * Get the XPath associated with this transform.
81
     *
82
     * @return \SimpleSAML\XMLSecurity\XML\ds\XPath|null
83
     */
84
    public function getXPath(): ?XPath
85
    {
86
        return $this->xpath;
87
    }
88
89
90
    /**
91
     * Get the InclusiveNamespaces associated with this transform.
92
     *
93
     * @return \SimpleSAML\XMLSecurity\XML\ec\InclusiveNamespaces|null
94
     */
95
    public function getInclusiveNamespaces(): ?InclusiveNamespaces
96
    {
97
        return $this->inclusiveNamespaces;
98
    }
99
100
101
    /**
102
     * Convert XML into a Transform element.
103
     *
104
     * @param \DOMElement $xml The XML element we should load.
105
     */
106
    public static function fromXML(DOMElement $xml): static
107
    {
108
        Assert::same($xml->localName, 'Transform', InvalidDOMElementException::class);
109
        Assert::same($xml->namespaceURI, Transform::NS, InvalidDOMElementException::class);
110
111
        $xpath = XPath::getChildrenOfClass($xml);
112
        Assert::maxCount($xpath, 1, 'Only one XPath element supported per Transform.', TooManyElementsException::class);
113
114
        $prefixes = InclusiveNamespaces::getChildrenOfClass($xml);
115
        Assert::maxCount(
116
            $prefixes,
117
            1,
118
            'Only one InclusiveNamespaces element supported per Transform.',
119
            TooManyElementsException::class,
120
        );
121
122
        return new static(
123
            self::getAttribute($xml, 'Algorithm', AnyURIValue::class),
124
            array_pop($xpath),
125
            array_pop($prefixes),
126
        );
127
    }
128
129
130
    /**
131
     * Convert this Transform element to XML.
132
     *
133
     * @param \DOMElement|null $parent The element we should append this Transform element to.
134
     */
135
    public function toXML(?DOMElement $parent = null): DOMElement
136
    {
137
        $e = $this->instantiateParentElement($parent);
138
        $e->setAttribute('Algorithm', strval($this->getAlgorithm()));
139
140
        switch ($this->getAlgorithm()) {
141
            case XPATH_C::XPATH10_URI:
142
                $this->getXPath()?->toXML($e);
143
                break;
144
            case C::C14N_EXCLUSIVE_WITH_COMMENTS:
145
            case C::C14N_EXCLUSIVE_WITHOUT_COMMENTS:
146
                $this->getInclusiveNamespaces()?->toXML($e);
147
                break;
148
        }
149
150
        return $e;
151
    }
152
}
153