Completed
Push — master ( 9913ec...45b4e9 )
by Arne
01:45
created

Lock::getIdentity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Archivr\LockAdapter;
4
5
class Lock
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $name;
11
12
    /**
13
     * @var string
14
     */
15
    protected $identity;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    protected $acquired;
21
22
    public function __construct(string $name, string $identity = null, \DateTime $acquired = null)
23
    {
24
        $this->name = $name;
25
        $this->identity = $identity;
26
        $this->acquired = $acquired ?: new \DateTime();
27
    }
28
29
    /**
30
     * @return string
31
     */
32
    public function getName(): string
33
    {
34
        return $this->name;
35
    }
36
37
    /**
38
     * @return string
39
     */
40
    public function getIdentity(): string
41
    {
42
        return $this->identity;
43
    }
44
45
    /**
46
     * @return \DateTime
47
     */
48
    public function getAcquired(): \DateTime
49
    {
50
        return $this->acquired;
51
    }
52
53
    public function getPayload()
54
    {
55
        return json_encode([
56
            'name' => $this->name,
57
            'identity' => $this->identity,
58
            'acquired' => $this->acquired->getTimestamp()
59
        ]);
60
    }
61
62
    public static function fromPayload(string $payload)
63
    {
64
        $info = json_decode($payload, true);
65
66
        return new static(
67
            $info['name'],
68
            $info['identity'],
69
            new \DateTime("@{$info['acquired']}")
70
        );
71
    }
72
}
73