Passed
Pull Request — master (#305)
by Tim
02:21
created

BaseID::fromXML()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

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