AbstractSignedMdElement   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 85
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getXML() 0 3 1
A setXML() 0 3 1
A toXML() 0 32 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\XML\SignableElementTrait;
9
use SimpleSAML\SAML2\XML\SignedElementTrait;
10
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
11
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
12
13
use function method_exists;
14
15
/**
16
 * Abstract class that represents a signed metadata element.
17
 *
18
 * @package simplesamlphp/saml2
19
 */
20
abstract class AbstractSignedMdElement extends AbstractMdElement implements
21
    SignableElementInterface,
22
    SignedElementInterface
23
{
24
    use SignableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\SignableElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractSignedMdElement: $ownerDocument, $documentElement
Loading history...
25
    use SignedElementTrait {
0 ignored issues
show
introduced by
The trait SimpleSAML\SAML2\XML\SignedElementTrait requires some properties which are not provided by SimpleSAML\SAML2\XML\md\AbstractSignedMdElement: $ownerDocument, $documentElement
Loading history...
26
        SignedElementTrait::getBlacklistedAlgorithms insteadof SignableElementTrait;
27
    }
28
29
30
    /**
31
     * The original signed XML
32
     *
33
     * @var \DOMElement
34
     */
35
    protected DOMElement $xml;
36
37
38
    /**
39
     * Get the XML element.
40
     *
41
     * @return \DOMElement
42
     */
43
    public function getXML(): DOMElement
44
    {
45
        return $this->xml;
46
    }
47
48
49
    /**
50
     * Set the XML element.
51
     *
52
     * @param \DOMElement $xml
53
     */
54
    protected function setXML(DOMElement $xml): void
55
    {
56
        $this->xml = $xml;
57
    }
58
59
60
    /**
61
     * @param \DOMElement|null $parent The EntityDescriptor we should append this SPSSODescriptor to.
62
     * @return \DOMElement
63
     * @throws \Exception
64
     */
65
    public function toXML(?DOMElement $parent = null): DOMElement
66
    {
67
        if ($this->isSigned() === true && $this->signer === null) {
68
            // We already have a signed document and no signer was set to re-sign it
69
            if ($parent === null) {
70
                return $this->getXML();
71
            }
72
73
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
74
            $parent->appendChild($node);
75
            return $parent;
76
        }
77
78
        $e = $this->toUnsignedXML($parent);
79
        // This is a dirty hack, but if we add the xsi-type on AbstractRoleDescriptor we cannot
80
        // get the tests to pass because the attribute-order is messed up. This has something
81
        // to do with the fact that toUnsignedXML's recursive nature.
82
        if (method_exists(static::class, 'getXsiTypePrefix')) {
83
            $e->setAttributeNS(
84
                'http://www.w3.org/2000/xmlns/',
85
                'xmlns:' . static::getXsiTypePrefix(),
0 ignored issues
show
Bug introduced by
The method getXsiTypePrefix() does not exist on SimpleSAML\SAML2\XML\md\AbstractSignedMdElement. It seems like you code against a sub-type of SimpleSAML\SAML2\XML\md\AbstractSignedMdElement such as SimpleSAML\SAML2\XML\md\AbstractRoleDescriptor. ( Ignorable by Annotation )

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

85
                'xmlns:' . static::/** @scrutinizer ignore-call */ getXsiTypePrefix(),
Loading history...
86
                static::getXsiTypeNamespaceURI(),
0 ignored issues
show
Bug introduced by
The method getXsiTypeNamespaceURI() does not exist on SimpleSAML\SAML2\XML\md\AbstractSignedMdElement. It seems like you code against a sub-type of SimpleSAML\SAML2\XML\md\AbstractSignedMdElement such as SimpleSAML\SAML2\XML\md\AbstractRoleDescriptor. ( Ignorable by Annotation )

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

86
                static::/** @scrutinizer ignore-call */ 
87
                        getXsiTypeNamespaceURI(),
Loading history...
87
            );
88
        }
89
90
        if ($this->signer !== null) {
91
            $signedXML = $this->doSign($e);
92
            $signedXML->insertBefore($this->signature?->toXML($signedXML), $signedXML->firstChild);
93
            return $signedXML;
94
        }
95
96
        return $e;
97
    }
98
99
100
    /**
101
     * @param  \DOMElement|null $parent
102
     * @return \DOMElement
103
     */
104
    abstract public function toUnsignedXML(?DOMElement $parent = null): DOMElement;
105
}
106