SynchronizationList::count()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Storeman;
4
5
class SynchronizationList implements \Countable, \IteratorAggregate
6
{
7
    /**
8
     * Chronologically ordered list of synchronizations (earliest to latest).
9
     *
10
     * @var Synchronization[]
11
     */
12
    protected $synchronizations = [];
13
14
    /**
15
     * @param Synchronization[] $synchronizations
16
     */
17
    public function __construct(array $synchronizations = [])
18
    {
19
        foreach ($synchronizations as $synchronization)
20
        {
21
            $this->addSynchronization($synchronization);
22
        }
23
    }
24
25
    /**
26
     * @param Synchronization $synchronization
27
     *
28
     * @return SynchronizationList
29
     */
30
    public function addSynchronization(Synchronization $synchronization): SynchronizationList
31
    {
32
        if (isset($this->synchronizations[$synchronization->getRevision()]))
33
        {
34
            throw new Exception();
35
        }
36
37
        $this->synchronizations[$synchronization->getRevision()] = $synchronization;
38
39
        return $this;
40
    }
41
42
    /**
43
     * Returns the synchronization with the highest revision.
44
     *
45
     * @return Synchronization
46
     */
47
    public function getLastSynchronization(): ?Synchronization
48
    {
49
        if (!empty($this->synchronizations))
50
        {
51
            return $this->synchronizations[max(array_keys($this->synchronizations))];
52
        }
53
54
        return null;
55
    }
56
57
    /**
58
     * @param int $revision
59
     *
60
     * @return Synchronization
61
     */
62
    public function getSynchronization(int $revision): ?Synchronization
63
    {
64
        return isset($this->synchronizations[$revision]) ? $this->synchronizations[$revision] : null;
65
    }
66
67
    /**
68
     * Returns the synchronization that was the current one for the given datetime.
69
     *
70
     * @param \DateTime $time
71
     *
72
     * @return Synchronization
73
     */
74
    public function getSynchronizationByTime(\DateTime $time): ?Synchronization
75
    {
76
        $current = null;
77
78
        foreach ($this->synchronizations as $synchronization)
79
        {
80
            if ($synchronization->getTime() > $time)
81
            {
82
                break;
83
            }
84
85
            $current = $synchronization;
86
        }
87
88
        return $current;
89
    }
90
91
    /**
92
     * Returns new synchronization list containing only those synchronizations that were performed by the given identity.
93
     *
94
     * @param string $identity
95
     * @return SynchronizationList
96
     */
97
    public function getSynchronizationsByIdentity(string $identity): SynchronizationList
98
    {
99
        return new static(array_filter(iterator_to_array($this->getIterator()), function(Synchronization $synchronization) use ($identity) {
100
101
            return $synchronization->getIdentity() === $identity;
102
        }));
103
    }
104
105
    /**
106
     * Returns set of revisions contained in this list.
107
     *
108
     * @return int[]
109
     */
110
    public function getRevisions(): array
111
    {
112
        return array_values(array_map(function(Synchronization $synchronization) {
113
114
            return $synchronization->getRevision();
115
116
        }, iterator_to_array($this->getIterator())));
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     */
122
    public function count(): int
123
    {
124
        return count($this->synchronizations);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function getIterator(): \Traversable
131
    {
132
        return new \ArrayIterator($this->synchronizations);
133
    }
134
}
135