AbstractCondition   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 90
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 28
c 1
b 0
f 0
dl 0
loc 90
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 25 2
A getXsiType() 0 3 1
A __construct() 0 3 1
A toXML() 0 16 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\SAML11\Assert\Assert;
9
use SimpleSAML\SAML11\Constants as C;
10
use SimpleSAML\SAML11\Utils;
11
use SimpleSAML\SAML11\XML\{ExtensionPointInterface, ExtensionPointTrait};
12
use SimpleSAML\XML\Attribute as XMLAttribute;
13
use SimpleSAML\XML\Chunk;
14
use SimpleSAML\XML\{SchemaValidatableElementInterface, SchemaValidatableElementTrait};
15
use SimpleSAML\XMLSchema\Constants as C_XSI;
16
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, SchemaViolationException};
17
use SimpleSAML\XMLSchema\Type\QNameValue;
18
19
use function strval;
20
21
/**
22
 * SAML Condition data type.
23
 *
24
 * @package simplesamlphp/saml11
25
 */
26
abstract class AbstractCondition extends AbstractConditionType implements
27
    ExtensionPointInterface,
28
    SchemaValidatableElementInterface
29
{
30
    use ExtensionPointTrait;
31
    use SchemaValidatableElementTrait;
32
33
    /** @var string */
34
    public const LOCALNAME = 'Condition';
35
36
37
    /**
38
     * Initialize a custom saml:Condition element.
39
     *
40
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
41
     */
42
    protected function __construct(
43
        protected QNameValue $type,
44
    ) {
45
    }
46
47
48
    /**
49
     * @return \SimpleSAML\XMLSchema\Type\QNameValue
50
     */
51
    public function getXsiType(): QNameValue
52
    {
53
        return $this->type;
54
    }
55
56
57
    /**
58
     * Convert an XML element into a Condition.
59
     *
60
     * @param \DOMElement $xml The root XML element
61
     * @return static
62
     *
63
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
64
     *   if the qualified name of the supplied element is wrong
65
     */
66
    public static function fromXML(DOMElement $xml): static
67
    {
68
        Assert::same($xml->localName, 'Condition', InvalidDOMElementException::class);
69
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
70
        Assert::true(
71
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
72
            'Missing required xsi:type in <saml:Condition> element.',
73
            SchemaViolationException::class,
74
        );
75
76
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
77
78
        // now check if we have a handler registered for it
79
        $handler = Utils::getContainer()->getExtensionHandler($type);
80
        if ($handler === null) {
81
            // we don't have a handler, proceed with unknown condition
82
            return new UnknownCondition(new Chunk($xml), $type);
83
        }
84
85
        Assert::subclassOf(
86
            $handler,
87
            AbstractCondition::class,
88
            'Elements implementing Condition must extend \SimpleSAML\SAML11\XML\saml\AbstractCondition.',
89
        );
90
        return $handler::fromXML($xml);
91
    }
92
93
94
    /**
95
     * Convert this Condition to XML.
96
     *
97
     * @param \DOMElement $parent The element we are converting to XML.
98
     * @return \DOMElement The XML element after adding the data corresponding to this Condition.
99
     */
100
    public function toXML(?DOMElement $parent = null): DOMElement
101
    {
102
        $e = $this->instantiateParentElement($parent);
103
104
        if (!$e->lookupPrefix($this->getXsiType()->getNamespaceURI()->getValue())) {
105
            $e->setAttributeNS(
106
                'http://www.w3.org/2000/xmlns/',
107
                'xmlns:' . static::getXsiTypePrefix(),
108
                strval(static::getXsiTypeNamespaceURI()),
109
            );
110
        }
111
112
        $type = new XMLAttribute(C_XSI::NS_XSI, 'xsi', 'type', $this->getXsiType());
113
        $type->toXML($e);
114
115
        return $e;
116
    }
117
}
118