AbstractResponseAbstractType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 17
c 1
b 0
f 0
dl 0
loc 78
rs 10

4 Methods

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