Passed
Pull Request — master (#24)
by Tim
01:55
created

ServiceResponse::toXML()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 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;
10
use SimpleSAML\XML\SchemaValidatableElementTrait;
11
use SimpleSAML\XMLSchema\Exception\InvalidDOMElementException;
12
use SimpleSAML\XMLSchema\Exception\MissingElementException;
13
use SimpleSAML\XMLSchema\Exception\TooManyElementsException;
14
15
use function array_merge;
16
17
/**
18
 * Class for CAS serviceResponse
19
 *
20
 * @package simplesamlphp/xml-cas
21
 */
22
final class ServiceResponse extends AbstractServiceResponse implements SchemaValidatableElementInterface
23
{
24
    use SchemaValidatableElementTrait;
25
26
27
    /**
28
     * Convert XML into a cas:serviceResponse-element
29
     *
30
     * @param \DOMElement $xml The XML element we should load
31
     * @return static
32
     *
33
     * @throws \SimpleSAML\XMLSchema\Exception\InvalidDOMElementException
34
     *   if the qualified name of the supplied element is wrong
35
     * @throws \SimpleSAML\XMLSchema\Exception\MissingAttributeException
36
     *   if the supplied element is missing one of the mandatory attributes
37
     */
38
    public static function fromXML(DOMElement $xml): static
39
    {
40
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
41
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
42
43
        $authenticationSuccess = AuthenticationSuccess::getChildrenOfClass($xml);
44
        $authenticationFailure = AuthenticationFailure::getChildrenOfClass($xml);
45
        $proxySuccess = ProxySuccess::getChildrenOfClass($xml);
46
        $proxyFailure = ProxyFailure::getChildrenOfClass($xml);
47
48
        $response = array_merge($authenticationSuccess, $authenticationFailure, $proxySuccess, $proxyFailure);
49
        Assert::notEmpty(
50
            $response,
51
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
52
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
53
            MissingElementException::class,
54
        );
55
        Assert::count(
56
            $response,
57
            1,
58
            'The <cas:serviceResponse> must contain exactly one of <cas:authenticationSuccess>,'
59
            . ' <cas:authenticationFailure>, <cas:proxySuccess> or <cas:proxyFailure>.',
60
            TooManyElementsException::class,
61
        );
62
63
        return new static($response[0]);
64
    }
65
}
66