AuthenticationFailure   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 92
rs 10
wmc 7

5 Methods

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