ProxyFailure::getCode()   A
last analyzed

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 0
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\CAS\Error;
10
use SimpleSAML\XML\Exception\InvalidDOMElementException;
11
use SimpleSAML\XML\Exception\MissingAttributeException;
12
use SimpleSAML\XML\StringElementTrait;
13
use ValueError;
14
15
use function is_string;
16
use function trim;
17
18
/**
19
 * Class for CAS proxyFailure
20
 *
21
 * @package simplesamlphp/cas
22
 */
23
final class ProxyFailure extends AbstractResponse
24
{
25
    use StringElementTrait;
26
27
    /** @var string */
28
    final public const LOCALNAME = 'proxyFailure';
29
30
31
    /**
32
     * Create a new instance of ProxyFailure
33
     *
34
     * @param string $content
35
     * @param \SimpleSAML\CAS\Error|string $code
36
     */
37
    final public function __construct(
38
        string $content,
39
        protected Error|string $code,
40
    ) {
41
        $this->setContent($content);
42
    }
43
44
    /**
45
     * Collect the value of the code-property
46
     *
47
     * @return \SimpleSAML\CAS\Error|string
48
     */
49
    public function getCode(): Error|string
50
    {
51
        return $this->code;
52
    }
53
54
55
    /**
56
     * Validate the content of the element.
57
     *
58
     * @param string $content  The value to go in the XML textContent
59
     * @throws \Exception on failure
60
     * @return void
61
     */
62
    protected function validateContent(string $content): void
63
    {
64
        Assert::notWhitespaceOnly($content);
65
    }
66
67
68
    /**
69
     * Initialize an ProxyFailure element.
70
     *
71
     * @param \DOMElement $xml The XML element we should load.
72
     * @return static
73
     *
74
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
75
     *   if the qualified name of the supplied element is wrong
76
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
77
     *   if the supplied element is missing any of the mandatory attributes
78
     */
79
    public static function fromXML(DOMElement $xml): static
80
    {
81
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
82
        Assert::same($xml->namespaceURI, static::getNamespaceURI(), InvalidDOMElementException::class);
83
        Assert::true(
84
            $xml->hasAttribute('code'),
85
            'Missing code from ' . static::getLocalName(),
86
            MissingAttributeException::class,
87
        );
88
89
        try {
90
            $code = Error::from(self::getAttribute($xml, 'code'));
91
        } catch (ValueError) {
92
            $code = self::getAttribute($xml, 'code');
93
        }
94
95
        return new static(trim($xml->textContent), $code);
96
    }
97
98
99
    /**
100
     * Convert this ProxyFailure to XML.
101
     *
102
     * @param \DOMElement|null $parent The element we should append to.
103
     * @return \DOMElement This ProxyFailure-element.
104
     */
105
    public function toXML(?DOMElement $parent = null): DOMElement
106
    {
107
        $e = $this->instantiateParentElement($parent);
108
        $e->textContent = $this->getContent();
109
110
        $code = $this->getCode();
111
        $e->setAttribute('code', is_string($code) ? $code : $code->value);
112
113
        return $e;
114
    }
115
}
116