Passed
Pull Request — master (#351)
by Tim
02:15
created

NameIDType   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 29
c 4
b 0
f 0
dl 0
loc 117
rs 10
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validateContent() 0 3 1
A __construct() 0 13 1
A getFormat() 0 3 1
A getSPProvidedID() 0 3 1
A toXML() 0 22 5
A fromXML() 0 11 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\StringElementTrait;
10
11
/**
12
 * SAML NameIDType abstract data type.
13
 *
14
 * @package simplesamlphp/saml2
15
 */
16
17
abstract class NameIDType extends AbstractSamlElement implements IdentifierInterface
18
{
19
    use IDNameQualifiersTrait;
20
    use StringElementTrait;
21
22
23
    /**
24
     * Initialize a saml:NameIDType from scratch
25
     *
26
     * @param string $value
27
     * @param string|null $Format
28
     * @param string|null $SPProvidedID
29
     * @param string|null $NameQualifier
30
     * @param string|null $SPNameQualifier
31
     */
32
    protected function __construct(
33
        string $value,
34
        protected ?string $nameQualifier = null,
35
        protected ?string $spNameQualifier = null,
36
        protected ?string $format = null,
37
        protected ?string $spProvidedID = null,
38
    ) {
39
        Assert::nullOrNotWhitespaceOnly($nameQualifier);
40
        Assert::nullOrNotWhitespaceOnly($spNameQualifier);
41
        Assert::nullOrValidURI($format); // Covers the empty string
42
        Assert::nullOrNotWhitespaceOnly($spProvidedID);
43
44
        $this->setContent($value);
45
    }
46
47
48
    /**
49
     * Collect the value of the Format-property
50
     *
51
     * @return string|null
52
     */
53
    public function getFormat(): ?string
54
    {
55
        return $this->format;
56
    }
57
58
59
    /**
60
     * Collect the value of the SPProvidedID-property
61
     *
62
     * @return string|null
63
     */
64
    public function getSPProvidedID(): ?string
65
    {
66
        return $this->spProvidedID;
67
    }
68
69
70
    /**
71
     * Validate the content of the element.
72
     *
73
     * @param string $content  The value to go in the XML textContent
74
     * @throws \Exception on failure
75
     * @return void
76
     */
77
    protected function validateContent(string $content): void
78
    {
79
        Assert::notWhitespaceOnly($content);
80
    }
81
82
83
    /**
84
     * Convert XML into an NameID
85
     *
86
     * @param \DOMElement $xml The XML element we should load
87
     * @return static
88
     *
89
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
90
     *   if the qualified name of the supplied element is wrong
91
     */
92
    public static function fromXML(DOMElement $xml): static
93
    {
94
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The type SimpleSAML\SAML2\XML\sam...alidDOMElementException 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...
95
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
96
97
        $NameQualifier = self::getOptionalAttribute($xml, 'NameQualifier', null);
98
        $SPNameQualifier = self::getOptionalAttribute($xml, 'SPNameQualifier', null);
99
        $Format = self::getOptionalAttribute($xml, 'Format', null);
100
        $SPProvidedID = self::getOptionalAttribute($xml, 'SPProvidedID', null);
101
102
        return new static($xml->textContent, $NameQualifier, $SPNameQualifier, $Format, $SPProvidedID);
103
    }
104
105
106
    /**
107
     * Convert this NameIDType to XML.
108
     *
109
     * @param \DOMElement $parent The element we are converting to XML.
110
     * @return \DOMElement The XML element after adding the data corresponding to this NameIDType.
111
     */
112
    public function toXML(DOMElement $parent = null): DOMElement
113
    {
114
        $e = $this->instantiateParentElement($parent);
115
116
        if ($this->getNameQualifier() !== null) {
117
            $e->setAttribute('NameQualifier', $this->getNameQualifier());
118
        }
119
120
        if ($this->getSPNameQualifier() !== null) {
121
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier());
122
        }
123
124
        if ($this->getFormat() !== null) {
125
            $e->setAttribute('Format', $this->getFormat());
126
        }
127
128
        if ($this->getSPProvidedID() !== null) {
129
            $e->setAttribute('SPProvidedID', $this->getSPProvidedID());
130
        }
131
132
        $e->textContent = $this->getContent();
133
        return $e;
134
    }
135
}
136