AbstractFreshnessType   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 28
c 1
b 0
f 0
dl 0
loc 100
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A fromXML() 0 10 1
A toXML() 0 14 4
A __construct() 0 7 1
A validateContent() 0 11 1
A getAllowCache() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\fed;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\XML\Exception\InvalidDOMElementException;
10
use SimpleSAML\XML\Exception\SchemaViolationException;
11
use SimpleSAML\XML\ExtendableAttributesTrait;
12
use SimpleSAML\XML\StringElementTrait;
13
use SimpleSAML\XML\XsNamespace as NS;
14
15
use function intval;
16
use function sprintf;
17
use function strval;
18
19
/**
20
 * Class defining the FreshnessType element
21
 *
22
 * @package simplesamlphp/ws-security
23
 */
24
abstract class AbstractFreshnessType extends AbstractFedElement
25
{
26
    use StringElementTrait;
27
    use ExtendableAttributesTrait;
28
29
    /** The namespace-attribute for the xs:anyAttribute element */
30
    public const XS_ANY_ATTR_NAMESPACE = NS::OTHER;
31
32
33
    /**
34
     * AbstractFreshnessType constructor
35
     *
36
     * @param int $content
37
     * @param bool|null $AllowCache
38
     * @param array<\SimpleSAML\XML\Attribute> $namespacedAttributes
39
     */
40
    final public function __construct(
41
        int $content,
42
        protected ?bool $AllowCache = null,
43
        array $namespacedAttributes = [],
44
    ) {
45
        $this->setContent(strval($content));
46
        $this->setAttributesNS($namespacedAttributes);
47
    }
48
49
50
    /**
51
     * Validate the content of the element.
52
     *
53
     * @param string $content  The value to go in the XML textContent
54
     * @throws \Exception on failure
55
     * @return void
56
     */
57
    protected function validateContent(string $content): void
58
    {
59
        Assert::natural(
60
            intval($content),
61
            sprintf(
62
                'The value \'%s\' of an %s:%s element must an unsigned integer.',
63
                $content,
64
                static::NS_PREFIX,
65
                static::getLocalName(),
66
            ),
67
            SchemaViolationException::class,
68
        );
69
    }
70
71
72
    /**
73
     * @return bool|null
74
     */
75
    public function getAllowCache(): ?bool
76
    {
77
        return $this->AllowCache;
78
    }
79
80
81
    /**
82
     * Create an instance of this object from its XML representation.
83
     *
84
     * @param \DOMElement $xml
85
     * @return static
86
     *
87
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
88
     *   if the qualified name of the supplied element is wrong
89
     */
90
    public static function fromXML(DOMElement $xml): static
91
    {
92
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
93
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
94
        Assert::integerish($xml->textContent, SchemaViolationException::class);
95
96
        return new static(
97
            intval($xml->textContent),
98
            self::getOptionalBooleanAttribute($xml, 'AllowCache', null),
99
            self::getAttributesNSFromXML($xml),
100
        );
101
    }
102
103
104
    /**
105
     * Add this IssuerNameType to an XML element.
106
     *
107
     * @param \DOMElement|null $parent The element we should append this issuer name to.
108
     * @return \DOMElement
109
     */
110
    public function toXML(?DOMElement $parent = null): DOMElement
111
    {
112
        $e = parent::instantiateParentElement($parent);
113
        $e->textContent = $this->getContent();
114
115
        if ($this->getAllowCache() !== null) {
116
            $e->setAttribute('AllowCache', $this->getAllowCache() ? 'true' : 'false');
117
        }
118
119
        foreach ($this->getAttributesNS() as $attr) {
120
            $attr->toXML($e);
121
        }
122
123
        return $e;
124
    }
125
}
126