Completed
Push — master ( aa9feb...52b2fb )
by Arne
02:41
created

StandardIndexMerger::setConflictHandler()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Archivr\IndexMerger;
4
5
use Archivr\ConflictHandler\ConflictHandlerInterface;
6
use Archivr\ConflictHandler\PanickingConflictHandler;
7
use Archivr\Index;
8
use Archivr\IndexObject;
9
10
class StandardIndexMerger implements IndexMergerInterface
11
{
12
    /**
13
     * @var ConflictHandlerInterface
14
     */
15
    protected $conflictHandler;
16
17
    public function setConflictHandler(ConflictHandlerInterface $conflictHandler = null): IndexMergerInterface
18
    {
19
        $this->conflictHandler = $conflictHandler;
20
21
        return $this;
22
    }
23
24
    public function getConflictHandler(): ConflictHandlerInterface
25
    {
26
        if ($this->conflictHandler === null)
27
        {
28
            $this->setConflictHandler(new PanickingConflictHandler());
29
        }
30
31
        return $this->conflictHandler;
32
    }
33
34
    public function merge(Index $localIndex, Index $lastLocalIndex = null, Index $remoteIndex = null): Index
35
    {
36
        $mergedIndex = new Index();
37
        $conflictHandler = $this->getConflictHandler();
38
39
        // build new index from local index
40
        foreach ($localIndex as $localObject)
41
        {
42
            /** @var IndexObject $localObject */
43
44
            $remoteObject = $remoteIndex ? $remoteIndex->getObjectByPath($localObject->getRelativePath()) : null;
45
46
            $localObjectModified = $lastLocalIndex ? ($localObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : true;
47
48
            if ($remoteObject === null)
49
            {
50
                if ($localObjectModified)
51
                {
52
                    $mergedIndex->addObject($localObject);
53
                }
54
                elseif ($remoteIndex === null)
55
                {
56
                    $mergedIndex->addObject($localObject);
57
                }
58
            }
59
            else
60
            {
61
                $remoteObjectModified = $lastLocalIndex ? ($remoteObject->getMtime() > $lastLocalIndex->getCreated()->getTimestamp()) : false;
62
63
                if (!$localObjectModified)
64
                {
65
                    $mergedIndex->addObject($remoteObject);
66
                }
67
68
                elseif (!$remoteObjectModified)
69
                {
70
                    $mergedIndex->addObject($localObject);
71
                }
72
73
                else
74
                {
75
                    $conflictHandler->handleConflict($remoteObject, $localObject, $lastLocalIndex ? $lastLocalIndex->getObjectByPath($localObject->getRelativePath()) : null);
76
                }
77
            }
78
        }
79
80
        if ($remoteIndex !== null)
81
        {
82
            // add remote index content
83
            foreach ($remoteIndex as $remoteObject)
84
            {
85
                /** @var IndexObject $remoteObject */
86
87
                $localObject = $localIndex->getObjectByPath($remoteObject->getRelativePath());
88
                $lastLocalObject = $lastLocalIndex ? $lastLocalIndex->getObjectByPath($remoteObject->getRelativePath()) : null;
89
90
                if ($localObject === null)
91
                {
92
                    if ($lastLocalObject === null)
93
                    {
94
                        $mergedIndex->addObject($remoteObject);
95
                    }
96
                }
97
            }
98
        }
99
100
        return $mergedIndex;
101
    }
102
}
103