Completed
Push — master ( b2e536...a4e6dd )
by Arne
02:05
created

Synchronization::fromRecord()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 4
nop 1
1
<?php
2
3
namespace Storeman;
4
5
/**
6
 * As the name suggests this class represents the synchronization for a specific revision.
7
 */
8
class Synchronization
9
{
10
    /**
11
     * @var int
12
     */
13
    protected $revision;
14
15
    /**
16
     * @var \DateTime
17
     */
18
    protected $time;
19
20
    /**
21
     * @var string
22
     */
23
    protected $identity;
24
25
    /**
26
     * @var Index
27
     */
28
    protected $index;
29
30
    public function __construct(int $revision, \DateTime $time, string $identity, Index $index = null)
31
    {
32
        $this->revision = $revision;
33
        $this->time = $time;
34
        $this->identity = $identity;
35
        $this->index = $index;
36
    }
37
38
    public function getRevision(): int
39
    {
40
        return $this->revision;
41
    }
42
43
    public function getTime(): \DateTime
44
    {
45
        return $this->time;
46
    }
47
48
    public function getIdentity(): string
49
    {
50
        return $this->identity;
51
    }
52
53
    public function getIndex(): ?Index
54
    {
55
        return $this->index;
56
    }
57
58
    public function setIndex(Index $index): Synchronization
59
    {
60
        assert($this->index === null);
61
62
        $this->index = $index;
63
64
        return $this;
65
    }
66
67
    public function toScalarArray(): array
68
    {
69
        return [
70
            $this->revision,
71
            $this->time->getTimestamp(),
72
            $this->identity
73
        ];
74
    }
75
76
    public static function fromScalarArray(array $array, Index $index = null): Synchronization
77
    {
78
        $instance = new static(
79
            $array[0],
80
            \DateTime::createFromFormat('U', $array[1]),
0 ignored issues
show
Security Bug introduced by
It seems like \DateTime::createFromFormat('U', $array[1]) targeting DateTime::createFromFormat() can also be of type false; however, Storeman\Synchronization::__construct() does only seem to accept object<DateTime>, did you maybe forget to handle an error condition?
Loading history...
81
            $array[2],
82
            $index
83
        );
84
85
        return $instance;
86
    }
87
}
88