AbstractSubjectQuery   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 3
eloc 22
c 2
b 1
f 0
dl 0
loc 64
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A fromXML() 0 30 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
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;
12
use SimpleSAML\SAML11\XML\ExtensionPointTrait;
13
use SimpleSAML\SAML11\XML\saml\Subject;
14
use SimpleSAML\XML\Chunk;
15
use SimpleSAML\XML\SchemaValidatableElementInterface;
16
use SimpleSAML\XML\SchemaValidatableElementTrait;
17
use SimpleSAML\XMLSchema\Constants as C_XSI;
18
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
19
use SimpleSAML\XMLSchema\Exception\MissingElementException;
20
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
21
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
22
use SimpleSAML\XMLSchema\Type\QNameValue;
23
24
use function array_pop;
25
26
/**
27
 * SAMLP Query data type.
28
 *
29
 * @package simplesamlphp/saml11
30
 */
31
abstract class AbstractSubjectQuery extends AbstractSubjectQueryAbstractType implements
32
    ExtensionPointInterface,
33
    SchemaValidatableElementInterface
34
{
35
    use ExtensionPointTrait;
36
    use SchemaValidatableElementTrait;
37
38
39
    /** @var string */
40
    public const LOCALNAME = 'SubjectQuery';
41
42
43
    /**
44
     * Initialize a custom samlp:SubjectQuery element.
45
     *
46
     * @param \SimpleSAML\XMLSchema\Type\QNameValue $type
47
     */
48
    protected function __construct(
49
        protected QNameValue $type,
50
        Subject $subject,
51
    ) {
52
        parent::__construct($subject);
53
    }
54
55
56
    /**
57
     * Convert an XML element into a SubjectQuery.
58
     *
59
     * @param \DOMElement $xml The root XML element
60
     * @return static
61
     *
62
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
63
     *   if the qualified name of the supplied element is wrong
64
     */
65
    public static function fromXML(DOMElement $xml): static
66
    {
67
        Assert::same($xml->localName, 'SubjectQuery', InvalidDOMElementException::class);
68
        Assert::same($xml->namespaceURI, C::NS_SAMLP, InvalidDOMElementException::class);
69
        Assert::true(
70
            $xml->hasAttributeNS(C_XSI::NS_XSI, 'type'),
71
            'Missing required xsi:type in <samlp:SubjectQuery> element.',
72
            SchemaViolationException::class,
73
        );
74
75
        $type = QNameValue::fromDocument($xml->getAttributeNS(C_XSI::NS_XSI, 'type'), $xml);
76
77
        // now check if we have a handler registered for it
78
        $handler = Utils::getContainer()->getExtensionHandler($type);
79
        if ($handler === null) {
80
            $subject = Subject::getChildrenOfClass($xml);
81
            Assert::minCount($subject, 1, MissingElementException::class);
82
            Assert::maxCount($subject, 1, TooManyElementsException::class);
83
84
            // we don't have a handler, proceed with unknown query
85
            return new UnknownSubjectQuery(new Chunk($xml), $type, array_pop($subject));
86
        }
87
88
        Assert::subclassOf(
89
            $handler,
90
            AbstractSubjectQuery::class,
91
            'Elements implementing SubjectQuery must extend \SimpleSAML\SAML11\XML\samlp\AbstractSubjectQuery.',
92
        );
93
94
        return $handler::fromXML($xml);
95
    }
96
}
97