AbstractSubjectStatement   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

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

4 Methods

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