Completed
Push — master ( 51fab9...63db47 )
by duan
02:00
created

Node::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Yiranzai\File;
4
5
use DateTime;
6
7
/**
8
 * Class Node
9
 * @package Yiranzai\Dht
10
 */
11
class Node
12
{
13
    /**
14
     * @var string
15
     */
16
    public $data;
17
    /**
18
     * @var Node|null
19
     */
20
    public $next;
21
    /**
22
     * @var string
23
     */
24
    public $key;
25
    /**
26
     * @var DateTime
27
     */
28
    protected $expiredAt;
29
30
    /**
31
     * Node constructor.
32
     * @param string   $key
33
     * @param string   $data
34
     * @param DateTime $expiredAt
35
     */
36 12
    public function __construct(string $key, string $data, DateTime $expiredAt = null)
37
    {
38 12
        $this->key       = $key;
39 12
        $this->data      = $data;
40 12
        $this->expiredAt = $expiredAt;
41 12
    }
42
43
    /**
44
     * @param null|string $default
45
     * @return string|null
46
     * @throws \Exception
47
     */
48 9
    public function getData($default = null): ?string
49
    {
50 9
        if ($this->isExpired()) {
51 3
            return $default;
52
        }
53 6
        return $this->data;
54
    }
55
56
    /**
57
     * @return bool
58
     * @throws \Exception
59
     */
60 9
    public function isExpired(): bool
61
    {
62 9
        if ($this->expiredAt === null) {
63 6
            return false;
64
        }
65 3
        return $this->expiredAt < new DateTime();
66
    }
67
}
68