ServiceResponse::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
use SimpleSAML\XML\SchemaValidatableElementInterface;
13
use SimpleSAML\XML\SchemaValidatableElementTrait;
14
15
use function array_merge;
16
use function array_pop;
17
18
/**
19
 * Class for CAS serviceResponse
20
 *
21
 * @package simplesamlphp/cas
22
 */
23
final class ServiceResponse extends AbstractCasElement implements SchemaValidatableElementInterface
24
{
25
    use SchemaValidatableElementTrait;
0 ignored issues
show
introduced by
The trait SimpleSAML\XML\SchemaValidatableElementTrait requires some properties which are not provided by SimpleSAML\CAS\XML\cas\ServiceResponse: $message, $line
Loading history...
26
27
    /** @var string */
28
    final public const LOCALNAME = 'serviceResponse';
29
30
31
    /**
32
     * Initialize a cas:serviceResponse element
33
     *
34
     * @param \SimpleSAML\CAS\XML\cas\AbstractResponse $response
35
     */
36
    final public function __construct(
37
        protected AbstractResponse $response,
38
    ) {
39
    }
40
41
42
    /**
43
     * @return \SimpleSAML\CAS\XML\cas\AbstractResponse
44
     */
45
    public function getResponse(): AbstractResponse
46
    {
47
        return $this->response;
48
    }
49
50
51
    /**
52
     * Convert XML into a cas:serviceResponse-element
53
     *
54
     * @param \DOMElement $xml The XML element we should load
55
     * @return static
56
     *
57
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
58
     *   if the qualified name of the supplied element is wrong
59
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
60
     *   if the supplied element is missing one of the mandatory attributes
61
     */
62
    public static function fromXML(DOMElement $xml): static
63
    {
64
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
65
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
66
67
        $authenticationSuccess = AuthenticationSuccess::getChildrenOfClass($xml);
68
        $authenticationFailure = AuthenticationFailure::getChildrenOfClass($xml);
69
        $proxySuccess = ProxySuccess::getChildrenOfClass($xml);
70
        $proxyFailure = ProxyFailure::getChildrenOfClass($xml);
71
72
        $response = array_merge($authenticationSuccess, $authenticationFailure, $proxySuccess, $proxyFailure);
73
        Assert::notEmpty(
74
            $response,
75
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
76
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
77
            MissingElementException::class,
78
        );
79
        Assert::count(
80
            $response,
81
            1,
82
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
83
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
84
            TooManyElementsException::class,
85
        );
86
87
        return new static(array_pop($response));
88
    }
89
90
91
    /**
92
     * Convert this ServiceResponse to XML.
93
     *
94
     * @param \DOMElement|null $parent The element we should append this ServiceResponse to.
95
     * @return \DOMElement
96
     */
97
    public function toXML(?DOMElement $parent = null): DOMElement
98
    {
99
        $e = $this->instantiateParentElement($parent);
100
101
        $this->getResponse()->toXML($e);
102
103
        return $e;
104
    }
105
}
106