AbstractSignedMdElement::toXML()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 17
nc 6
nop 1
dl 0
loc 32
rs 9.0777
c 0
b 0
f 0
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
     * The original signed XML
31
     *
32
     * @var \DOMElement
33
     */
34
    protected DOMElement $xml;
35
36
37
    /**
38
     * Get the XML element.
39
     *
40
     * @return \DOMElement
41
     */
42
    public function getXML(): DOMElement
43
    {
44
        return $this->xml;
45
    }
46
47
48
    /**
49
     * Set the XML element.
50
     *
51
     * @param \DOMElement $xml
52
     */
53
    protected function setXML(DOMElement $xml): void
54
    {
55
        $this->xml = $xml;
56
    }
57
58
59
    /**
60
     * @param \DOMElement|null $parent The EntityDescriptor we should append this SPSSODescriptor to.
61
     * @return \DOMElement
62
     * @throws \Exception
63
     */
64
    public function toXML(?DOMElement $parent = null): DOMElement
65
    {
66
        if ($this->isSigned() === true && $this->signer === null) {
67
            // We already have a signed document and no signer was set to re-sign it
68
            if ($parent === null) {
69
                return $this->getXML();
70
            }
71
72
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
73
            $parent->appendChild($node);
74
            return $parent;
75
        }
76
77
        $e = $this->toUnsignedXML($parent);
78
        // This is a dirty hack, but if we add the xsi-type on AbstractRoleDescriptor we cannot
79
        // get the tests to pass because the attribute-order is messed up. This has something
80
        // to do with the fact that toUnsignedXML's recursive nature.
81
        if (method_exists(static::class, 'getXsiTypePrefix')) {
82
            $e->setAttributeNS(
83
                'http://www.w3.org/2000/xmlns/',
84
                '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

84
                'xmlns:' . static::/** @scrutinizer ignore-call */ getXsiTypePrefix(),
Loading history...
85
                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

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