Passed
Pull Request — master (#33)
by Tim
02:36
created

SignatureProperty   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 133
rs 10
wmc 11

7 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 30 3
A setId() 0 3 1
A setTarget() 0 4 1
A getId() 0 3 1
A __construct() 0 8 1
A toXML() 0 14 3
A getTarget() 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\Chunk;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\MissingElementException;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XML\ExtendableElementTrait;
15
use SimpleSAML\XML\XMLElementInterface;
16
17
/**
18
 * Class representing a ds:SignatureProperty element.
19
 *
20
 * @package simplesamlphp/xml-security
21
 */
22
final class SignatureProperty extends AbstractDsElement
23
{
24
    use ExtendableElementTrait;
25
26
27
    /** The namespace-attribute for the xs:any element */
28
    public const NAMESPACE = C::XS_ANY_NS_OTHER;
29
30
    /** @var string $Target */
31
    protected string $Target;
32
33
    /** @var string|null $Id */
34
    protected ?string $Id;
35
36
37
    /**
38
     * Initialize a ds:SignatureProperty
39
     *
40
     * @param \SimpleSAML\XML\XMLElementInterface[] $elements
41
     * @param string $Target
42
     * @param string|null $Id
43
     */
44
    public function __construct(
45
        array $elements,
46
        string $Target,
47
        ?string $Id = null
48
    ) {
49
        $this->setElements($elements);
50
        $this->setTarget($Target);
51
        $this->setId($Id);
52
    }
53
54
55
    /**
56
     * @return string
57
     */
58
    public function getTarget(): string
59
    {
60
        return $this->Target;
61
    }
62
63
64
    /**
65
     * @param string $Target
66
     */
67
    protected function setTarget(string $Target): void
68
    {
69
        Assert::validURI($Target, SchemaViolationException::class); // Covers the empty string
70
        $this->Target = $Target;
71
    }
72
73
74
    /**
75
     * @return string|null
76
     */
77
    public function getId(): ?string
78
    {
79
        return $this->Id;
80
    }
81
82
83
    /**
84
     * @param string|null $Id
85
     */
86
    private function setId(?string $Id): void
87
    {
88
        $this->Id = $Id;
89
    }
90
91
92
    /**
93
     * Convert XML into a SignatureProperty element
94
     *
95
     * @param \DOMElement $xml The XML element we should load
96
     * @return self
97
     *
98
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
99
     *   If the qualified name of the supplied element is wrong
100
     */
101
    public static function fromXML(DOMElement $xml): self
102
    {
103
        Assert::same($xml->localName, 'SignatureProperty', InvalidDOMElementException::class);
104
        Assert::same($xml->namespaceURI, SignatureProperty::NS, InvalidDOMElementException::class);
105
106
        /** @psalm-var string $Target */
107
        $Target = self::getAttribute($xml, 'Target');
108
        $Id = self::getAttribute($xml, 'Id', null);
109
110
        $children = [];
111
        foreach ($xml->childNodes as $child) {
112
            if (!($child instanceof DOMElement)) {
113
                continue;
114
            }
115
116
            $children[] = new Chunk($child);
117
        }
118
119
        /** @psalm-var \SimpleSAML\XML\XMLElementInterface[] $children */
120
        Assert::minCount(
121
            $children,
122
            1,
123
            'A <ds:SignatureProperty> must contain at least one element.',
124
            MissingElementException::class,
125
        );
126
127
        return new self(
128
            $children,
129
            $Target,
130
            $Id,
131
        );
132
    }
133
134
135
    /**
136
     * Convert this SignatureProperty element to XML.
137
     *
138
     * @param \DOMElement|null $parent The element we should append this SignatureProperty element to.
139
     * @return \DOMElement
140
     */
141
    public function toXML(DOMElement $parent = null): DOMElement
142
    {
143
        $e = $this->instantiateParentElement($parent);
144
        $e->setAttribute('Target', $this->Target);
145
146
        if ($this->Id !== null) {
147
            $e->setAttribute('Id', $this->Id);
148
        }
149
150
        foreach ($this->elements as $element) {
151
            $e->appendChild($e->ownerDocument->importNode($element->getXML(), true));
0 ignored issues
show
Bug introduced by
The method importNode() does not exist on null. ( Ignorable by Annotation )

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

151
            $e->appendChild($e->ownerDocument->/** @scrutinizer ignore-call */ importNode($element->getXML(), true));

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
152
        }
153
154
        return $e;
155
    }
156
}
157