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

ProxyFailure::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 proxyFailure
15
 *
16
 * @package simplesamlphp/cas
17
 */
18
final class ProxyFailure extends AbstractResponse
19
{
20
    use StringElementTrait;
21
22
    /** @var string */
23
    public const LOCALNAME = 'proxyFailure';
24
25
26
    /**
27
     * Create a new instance of ProxyFailure
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 ProxyFailure must not be a empty.');
37
        $this->setContent($content);
38
    }
39
40
    /**
41
     * Collect the value of the code-property
42
     *
43
     * @return string
44
     */
45
    public function getCode(): string
46
    {
47
        return $this->code;
48
    }
49
50
51
    /**
52
     * Validate the content of the element.
53
     *
54
     * @param string $content  The value to go in the XML textContent
55
     * @throws \Exception on failure
56
     * @return void
57
     */
58
    protected function validateContent(string $content): void
59
    {
60
        Assert::notWhitespaceOnly($content);
61
    }
62
63
64
    /**
65
     * Initialize an ProxyFailure element.
66
     *
67
     * @param \DOMElement $xml The XML element we should load.
68
     * @return static
69
     *
70
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     * @throws \SimpleSAML\XML\Exception\MissingAttributeException
73
     *   if the supplied element is missing any of the mandatory attributes
74
     */
75
    public static function fromXML(DOMElement $xml): static
76
    {
77
        Assert::same($xml->localName, 'proxyFailure', InvalidDOMElementException::class);
78
        Assert::same($xml->namespaceURI, ProxyFailure::NS, InvalidDOMElementException::class);
79
        Assert::true(
80
            $xml->hasAttribute('code'),
81
            'Missing code from ' . static::getLocalName(),
82
            MissingAttributeException::class,
83
        );
84
85
        /** @psalm-var string $code */
86
        $code = self::getAttribute($xml, 'code');
87
        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\ProxyFailure::__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

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