Passed
Pull Request — master (#305)
by Jaime Pérez
02:19
created

Statement   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 60
rs 10
c 0
b 0
f 0
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 27 2
A toXML() 0 8 1
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\Constants as C;
10
use SimpleSAML\SAML2\Utils;
11
use SimpleSAML\SAML2\XML\ExtensionPointInterface;
12
use SimpleSAML\SAML2\XML\ExtensionPointTrait;
13
use SimpleSAML\SAML2\XML\saml\Statement;
14
use SimpleSAML\XML\Exception\InvalidDOMElementException;
15
use SimpleSAML\XML\Exception\SchemaViolationException;
16
17
use function count;
18
use function explode;
19
20
/**
21
 * Class implementing the <saml:Statement> extension point.
22
 *
23
 * @package simplesamlphp/saml2
24
 */
25
abstract class Statement extends AbstractStatementType implements ExtensionPointInterface
26
{
27
    use ExtensionPointTrait;
28
29
    /** @var string */
30
    public const LOCALNAME = 'Statement';
31
32
33
    /**
34
     * Convert an XML element into a Statement.
35
     *
36
     * @param \DOMElement $xml The root XML element
37
     * @return \SimpleSAML\SAML2\XML\saml\Statement The condition
38
     *
39
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException if the qualified name of the supplied element is wrong
40
     */
41
    public static function fromXML(DOMElement $xml): object
42
    {
43
        Assert::same($xml->localName, 'Statement', InvalidDOMElementException::class);
44
        Assert::same($xml->namespaceURI, C::NS_SAML, InvalidDOMElementException::class);
45
        Assert::true(
46
            $xml->hasAttributeNS(C::NS_XSI, 'type'),
47
            'Missing required xsi:type in <saml:Statement> element.',
48
            SchemaViolationException::class
49
        );
50
51
        $type = $xml->getAttributeNS(C::NS_XSI, 'type');
52
        Assert::validQName($type, SchemaViolationException::class);
53
54
        $qname = explode(':', $type, 2);
55
        if (count($qname) === 2) {
56
            list($prefix, $element) = $qname;
57
        } else {
58
            $prefix = null;
59
            list($element) = $qname;
60
        }
61
        $ns = $xml->lookupNamespaceUri($prefix);
62
        $handler = Utils::getContainer()->getElementHandler($ns, $element);
63
64
        Assert::notNull($handler, 'Unknown Statement type `' . $type . '`.');
65
        Assert::isAOf($handler, Statement::class);
66
67
        return $handler::fromXML($xml);
68
    }
69
70
71
    /**
72
     * Convert this Statement to XML.
73
     *
74
     * @param \DOMElement $parent The element we are converting to XML.
75
     * @return \DOMElement The XML element after adding the data corresponding to this Statement.
76
     */
77
    public function toXML(DOMElement $parent = null): DOMElement
78
    {
79
        $e = $this->instantiateParentElement($parent);
80
81
        $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...ent::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...
82
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', static::getXsiType());
0 ignored issues
show
Bug Best Practice introduced by
The method SimpleSAML\SAML2\XML\Ext...Interface::getXsiType() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

82
        $e->setAttributeNS(C::NS_XSI, 'xsi:type', static::/** @scrutinizer ignore-call */ getXsiType());
Loading history...
83
84
        return $e;
85
    }
86
}
87