ServiceResponse   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 24
dl 0
loc 81
c 0
b 0
f 0
rs 10

4 Methods

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