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

SynchronizationList::getIterator()   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
class SynchronizationList implements \Countable, \IteratorAggregate
8
{
9
    /**
10
     * Chronologically ordered list of synchronizations (earliest to latest).
11
     *
12
     * @var Synchronization[]
13
     */
14
    protected $synchronizations = [];
15
16
    /**
17
     * @param Synchronization[] $synchronizations
18
     */
19
    public function __construct(array $synchronizations = [])
20
    {
21
        foreach ($synchronizations as $synchronization)
22
        {
23
            $this->addSynchronization($synchronization);
24
        }
25
    }
26
27
    /**
28
     * @param Synchronization $synchronization
29
     *
30
     * @return SynchronizationList
31
     * @throws Exception
32
     */
33
    public function addSynchronization(Synchronization $synchronization): SynchronizationList
34
    {
35
        if (isset($this->synchronizations[$synchronization->getRevision()]))
36
        {
37
            throw new Exception();
38
        }
39
40
        $this->synchronizations[$synchronization->getRevision()] = $synchronization;
41
42
        return $this;
43
    }
44
45
    /**
46
     * Returns the synchronization with the highest revision.
47
     *
48
     * @return Synchronization
49
     */
50
    public function getLastSynchronization(): ?Synchronization
51
    {
52
        if ($this->synchronizations)
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->synchronizations of type Storeman\Synchronization[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
        {
54
            return $this->synchronizations[max(array_keys($this->synchronizations))];
55
        }
56
57
        return null;
58
    }
59
60
    /**
61
     * @param int $revision
62
     *
63
     * @return Synchronization
64
     */
65
    public function getSynchronizationByRevision(int $revision): ?Synchronization
66
    {
67
        return isset($this->synchronizations[$revision]) ? $this->synchronizations[$revision] : null;
68
    }
69
70
    /**
71
     * Returns the synchronization that was the current one for the given datetime.
72
     *
73
     * @param \DateTime $time
74
     *
75
     * @return Synchronization
76
     */
77
    public function getSynchronizationByTime(\DateTime $time): ?Synchronization
78
    {
79
        $current = null;
80
81
        foreach ($this->synchronizations as $synchronization)
82
        {
83
            if ($synchronization->getTime() > $time)
84
            {
85
                break;
86
            }
87
88
            $current = $synchronization;
89
        }
90
91
        return $current;
92
    }
93
94
    /**
95
     * Returns new synchronization list containing only those synchronizations that were performed by the given identity.
96
     *
97
     * @param string $identity
98
     * @return SynchronizationList
99
     */
100
    public function getSynchronizationsByIdentity(string $identity): SynchronizationList
101
    {
102
        return new static(array_filter(iterator_to_array($this->getIterator()), function(Synchronization $synchronization) use ($identity) {
103
104
            return $synchronization->getIdentity() === $identity;
105
        }));
106
    }
107
108
    /**
109
     * Returns set of revisions contained in this list.
110
     *
111
     * @return int[]
112
     */
113
    public function getRevisions(): array
114
    {
115
        return array_values(array_map(function(Synchronization $synchronization) {
116
117
            return $synchronization->getRevision();
118
119
        }, iterator_to_array($this->getIterator())));
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function count(): int
126
    {
127
        return count($this->synchronizations);
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    public function getIterator(): \Traversable
134
    {
135
        return new \ArrayIterator($this->synchronizations);
136
    }
137
}
138