AbstractBaseID   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 47
dl 0
loc 127
rs 10
c 0
b 0
f 0
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 21 3
A __construct() 0 6 1
A getXsiType() 0 3 1
A fromXML() 0 42 4
A getEncryptionBackend() 0 5 1
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\Utils;
11
use SimpleSAML\SAML2\XML\EncryptableElementTrait;
12
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
13
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
14
use SimpleSAML\XML\Attribute as XMLAttribute;
15
use SimpleSAML\XML\Chunk;
16
use SimpleSAML\XML\Exception\InvalidDOMElementException;
17
use SimpleSAML\XML\Exception\SchemaViolationException;
18
use SimpleSAML\XML\SchemaValidatableElementInterface;
19
use SimpleSAML\XML\SchemaValidatableElementTrait;
20
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
21
use SimpleSAML\XMLSecurity\XML\EncryptableElementInterface;
22
23
use function count;
24
use function explode;
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
    /** @var string */
41
    public const LOCALNAME = 'BaseID';
42
43
44
    /**
45
     * Initialize a saml:BaseID from scratch
46
     *
47
     * @param string $type
48
     * @param string|null $NameQualifier
49
     * @param string|null $SPNameQualifier
50
     */
51
    protected function __construct(
52
        protected string $type,
53
        ?string $NameQualifier = null,
54
        ?string $SPNameQualifier = null,
55
    ) {
56
        parent::__construct($NameQualifier, $SPNameQualifier);
57
    }
58
59
60
    /**
61
     * @inheritDoc
62
     */
63
    public function getXsiType(): string
64
    {
65
        return $this->type;
66
    }
67
68
69
    /**
70
     * Convert XML into an BaseID
71
     *
72
     * @param \DOMElement $xml The XML element we should load
73
     * @return static
74
     *
75
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
76
     *   if the qualified name of the supplied element is wrong
77
     */
78
    public static function fromXML(DOMElement $xml): static
79
    {
80
        Assert::same($xml->localName, 'BaseID', InvalidDOMElementException::class);
81
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
82
        Assert::true(
83
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
84
            'Missing required xsi:type in <saml:BaseID> element.',
85
            SchemaViolationException::class,
86
        );
87
88
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
89
        Assert::validQName($type, SchemaViolationException::class);
90
91
        // first, try to resolve the type to a full namespaced version
92
        $qname = explode(':', $type, 2);
93
        if (count($qname) === 2) {
94
            list($prefix, $element) = $qname;
95
        } else {
96
            $prefix = null;
97
            list($element) = $qname;
98
        }
99
        $ns = $xml->lookupNamespaceUri($prefix);
100
        $type = ($ns === null) ? $element : implode(':', [$ns, $element]);
101
102
        // now check if we have a handler registered for it
103
        $handler = Utils::getContainer()->getExtensionHandler($type);
104
        if ($handler === null) {
105
            // we don't have a handler, proceed with unknown identifier
106
            return new UnknownID(
107
                new Chunk($xml),
108
                $type,
109
                self::getOptionalAttribute($xml, 'NameQualifier', null),
110
                self::getOptionalAttribute($xml, 'SPNameQualifier', null),
111
            );
112
        }
113
114
        Assert::subclassOf(
115
            $handler,
116
            AbstractBaseID::class,
117
            'Elements implementing BaseID must extend \SimpleSAML\SAML2\XML\saml\AbstractBaseID.',
118
        );
119
        return $handler::fromXML($xml);
120
    }
121
122
123
    /**
124
     * Convert this BaseID to XML.
125
     *
126
     * @param \DOMElement $parent The element we are converting to XML.
127
     * @return \DOMElement The XML element after adding the data corresponding to this BaseID.
128
     */
129
    public function toXML(?DOMElement $parent = null): DOMElement
130
    {
131
        $e = parent::toXML($parent);
132
        $e->setAttributeNS(
133
            'http://www.w3.org/2000/xmlns/',
134
            'xmlns:' . static::getXsiTypePrefix(),
135
            static::getXsiTypeNamespaceURI(),
136
        );
137
138
        $type = new XMLAttribute(C::NS_XSI, 'xsi', 'type', $this->getXsiType());
139
        $type->toXML($e);
140
141
        if ($this->getNameQualifier() !== null) {
142
            $e->setAttribute('NameQualifier', $this->getNameQualifier());
143
        }
144
145
        if ($this->getSPNameQualifier() !== null) {
146
            $e->setAttribute('SPNameQualifier', $this->getSPNameQualifier());
147
        }
148
149
        return $e;
150
    }
151
152
153
    public function getEncryptionBackend(): ?EncryptionBackend
154
    {
155
        // return the encryption backend you want to use,
156
        // or null if you are fine with the default
157
        return null;
158
    }
159
}
160