AbstractLifetimeType::getExpires()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
c 1
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\WSSecurity\XML\wst_200502;
6
7
use DOMElement;
8
use SimpleSAML\WSSecurity\Assert\Assert;
9
use SimpleSAML\WSSecurity\XML\wsu\Created;
10
use SimpleSAML\WSSecurity\XML\wsu\Expires;
11
use SimpleSAML\XML\Exception\InvalidDOMElementException;
12
use SimpleSAML\XML\Exception\TooManyElementsException;
13
14
/**
15
 * Class defining the LifetimeType element
16
 *
17
 * @package simplesamlphp/ws-security
18
 */
19
abstract class AbstractLifetimeType extends AbstractWstElement
20
{
21
    /**
22
     * AbstractLifetimeType constructor
23
     *
24
     * @param \SimpleSAML\WSSecurity\XML\wsu\Created|null $created
25
     * @param \SimpleSAML\WSSecurity\XML\wsu\Expires|null $expires
26
     */
27
    final public function __construct(
28
        protected ?Created $created = null,
29
        protected ?Expires $expires = null,
30
    ) {
31
    }
32
33
34
    /**
35
     * @return \SimpleSAML\WSSecurity\XML\wsu\Created|null
36
     */
37
    public function getCreated(): ?Created
38
    {
39
        return $this->created;
40
    }
41
42
43
    /**
44
     * @return \SimpleSAML\WSSecurity\XML\wsu\Expires|null
45
     */
46
    public function getExpires(): ?Expires
47
    {
48
        return $this->expires;
49
    }
50
51
52
    /**
53
     * Test if an object, at the state it's in, would produce an empty XML-element
54
     *
55
     * @return bool
56
     */
57
    public function isEmptyElement(): bool
58
    {
59
        return empty($this->getCreated())
60
            && empty($this->getExpires());
61
    }
62
63
64
    /**
65
     * Create an instance of this object from its XML representation.
66
     *
67
     * @param \DOMElement $xml
68
     * @return static
69
     *
70
     * @throws \SimpleSAML\XML\Exception\InvalidDOMElementException
71
     *   if the qualified name of the supplied element is wrong
72
     */
73
    public static function fromXML(DOMElement $xml): static
74
    {
75
        Assert::same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
0 ignored issues
show
Bug introduced by
The method same() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

75
        Assert::/** @scrutinizer ignore-call */ 
76
                same($xml->localName, static::getLocalName(), InvalidDOMElementException::class);
Loading history...
76
        Assert::same($xml->namespaceURI, static::NS, InvalidDOMElementException::class);
77
78
        $created = Created::getChildrenOfClass($xml);
79
        Assert::maxCount($created, 1, TooManyElementsException::class);
0 ignored issues
show
Bug introduced by
The method maxCount() does not exist on SimpleSAML\WSSecurity\Assert\Assert. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

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

79
        Assert::/** @scrutinizer ignore-call */ 
80
                maxCount($created, 1, TooManyElementsException::class);
Loading history...
80
81
        $expires = Expires::getChildrenOfClass($xml);
82
        Assert::maxCount($expires, 1, TooManyElementsException::class);
83
84
85
        return new static(
86
            array_pop($created),
87
            array_pop($expires),
88
        );
89
    }
90
91
92
    /**
93
     * Add this LifetimeType to an XML element.
94
     *
95
     * @param \DOMElement|null $parent The element we should append this element to.
96
     * @return \DOMElement
97
     */
98
    public function toXML(?DOMElement $parent = null): DOMElement
99
    {
100
        $e = parent::instantiateParentElement($parent);
101
102
        $this->getCreated()?->toXML($e);
103
        $this->getExpires()?->toXML($e);
104
105
        return $e;
106
    }
107
}
108