Passed
Push — master ( 18ff88...32a943 )
by Michael
39s
created

Priority::merge()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 30
ccs 15
cts 15
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 13
nc 4
nop 2
crap 5
1
<?php
2
3
namespace micmania1\config\MergeStrategy;
4
5
use micmania1\config\ConfigCollectionInterface;
6
use micmania1\config\ConfigCollection;
7
8
class Priority
9
{
10 16
    public function merge(ConfigCollectionInterface $mine, ConfigCollectionInterface $theirs)
11
    {
12 16
        foreach ($mine->all() as $key => $item) {
13
            // If the item doesn't exist in theirs, we can just set it and continue.
14 16
            if(!$theirs->exists($key)) {
15 11
                $theirs->set($key, $item);
16 11
                continue;
17
            }
18
19
            /** @var ConfigItemInterface **/
20 11
            $theirsItem = $theirs->get($key);
21
22
            // Get the two values for comparison
23 11
            $lessImportantValue = $theirsItem->getValue();
24 11
            $importantValue = $item->getValue();
25
26
            // Set the default value
27 11
            $value = $importantValue;
28
29
            // If its an array and the key already esists, we can use array_merge
30 11
            if (is_array($importantValue) && is_array($lessImportantValue)) {
31 2
                $value = array_merge($lessImportantValue, $importantValue);
32 2
            }
33
34
            // The key is not set or the value is to be overwritten
35 11
            $theirsItem->set($value, $item->getMetaData());
36 16
        }
37
38 16
        return $theirs;
39
    }
40
}
41