AbstractBaseID::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Assert\Assert;
9
use SimpleSAML\SAML2\Constants as C;
10
use SimpleSAML\SAML2\Type\SAMLStringValue;
11
use SimpleSAML\SAML2\Utils;
12
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
13
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
14
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
15
use SimpleSAML\XML\Attribute as XMLAttribute;
16
use SimpleSAML\XML\Chunk;
17
use SimpleSAML\XML\SchemaValidatableElementInterface;
18
use SimpleSAML\XML\SchemaValidatableElementTrait;
19
use SimpleSAML\XMLSchema\Constants as C_XSI;
20
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
21
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
22
use SimpleSAML\XMLSchema\Type\QNameValue;
23
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
24
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
25
26
/**
27
 * SAML BaseID data type.
28
 *
29
 * @package simplesamlphp/saml2
30
 */
31
abstract class AbstractBaseID extends AbstractBaseIDType implements
32
    EncryptableElementInterface,
33
    ExtensionPointInterface,
34
    SchemaValidatableElementInterface
35
{
36
    use EncryptableElementTrait;
0 ignored issues
show
Bug introduced by
The trait SimpleSAML\SAML2\XML\EncryptableElementTrait requires the property $ownerDocument which is not provided by SimpleSAML\SAML2\XML\saml\AbstractBaseID.
Loading history...
37
    use ExtensionPointTrait;
38
    use SchemaValidatableElementTrait;
39
40
41
    /** @var string */
42
    public const LOCALNAME = 'BaseID';
43
44
45
    /**
46
     * Initialize a saml:BaseID from scratch
47
     *
48
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
49
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $NameQualifier
50
     * @param \SimpleSAML\SAML2\Type\SAMLStringValue|null $SPNameQualifier
51
     */
52
    protected function __construct(
53
        protected QNameValue $type,
54
        ?SAMLStringValue $NameQualifier = null,
55
        ?SAMLStringValue $SPNameQualifier = null,
56
    ) {
57
        parent::__construct($NameQualifier, $SPNameQualifier);
58
    }
59
60
61
    /**
62
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
63
     */
64
    public function getXsiType(): QNameValue
65
    {
66
        return $this->type;
67
    }
68
69
70
    /**
71
     * Convert XML into an BaseID
72
     *
73
     * @param \DOMElement $xml The XML element we should load
74
     * @return static
75
     *
76
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
77
     *   if the qualified name of the supplied element is wrong
78
     */
79
    public static function fromXML(DOMElement $xml): static
80
    {
81
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
82
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
83
        Assert::true(
84
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
85
            'Missing required xsi:type in <saml:BaseID> element.',
86
            SchemaViolationException::class,
87
        );
88
89
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
90
91
        // now check if we have a handler registered for it
92
        $handler = Utils::getContainer()->getExtensionHandler($type);
93
        if ($handler === null) {
94
            // we don't have a handler, proceed with unknown identifier
95
            return new UnknownID(
96
                new Chunk($xml),
97
                $type,
98
                self::getOptionalAttribute($xml, 'NameQualifier', SAMLStringValue::class, null),
99
                self::getOptionalAttribute($xml, 'SPNameQualifier', SAMLStringValue::class, null),
100
            );
101
        }
102
103
        Assert::subclassOf(
104
            $handler,
105
            AbstractBaseID::class,
106
            'Elements implementing BaseID must extend \SimpleSAML\SAML2\XML\saml\AbstractBaseID.',
107
        );
108
        return $handler::fromXML($xml);
109
    }
110
111
112
    /**
113
     * Convert this BaseID to XML.
114
     *
115
     * @param \DOMElement $parent The element we are converting to XML.
116
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
117
     */
118
    public function toXML(?DOMElement $parent = null): DOMElement
119
    {
120
        $e = parent::toXML($parent);
121
        $e->setAttributeNS(
122
            'http://www.w3.org/2000/xmlns/',
123
            'xmlns:' . static::getXsiTypePrefix(),
124
            static::getXsiTypeNamespaceURI()->getValue(),
125
        );
126
127
        $type = new XMLAttribute(C_XSI::NS_XSI, 'xsi', 'type', $this->getXsiType());
128
        $type->toXML($e);
129
130
        if ($this->getNameQualifier() !== null) {
131
            $e->setAttribute('NameQualifier', $this->getNameQualifier()->getValue());
132
        }
133
134
        if ($this->getSPNameQualifier() !== null) {
135
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier()->getValue());
136
        }
137
138
        return $e;
139
    }
140
141
142
    public function getEncryptionBackend(): ?EncryptionBackend
143
    {
144
        // return the encryption backend you want to use,
145
        // or null if you are fine with the default
146
        return null;
147
    }
148
}
149