Completed
Pull Request — master (#226)
by Jaime Pérez
08:39
created

AbstractSubjectQuery::setSubject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SAML2\XML\samlp;
6
7
use DOMElement;
8
use Exception;
9
use SAML2\Constants;
10
use SAML2\Utils;
11
use SAML2\XML\IdentifierTrait;
12
use SAML2\XML\saml\Issuer;
13
use SAML2\XML\saml\NameID;
14
use SAML2\XML\saml\Subject;
15
use Webmozart\Assert\Assert;
16
17
/**
18
 * Base class for SAML 2 subject query messages.
19
 *
20
 * This base class can be used for various requests which ask for
21
 * information about a particular subject.
22
 *
23
 * Note that this class currently only handles the simple case - where the
24
 * subject doesn't contain any sort of subject confirmation requirements.
25
 *
26
 * @package SimpleSAMLphp
27
 */
28
abstract class AbstractSubjectQuery extends AbstractRequest
29
{
30
    /** @var \SAML2\XML\saml\Subject */
31
    protected $subject;
32
33
34
35
    /**
36
     * Constructor for SAML 2 response messages.
37
     *
38
     * @param \SAML2\XML\saml\Subject $subject
39
     * @param \SAML2\XML\saml\Issuer $issuer
40
     * @param string $id
41
     * @param string $version
42
     * @param int $issueInstant
43
     * @param string|null $destination
44
     * @param string|null $consent
45
     * @param \SAML2\XML\samlp\Extensions $extensions
46
     */
47
    protected function __construct(
48
        Subject $subject,
49
        ?Issuer $issuer = null,
50
        ?string $id = null,
51
        ?string $version = '2.0',
52
        ?int $issueInstant = null,
53
        ?string $destination = null,
54
        ?string $consent = null,
55
        ?Extensions $extensions = null
56
    ) {
57
        parent::__construct($issuer, $id, $version, $issueInstant, $destination, $consent, $extensions);
58
59
        $this->setSubject($subject);
60
    }
61
62
63
    /**
64
     * Collect the value of the subject
65
     *
66
     * @return \SAML2\XML\saml\Subject
67
     */
68
    public function getSubject(): Subject
69
    {
70
        return $this->subject;
71
    }
72
73
74
    /**
75
     * Set the value of the subject-property
76
     * @param \SAML2\XML\saml\Subject $value
77
     *
78
     * @return void
79
     */
80
    private function setSubject(Subject $subject): void
81
    {
82
        $this->subject = $subject;
83
    }
84
85
86
    /**
87
     * Convert subject query message to an XML element.
88
     *
89
     * @return \DOMElement This subject query.
90
     *
91
     * @throws \InvalidArgumentException if assertions are false
92
     */
93
    public function toXML(?DOMElement $parent = null): DOMElement
94
    {
95
        Assert::null($parent);
96
        Assert::notEmpty($this->subject, 'Cannot convert SubjectQuery to XML without a Subject set.');
97
98
        $parent = parent::toXML();
99
100
        $this->subject->toXML($parent);
101
102
        return $parent;
103
    }
104
}
105