Passed
Push — master ( 768e90...be6a83 )
by Tim
01:37
created

AuthenticationFailure::setCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\MissingAttributeException;
11
use SimpleSAML\XML\StringElementTrait;
12
13
/**
14
 * Class for CAS authenticationFailure
15
 *
16
 * @package simplesamlphp/cas
17
 */
18
final class AuthenticationFailure extends AbstractResponse
19
{
20
    use StringElementTrait;
21
22
    /** @var string */
23
    public const LOCALNAME = 'authenticationFailure';
24
25
26
    /**
27
     * Create a new instance of AuthenticationFailure
28
     *
29
     * @param string $content
30
     * @param string $code
31
     */
32
    final public function __construct(
33
        string $content,
34
        protected string $code,
35
    ) {
36
        Assert::notEmpty($code, 'The code in AuthenticationFailure must not be a empty.');
37
        $this->setContent($content);
38
    }
39
40
41
    /**
42
     * Collect the value of the code-property
43
     *
44
     * @return string
45
     */
46
    public function getCode(): string
47
    {
48
        return $this->code;
49
    }
50
51
52
    /**
53
     * Validate the content of the element.
54
     *
55
     * @param string $content  The value to go in the XML textContent
56
     * @throws \Exception on failure
57
     * @return void
58
     */
59
    protected function validateContent(string $content): void
60
    {
61
        Assert::notWhitespaceOnly($content);
62
    }
63
64
65
    /**
66
     * Initialize an AuthenticationFailure element.
67
     *
68
     * @param \DOMElement $xml The XML element we should load.
69
     * @return static
70
     *
71
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
72
     *   if the qualified name of the supplied element is wrong
73
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
74
     *   if the supplied element is missing any of the mandatory attributes
75
     */
76
    public static function fromXML(DOMElement $xml): static
77
    {
78
        Assert::same($xml->localName, 'authenticationFailure', InvalidDOMElementException::class);
79
        Assert::same($xml->namespaceURI, ProxyFailure::NS, InvalidDOMElementException::class);
80
        Assert::true(
81
            $xml->hasAttribute('code'),
82
            'Missing code from ' . static::getLocalName(),
83
            MissingAttributeException::class,
84
        );
85
86
        /** @psalm-var string $code */
87
        $code = self::getAttribute($xml, 'code');
88
        return new static(trim($xml->textContent), $code);
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type null; however, parameter $code of SimpleSAML\CAS\XML\cas\A...nFailure::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
        return new static(trim($xml->textContent), /** @scrutinizer ignore-type */ $code);
Loading history...
89
    }
90
91
92
    /**
93
     * Convert this AuthenticationFailure to XML.
94
     *
95
     * @param \DOMElement|null $parent The element we should append to.
96
     * @return \DOMElement This AuthenticatioFailure-element.
97
     */
98
    public function toXML(DOMElement $parent = null): DOMElement
99
    {
100
        $e = $this->instantiateParentElement($parent);
101
        $e->textContent = $this->getContent();
102
        $e->setAttribute('code', $this->getCode());
103
104
        return $e;
105
    }
106
}
107