Passed
Push — master ( c66cd3...78be34 )
by Tim
12:20
created

AbstractSignedMdElement   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 82
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getXML() 0 3 1
A setXML() 0 3 1
A toXML() 0 22 5
A getBlacklistedAlgorithms() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\md;
6
7
use DOMElement;
8
use SimpleSAML\SAML2\Compat\ContainerSingleton;
9
use SimpleSAML\XMLSecurity\XML\SignableElementTrait;
10
use SimpleSAML\XMLSecurity\XML\SignableElementInterface;
11
use SimpleSAML\XMLSecurity\XML\SignedElementTrait;
12
use SimpleSAML\XMLSecurity\XML\SignedElementInterface;
13
14
/**
15
 * Abstract class that represents a signed metadata element.
16
 *
17
 * @package simplesamlphp/saml2
18
 */
19
abstract class AbstractSignedMdElement extends AbstractMdElement implements
20
    SignableElementInterface,
21
    SignedElementInterface
22
{
23
    use SignableElementTrait;
24
    use SignedElementTrait;
25
26
    /**
27
     * The original signed XML
28
     *
29
     * @var \DOMElement
30
     */
31
    protected DOMElement $xml;
32
33
34
    /**
35
     * Get the XML element.
36
     *
37
     * @return \DOMElement
38
     */
39
    public function getXML(): DOMElement
40
    {
41
        return $this->xml;
42
    }
43
44
45
    /**
46
     * Set the XML element.
47
     *
48
     * @param \DOMElement $xml
49
     */
50
    protected function setXML(DOMElement $xml): void
51
    {
52
        $this->xml = $xml;
53
    }
54
55
56
    /**
57
     * @return array|null
58
     */
59
    public function getBlacklistedAlgorithms(): ?array
60
    {
61
        $container = ContainerSingleton::getInstance();
62
        return $container->getBlacklistedEncryptionAlgorithms();
63
    }
64
65
66
    /**
67
     * @param \DOMElement|null $parent The EntityDescriptor we should append this SPSSODescriptor to.
68
     * @return \DOMElement
69
     * @throws \Exception
70
     */
71
    public function toXML(DOMElement $parent = null): DOMElement
72
    {
73
        if ($this->isSigned() === true && $this->signer === null) {
74
            // We already have a signed document and no signer was set to re-sign it
75
            if ($parent === null) {
76
                return $this->getXML();
77
            }
78
79
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
80
            $parent->appendChild($node);
81
            return $parent;
82
        }
83
84
        $e = $this->toUnsignedXML($parent);
85
86
        if ($this->signer !== null) {
87
            $signedXML = $this->doSign($e);
88
            $signedXML->insertBefore($this->signature?->toXML($signedXML), $signedXML->firstChild);
89
            return $signedXML;
90
        }
91
92
        return $e;
93
    }
94
95
96
    /**
97
     * @param  \DOMElement|null $parent
98
     * @return \DOMElement
99
     */
100
    abstract public function toUnsignedXML(DOMElement $parent = null): DOMElement;
101
}
102