AuthenticationSuccess   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 7
eloc 30
dl 0
loc 119
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getProxyGrantingTicket() 0 3 1
A toXML() 0 10 1
A fromXML() 0 29 1
A getAttributes() 0 3 1
A getProxies() 0 3 1
A __construct() 0 6 1
A getUser() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\CAS\XML;
6
7
use DOMElement;
8
use SimpleSAML\CAS\Assert\Assert;
9
use SimpleSAML\XMLSchema\Exception\{InvalidDOMElementException, MissingElementException};
10
11
use function array_pop;
12
13
/**
14
 * Class for CAS authenticationSuccess
15
 *
16
 * @package simplesamlphp/cas
17
 */
18
final class AuthenticationSuccess extends AbstractResponse
19
{
20
    /** @var string */
21
    final public const LOCALNAME = 'authenticationSuccess';
22
23
24
    /**
25
     * Initialize a cas:authenticationSuccess element
26
     *
27
     * @param \SimpleSAML\CAS\XML\User $user
28
     * @param \SimpleSAML\CAS\XML\Attributes $attributes
29
     * @param \SimpleSAML\CAS\XML\ProxyGrantingTicket|null $proxyGrantingTicket
30
     * @param \SimpleSAML\CAS\XML\Proxies|null $proxies
31
     */
32
    final public function __construct(
33
        protected User $user,
34
        protected Attributes $attributes,
35
        protected ?ProxyGrantingTicket $proxyGrantingTicket = null,
36
        protected ?Proxies $proxies = null,
37
    ) {
38
    }
39
40
41
    /**
42
     * @return \SimpleSAML\CAS\XML\User
43
     */
44
    public function getUser(): User
45
    {
46
        return $this->user;
47
    }
48
49
50
    /**
51
     * @return \SimpleSAML\CAS\XML\Attributes
52
     */
53
    public function getAttributes(): Attributes
54
    {
55
        return $this->attributes;
56
    }
57
58
59
    /**
60
     * @return \SimpleSAML\CAS\XML\ProxyGrantingTicket
61
     */
62
    public function getProxyGrantingTicket(): ?ProxyGrantingTicket
63
    {
64
        return $this->proxyGrantingTicket;
65
    }
66
67
68
    /**
69
     * @return \SimpleSAML\CAS\XML\Proxies
70
     */
71
    public function getProxies(): ?Proxies
72
    {
73
        return $this->proxies;
74
    }
75
76
77
    /**
78
     * Convert XML into a cas:authenticationSuccess-element
79
     *
80
     * @param \DOMElement $xml The XML element we should load
81
     * @return static
82
     *
83
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
84
     *   if the qualified name of the supplied element is wrong
85
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
86
     *   if the supplied element is missing one of the mandatory attributes
87
     */
88
    public static function fromXML(DOMElement $xml): static
89
    {
90
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
91
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
92
93
        $user = User::getChildrenOfClass($xml);
94
        Assert::count(
95
            $user,
96
            1,
97
            'Exactly one <cas:user> must be specified.',
98
            MissingElementException::class,
99
        );
100
101
        $attributes = Attributes::getChildrenOfClass($xml);
102
        Assert::count(
103
            $attributes,
104
            1,
105
            'Exactly one <cas:attributes> must be specified.',
106
            MissingElementException::class,
107
        );
108
109
        $proxyGrantingTicket = ProxyGrantingTicket::getChildrenOfClass($xml);
110
        $proxies = Proxies::getChildrenOfClass($xml);
111
112
        return new static(
113
            $user[0],
114
            $attributes[0],
115
            array_pop($proxyGrantingTicket),
116
            array_pop($proxies),
117
        );
118
    }
119
120
121
    /**
122
     * Convert this AuthenticationSuccess to XML.
123
     *
124
     * @param \DOMElement|null $parent The element we should append this AuthenticationSuccess to.
125
     * @return \DOMElement
126
     */
127
    public function toXML(?DOMElement $parent = null): DOMElement
128
    {
129
        $e = $this->instantiateParentElement($parent);
130
131
        $this->getUser()->toXML($e);
132
        $this->getAttributes()->toXML($e);
133
        $this->getProxyGrantingTicket()?->toXML($e);
134
        $this->getProxies()?->toXML($e);
135
136
        return $e;
137
    }
138
}
139