AbstractResponseAbstractType::toXML()   A
last analyzed

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