Subject   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 93
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
    /**
28
     * Initialize a Subject element.
29
     *
30
     * @param \SimpleSAML\SAML2\XML\saml\IdentifierInterface|null $identifier
31
     * @param \SimpleSAML\SAML2\XML\saml\SubjectConfirmation[] $subjectConfirmation
32
     */
33
    public function __construct(
34
        ?IdentifierInterface $identifier,
35
        protected array $subjectConfirmation = [],
36
    ) {
37
        if (empty($subjectConfirmation)) {
38
            Assert::notNull(
39
                $identifier,
40
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide exactly one of '
41
                    . '<saml:BaseID>, <saml:NameID> or <saml:EncryptedID>',
42
            );
43
        }
44
        Assert::maxCount($subjectConfirmation, C::UNBOUNDED_LIMIT);
45
        Assert::allIsInstanceOf($subjectConfirmation, SubjectConfirmation::class);
46
47
        $this->setIdentifier($identifier);
48
    }
49
50
51
    /**
52
     * Collect the value of the SubjectConfirmation-property
53
     *
54
     * @return \SimpleSAML\SAML2\XML\saml\SubjectConfirmation[]
55
     */
56
    public function getSubjectConfirmation(): array
57
    {
58
        return $this->subjectConfirmation;
59
    }
60
61
62
    /**
63
     * Convert XML into a Subject
64
     *
65
     * @param \DOMElement $xml The XML element we should load
66
     * @return static
67
     *
68
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
69
     *   if the qualified name of the supplied element is wrong
70
     */
71
    public static function fromXML(DOMElement $xml): static
72
    {
73
        Assert::same($xml->localName, 'Subject', InvalidDOMElementException::class);
74
        Assert::same($xml->namespaceURI, Subject::NS, InvalidDOMElementException::class);
75
76
        $identifier = self::getIdentifierFromXML($xml);
77
        $subjectConfirmation = SubjectConfirmation::getChildrenOfClass($xml);
78
79
        if (empty($subjectConfirmation)) {
80
            Assert::notNull(
81
                $identifier,
82
                'A <saml:Subject> not containing <saml:SubjectConfirmation> should provide' .
83
                ' exactly one of <saml:BaseID>, <saml:NameID> or <saml:EncryptedID>',
84
                TooManyElementsException::class,
85
            );
86
        }
87
88
        return new static(
89
            $identifier,
90
            $subjectConfirmation,
91
        );
92
    }
93
94
95
    /**
96
     * Convert this element to XML.
97
     *
98
     * @param  \DOMElement|null $parent The parent element we should append this element to.
99
     * @return \DOMElement This element, as XML.
100
     */
101
    public function toXML(?DOMElement $parent = null): DOMElement
102
    {
103
        $e = $this->instantiateParentElement($parent);
104
105
        /** @var \SimpleSAML\XML\SerializableElementInterface|null $identifier */
106
        $identifier = $this->getIdentifier();
107
        $identifier?->toXML($e);
108
109
        foreach ($this->getSubjectConfirmation() as $sc) {
110
            $sc->toXML($e);
111
        }
112
113
        return $e;
114
    }
115
}
116