Passed
Pull Request — master (#226)
by Jaime Pérez
03:06
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 SAML2\XML\saml\Issuer;
9
use SAML2\XML\saml\Subject;
10
use Webmozart\Assert\Assert;
11
12
/**
13
 * Base class for SAML 2 subject query messages.
14
 *
15
 * This base class can be used for various requests which ask for
16
 * information about a particular subject.
17
 *
18
 * Note that this class currently only handles the simple case - where the
19
 * subject doesn't contain any sort of subject confirmation requirements.
20
 *
21
 * @package SimpleSAMLphp
22
 */
23
abstract class AbstractSubjectQuery extends AbstractRequest
24
{
25
    /** @var \SAML2\XML\saml\Subject */
26
    protected $subject;
27
28
29
30
    /**
31
     * Constructor for SAML 2 response messages.
32
     *
33
     * @param \SAML2\XML\saml\Subject $subject
34
     * @param \SAML2\XML\saml\Issuer $issuer
35
     * @param string $id
36
     * @param int $issueInstant
37
     * @param string|null $destination
38
     * @param string|null $consent
39
     * @param \SAML2\XML\samlp\Extensions $extensions
40
     */
41
    protected function __construct(
42
        Subject $subject,
43
        ?Issuer $issuer = null,
44
        ?string $id = null,
45
        ?int $issueInstant = null,
46
        ?string $destination = null,
47
        ?string $consent = null,
48
        ?Extensions $extensions = null
49
    ) {
50
        parent::__construct($issuer, $id, $issueInstant, $destination, $consent, $extensions);
51
52
        $this->setSubject($subject);
53
    }
54
55
56
    /**
57
     * Collect the value of the subject
58
     *
59
     * @return \SAML2\XML\saml\Subject
60
     */
61
    public function getSubject(): Subject
62
    {
63
        return $this->subject;
64
    }
65
66
67
    /**
68
     * Set the value of the subject-property
69
     * @param \SAML2\XML\saml\Subject $subject
70
     *
71
     * @return void
72
     */
73
    private function setSubject(Subject $subject): void
74
    {
75
        $this->subject = $subject;
76
    }
77
78
79
    /**
80
     * Convert subject query message to an XML element.
81
     *
82
     * @return \DOMElement This subject query.
83
     *
84
     * @throws \InvalidArgumentException if assertions are false
85
     */
86
    public function toXML(?DOMElement $parent = null): DOMElement
87
    {
88
        Assert::notEmpty($this->subject, 'Cannot convert SubjectQuery to XML without a Subject set.');
89
90
        $parent = parent::toXML($parent);
91
92
        $this->subject->toXML($parent);
93
94
        return $parent;
95
    }
96
}
97