Completed
Push — master ( 2cde75...6c52d6 )
by Arne
02:04
created

Synchronization::getTime()   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 Storeman;
4
5
use Storeman\Exception\Exception;
6
7
/**
8
 * As the name suggests this class represents the synchronization for a specific revision.
9
 */
10
class Synchronization
11
{
12
    /**
13
     * @var int
14
     */
15
    protected $revision;
16
17
    /**
18
     * @var \DateTime
19
     */
20
    protected $time;
21
22
    /**
23
     * @var string
24
     */
25
    protected $identity;
26
27
    /**
28
     * @var string
29
     */
30
    protected $blobId;
31
32
    public function __construct(int $revision, string $blobId, \DateTime $time, string $identity = null)
33
    {
34
        $this->revision = $revision;
35
        $this->blobId = $blobId;
36
        $this->time = $time;
37
        $this->identity = $identity;
38
    }
39
40
    /**
41
     * @return int
42
     */
43
    public function getRevision(): int
44
    {
45
        return $this->revision;
46
    }
47
48
    /**
49
     * @return \DateTime
50
     */
51
    public function getTime(): \DateTime
52
    {
53
        return $this->time;
54
    }
55
56
    /**
57
     * @return string
58
     */
59
    public function getIdentity(): ?string
60
    {
61
        return $this->identity;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getBlobId(): string
68
    {
69
        return $this->blobId;
70
    }
71
72
    public function getRecord(): array
73
    {
74
        return [
75
            $this->revision,
76
            $this->blobId,
77
            $this->time->getTimestamp(),
78
            $this->identity
79
        ];
80
    }
81
82
    public static function fromRecord(array $row): Synchronization
83
    {
84
        $revision = $row[0];
85
        $blobId = $row[1];
86
        $time = \DateTime::createFromFormat('U', $row[2]);
87
        $identity = $row[3] ?: null;
88
89
        if (!($time instanceof \DateTime))
90
        {
91
            throw new Exception();
92
        }
93
94
        return new static($revision, $blobId, $time, $identity);
95
    }
96
}
97