Completed
Push — master ( da4b66...3ae217 )
by Tim
19s queued 15s
created

AbstractResponseAbstractType::toXML()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 24
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 24
rs 9.5555
cc 5
nc 4
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DateTimeImmutable;
8
use DOMElement;
9
10
/**
11
 * Base class for all SAML 1.1 responses.
12
 *
13
 * Implements what is common between the samlp:RequestAbstractType and
14
 * samlp:ResponseAbstractType element types.
15
 *
16
 * @package simplesamlphp/saml11
17
 */
18
abstract class AbstractResponseAbstractType extends AbstractMessage
19
{
20
    /**
21
     * Initialize a response.
22
     *
23
     * @param string $id
24
     * @param int $majorVersion
25
     * @param int $minorVersion
26
     * @param \DateTimeImmutable|null $issueInstant
27
     *
28
     * @throws \Exception
29
     */
30
    protected function __construct(
31
        protected string $id,
32
        int $majorVersion = 1,
33
        int $minorVersion = 1,
34
        ?DateTimeImmutable $issueInstant = null,
35
    ) {
36
        parent::__construct($majorVersion, $minorVersion, $issueInstant);
37
    }
38
39
40
    /**
41
     * Retrieve the identifier of this message.
42
     *
43
     * @return string The identifier of this message
44
     */
45
    public function getID(): string
46
    {
47
        return $this->id;
48
    }
49
50
51
    /**
52
     * Convert this message to an unsigned XML document.
53
     * This method does not sign the resulting XML document.
54
     *
55
     * @return \DOMElement The root element of the DOM tree
56
     */
57
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
58
    {
59
        $e = parent::toUnsignedXML($parent);
60
        $e->setAttribute('ResponseID', $this->getId());
61
62
        return $e;
63
    }
64
65
66
    /**
67
     * Create XML from this class
68
     *
69
     * @param \DOMElement|null $parent
70
     * @return \DOMElement
71
     */
72
    public function toXML(?DOMElement $parent = null): DOMElement
73
    {
74
        if ($this->isSigned() === true && $this->signer === null) {
75
            // We already have a signed document and no signer was set to re-sign it
76
            if ($parent === null) {
77
                return $this->xml;
78
            }
79
80
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
81
            $parent->appendChild($node);
82
83
            return $parent;
84
        }
85
86
        $e = $this->toUnsignedXML($parent);
87
88
        if ($this->signer !== null) {
89
            $signedXML = $this->doSign($e);
90
            $signedXML->appendChild($this->signature?->toXML($signedXML));
91
92
            return $signedXML;
93
        }
94
95
        return $e;
96
    }
97
}
98