Passed
Pull Request — master (#304)
by Tim
02:17
created

BaseID::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
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\SAML2\Compat\ContainerSingleton;
10
use SimpleSAML\SAML2\Constants as C;
11
use SimpleSAML\SAML2\Utils;
12
use SimpleSAML\SAML2\XML\IDNameQualifiersTrait;
13
use SimpleSAML\XML\Exception\SchemaViolationException;
14
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
15
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
16
use SimpleSAML\XMLSecurity\XML\EncryptableElementTrait;
17
18
use function trim;
19
20
/**
21
 * SAML BaseID data type.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
abstract class BaseID extends AbstractSamlElement implements CustomIdentifierInterface, EncryptableElementInterface
26
{
27
    use EncryptableElementTrait;
28
    use IDNameQualifiersTrait;
29
30
    /** @var string */
31
    public const LOCALNAME = 'BaseID';
32
33
    /** @var string */
34
    protected string $type;
35
36
37
    /**
38
     * Initialize a saml:BaseID from scratch
39
     *
40
     * @param string $type
41
     * @param string|null $NameQualifier
42
     * @param string|null $SPNameQualifier
43
     */
44
    protected function __construct(
45
        string $type,
46
        ?string $NameQualifier = null,
47
        ?string $SPNameQualifier = null
48
    ) {
49
        $this->setType($type);
50
        $this->setNameQualifier($NameQualifier);
51
        $this->setSPNameQualifier($SPNameQualifier);
52
    }
53
54
55
    /**
56
     * Get the type of this BaseID (expressed in the xsi:type attribute).
57
     *
58
     * @return string
59
     */
60
    public function getType(): string
61
    {
62
        return $this->type;
63
    }
64
65
66
    /**
67
     * Set the type of this BaseID (in the xsi:type attribute)
68
     *
69
     * @param string $type
70
     */
71
    protected function setType(string $type): void
72
    {
73
        Assert::validQName($type, SchemaViolationException::class); // Covers the empty string
74
        $this->type = $type;
75
    }
76
77
78
    public function getBlacklistedAlgorithms(): ?array
79
    {
80
        $container = ContainerSingleton::getInstance();
81
        return $container->getBlacklistedEncryptionAlgorithms();
82
    }
83
84
85
    public function getEncryptionBackend(): ?EncryptionBackend
86
    {
87
        // return the encryption backend you want to use,
88
        // or null if you are fine with the default
89
        return null;
90
    }
91
92
93
    /**
94
     * Convert an XML element into a message.
95
     *
96
     * @param \DOMElement $xml The root XML element
97
     * @return \SimpleSAML\SAML2\XML\saml\BaseID The identifier
98
     *
99
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
100
     */
101
    public static function fromXML(DOMElement $xml): object
102
    {
103
        Assert::same($xml->localName, 'BaseID', 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...
104
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
105
        Assert::true(
106
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
107
            'Missing required xsi:type in <saml:BaseID> element.',
108
            InvalidDOMElementException::class
109
        );
110
111
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
112
        Assert::validQName($type, SchemaViolationException::class);
113
114
        list($prefix, $element) = explode(':', $type, 2);
115
        $ns = $xml->lookupNamespaceUri($prefix);
116
        $handler = Utils::getContainer()->getElementHandler($ns, $element);
117
118
        Assert::notNull($handler, 'Unknown BaseID type `' . $type . '`.');
119
        Assert::isAOf($handler, BaseID::class);
120
121
        return $handler::fromXML($xml);
122
    }
123
124
125
    /**
126
     * Convert this BaseID to XML.
127
     *
128
     * @param \DOMElement $parent The element we are converting to XML.
129
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
130
     */
131
    public function toXML(DOMElement $parent = null): DOMElement
132
    {
133
        $e = $this->instantiateParentElement($parent);
134
        $e->setAttribute('xmlns:' . static::XSI_TYPE_PREFIX, static::XSI_TYPE_NS);
0 ignored issues
show
Bug introduced by
The constant SimpleSAML\SAML2\XML\saml\BaseID::XSI_TYPE_PREFIX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
Bug introduced by
The constant SimpleSAML\SAML2\XML\saml\BaseID::XSI_TYPE_NS was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
135
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', $this->type);
136
137
        if ($this->NameQualifier !== null) {
138
            $e->setAttribute('NameQualifier', $this->NameQualifier);
139
        }
140
141
        if ($this->SPNameQualifier !== null) {
142
            $e->setAttribute('SPNameQualifier', $this->SPNameQualifier);
143
        }
144
145
        return $e;
146
    }
147
}
148