AbstractRequestAbstractType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 26
c 2
b 0
f 0
dl 0
loc 104
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRespondWith() 0 3 1
A __construct() 0 10 1
A toXML() 0 33 6
A toUnsignedXML() 0 10 2
A getID() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML11\XML\samlp;
6
7
use DOMElement;
8
use SimpleSAML\SAML11\Assert\Assert;
9
use SimpleSAML\SAML11\Type\SAMLDateTimeValue;
10
use SimpleSAML\SAML11\Utils\XPath;
11
use SimpleSAML\XMLSchema\Exception\SchemaViolationException;
12
use SimpleSAML\XMLSchema\Type\{IDValue, NonNegativeIntegerValue};
13
14
use function array_pop;
15
use function strval;
16
17
/**
18
 * Base class for all SAML 1.1 requests.
19
 *
20
 * @package simplesamlphp/saml11
21
 */
22
abstract class AbstractRequestAbstractType extends AbstractMessage
23
{
24
    /**
25
     * Initialize a request.
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 $issueInstant
31
     * @param array<\SimpleSAML\SAML11\XML\samlp\RespondWith>
32
     *
33
     * @throws \Exception
34
     */
35
    protected function __construct(
36
        protected IDValue $id,
37
        protected NonNegativeIntegerValue $majorVersion,
38
        protected NonNegativeIntegerValue $minorVersion,
39
        protected SAMLDateTimeValue $issueInstant,
40
        protected array $respondWith = [],
41
    ) {
42
        Assert::allIsInstanceOf($respondWith, RespondWith::class, SchemaViolationException::class);
43
44
        parent::__construct($majorVersion, $minorVersion, $issueInstant);
45
    }
46
47
48
    /**
49
     * Retrieve the ID of this request.
50
     *
51
     * @return \SimpleSAML\XMLSchema\Type\IDValue The ID of this request
52
     */
53
    public function getID(): IDValue
54
    {
55
        return $this->id;
56
    }
57
58
59
    /**
60
     * @return array<\SimpleSAML\SAML11\XML\samlp\RespondWith>
61
     */
62
    public function getRespondWith(): array
63
    {
64
        return $this->respondWith;
65
    }
66
67
68
    /**
69
     * Convert this message to an unsigned XML document.
70
     * This method does not sign the resulting XML document.
71
     *
72
     * @return \DOMElement The root element of the DOM tree
73
     */
74
    protected function toUnsignedXML(?DOMElement $parent = null): DOMElement
75
    {
76
        $e = parent::toUnsignedXML($parent);
77
        $e->setAttribute('RequestID', strval($this->getID()));
78
79
        foreach ($this->getRespondWith() as $respondWith) {
80
            $respondWith->toXML($e);
81
        }
82
83
        return $e;
84
    }
85
86
87
    /**
88
     * Create XML from this class
89
     *
90
     * @param \DOMElement|null $parent
91
     * @return \DOMElement
92
     */
93
    public function toXML(?DOMElement $parent = null): DOMElement
94
    {
95
        if ($this->isSigned() === true && $this->signer === null) {
96
            // We already have a signed document and no signer was set to re-sign it
97
            if ($parent === null) {
98
                return $this->xml;
99
            }
100
101
            $node = $parent->ownerDocument?->importNode($this->getXML(), true);
102
            $parent->appendChild($node);
103
104
            return $parent;
105
        }
106
107
        $e = $this->toUnsignedXML($parent);
108
109
        if ($this->signer !== null) {
110
            $signedXML = $this->doSign($e);
111
112
            // Test for an RespondWith
113
            $messageElements = XPath::xpQuery($signedXML, './saml_protocol:RespondWith', XPath::getXPath($signedXML));
114
            $respondWith = array_pop($messageElements);
115
116
            if ($respondWith === null) {
117
                $signedXML->appendChild($this->signature?->toXML($signedXML));
118
            } else {
119
                $signedXML->insertBefore($this->signature?->toXML($signedXML), $respondWith->nextSibling);
120
            }
121
122
            return $signedXML;
123
        }
124
125
        return $e;
126
    }
127
}
128