Subject   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 92
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A toXML() 0 13 2
A __construct() 0 15 2
A fromXML() 0 20 2
A getSubjectConfirmation() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\SAML2\XML\saml;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\SAML2\XML\IdentifierTrait;
10
use SimpleSAML\XML\Constants as C;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
use SimpleSAML\XML\SchemaValidatableElementInterface;
14
use SimpleSAML\XML\SchemaValidatableElementTrait;
15
16
/**
17
 * Class representing SAML 2 Subject element.
18
 *
19
 * @package simplesamlphp/saml2
20
 */
21
final class Subject extends AbstractSamlElement implements SchemaValidatableElementInterface
22
{
23
    use IdentifierTrait;
24
    use SchemaValidatableElementTrait;
25
26
    /**
27
     * Initialize a Subject element.
28
     *
29
     * @param \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null $identifier
30
     * @param \SimpleSAML\SAML2\XML\saml\SubjectConfirmation[] $subjectConfirmation
31
     */
32
    public function __construct(
33
        ?IdentifierInterface $identifier,
34
        protected array $subjectConfirmation = [],
35
    ) {
36
        if (empty($subjectConfirmation)) {
37
            Assert::notNull(
38
                $identifier,
39
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide exactly one of '
40
                    . '<saml:BaseID>, <saml:NameID> or <saml:EncryptedID>',
41
            );
42
        }
43
        Assert::maxCount($subjectConfirmation, C::UNBOUNDED_LIMIT);
44
        Assert::allIsInstanceOf($subjectConfirmation, SubjectConfirmation::class);
45
46
        $this->setIdentifier($identifier);
47
    }
48
49
50
    /**
51
     * Collect the value of the SubjectConfirmation-property
52
     *
53
     * @return \SimpleSAML\SAML2\XML\saml\SubjectConfirmation[]
54
     */
55
    public function getSubjectConfirmation(): array
56
    {
57
        return $this->subjectConfirmation;
58
    }
59
60
61
    /**
62
     * Convert XML into a Subject
63
     *
64
     * @param \DOMElement $xml The XML element we should load
65
     * @return static
66
     *
67
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
68
     *   if the qualified name of the supplied element is wrong
69
     */
70
    public static function fromXML(DOMElement $xml): static
71
    {
72
        Assert::same($xml->localName, 'Subject', InvalidDOMElementException::class);
73
        Assert::same($xml->namespaceURI, Subject::NS, InvalidDOMElementException::class);
74
75
        $identifier = self::getIdentifierFromXML($xml);
76
        $subjectConfirmation = SubjectConfirmation::getChildrenOfClass($xml);
77
78
        if (empty($subjectConfirmation)) {
79
            Assert::notNull(
80
                $identifier,
81
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide' .
82
                ' exactly one of <saml:BaseID>, <saml:NameID> or <saml:EncryptedID>',
83
                TooManyElementsException::class,
84
            );
85
        }
86
87
        return new static(
88
            $identifier,
89
            $subjectConfirmation,
90
        );
91
    }
92
93
94
    /**
95
     * Convert this element to XML.
96
     *
97
     * @param  \DOMElement|null $parent The parent element we should append this element to.
98
     * @return \DOMElement This element, as XML.
99
     */
100
    public function toXML(?DOMElement $parent = null): DOMElement
101
    {
102
        $e = $this->instantiateParentElement($parent);
103
104
        /** @var \SimpleSAML\XML\SerializableElementInterface|null $identifier */
105
        $identifier = $this->getIdentifier();
106
        $identifier?->toXML($e);
107
108
        foreach ($this->getSubjectConfirmation() as $sc) {
109
            $sc->toXML($e);
110
        }
111
112
        return $e;
113
    }
114
}
115