Completed
Push — master ( 65570c...bf6ec7 )
by Arne
03:29 queued 12s
created

IndexDifference   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 1
dl 0
loc 56
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addDifference() 0 6 1
A getDifference() 0 4 2
A hasDifference() 0 4 1
A merge() 0 8 1
A count() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
namespace Storeman\Index\Diff;
4
5
class IndexDifference implements \Countable, \IteratorAggregate
6
{
7
    /**
8
     * @var IndexObjectDifference[]
9
     */
10
    protected $differences = [];
11
12
    public function addDifference(IndexObjectDifference $difference): IndexDifference
13
    {
14
        $this->differences[$difference->getRelativePath()] = $difference;
15
16
        return $this;
17
    }
18
19
    public function getDifference(string $path): ?IndexObjectDifference
20
    {
21
        return array_key_exists($path, $this->differences) ? $this->differences[$path] : null;
22
    }
23
24
    public function hasDifference(string $path): bool
25
    {
26
        return array_key_exists($path, $this->differences);
27
    }
28
29
    /**
30
     * Merges the differences of the given diff into this diff.
31
     * There must be an empty intersection between the path sets of both diffs.
32
     *
33
     * @param IndexDifference $other
34
     * @return IndexDifference
35
     */
36
    public function merge(IndexDifference $other): IndexDifference
37
    {
38
        assert(empty(array_intersect(array_keys($this->differences), array_keys($other->differences))));
39
40
        $this->differences = array_merge($this->differences, $other->differences);
41
42
        return $this;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function count()
49
    {
50
        return count($this->differences);
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function getIterator()
57
    {
58
        return new \ArrayIterator($this->differences);
59
    }
60
}
61