Passed
Push — master ( 768e90...be6a83 )
by Tim
01:37
created

ServiceResponse::setResponse()   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 SimpleSAML\CAS\XML\cas;
6
7
use DOMElement;
8
use SimpleSAML\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingElementException;
11
use SimpleSAML\XML\Exception\TooManyElementsException;
12
13
/**
14
 * Class for CAS serviceResponse
15
 *
16
 * @package simplesamlphp/cas
17
 */
18
final class ServiceResponse extends AbstractCasElement
19
{
20
    /** @var string */
21
    public const LOCALNAME = 'serviceResponse';
22
23
24
    /**
25
     * Initialize a cas:serviceResponse element
26
     *
27
     * @param \SimpleSAML\CAS\XML\cas\AbstractResponse $response
28
     */
29
    final public function __construct(
30
        protected AbstractResponse $response,
31
    ) {
32
    }
33
34
35
    /**
36
     * @return \SimpleSAML\CAS\XML\cas\AbstractResponse
37
     */
38
    public function getResponse(): AbstractResponse
39
    {
40
        return $this->response;
41
    }
42
43
44
    /**
45
     * Convert XML into a cas:serviceResponse-element
46
     *
47
     * @param \DOMElement $xml The XML element we should load
48
     * @return static
49
     *
50
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
51
     *   if the qualified name of the supplied element is wrong
52
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
53
     *   if the supplied element is missing one of the mandatory attributes
54
     */
55
    public static function fromXML(DOMElement $xml): static
56
    {
57
        Assert::same($xml->localName, 'serviceResponse', InvalidDOMElementException::class);
58
        Assert::same($xml->namespaceURI, ServiceResponse::NS, InvalidDOMElementException::class);
59
60
        $authenticationSuccess = AuthenticationSuccess::getChildrenOfClass($xml);
61
        $authenticationFailure = AuthenticationFailure::getChildrenOfClass($xml);
62
        $proxySuccess = ProxySuccess::getChildrenOfClass($xml);
63
        $proxyFailure = ProxyFailure::getChildrenOfClass($xml);
64
65
        $response = array_merge($authenticationSuccess, $authenticationFailure, $proxySuccess, $proxyFailure);
66
        Assert::notEmpty(
67
            $response,
68
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
69
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
70
            MissingElementException::class,
71
        );
72
        Assert::count(
73
            $response,
74
            1,
75
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
76
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
77
            TooManyElementsException::class,
78
        );
79
80
        return new static(array_pop($response));
81
    }
82
83
84
    /**
85
     * Convert this ServiceResponse to XML.
86
     *
87
     * @param \DOMElement|null $parent The element we should append this ServiceResponse to.
88
     * @return \DOMElement
89
     */
90
    public function toXML(DOMElement $parent = null): DOMElement
91
    {
92
        $e = $this->instantiateParentElement($parent);
93
94
        $this->getResponse()->toXML($e);
95
96
        return $e;
97
    }
98
}
99