Completed
Push — master ( 2178d1...145b3d )
by Raffael
06:24 queued 03:25
created

Diff   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 27
lcom 0
cbo 1
dl 0
loc 56
ccs 0
cts 31
cp 0
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D calculate() 0 50 27
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * tubee.io
7
 *
8
 * @copyright   Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com)
9
 * @license     GPL-3.0 https://opensource.org/licenses/GPL-3.0
10
 */
11
12
namespace Tubee\AttributeMap;
13
14
use Tubee\Helper;
15
16
class Diff
17
{
18
    /**
19
     * Create diff.
20
     */
21
    public function calculate(array $map, array $object, array $endpoint_object): array
22
    {
23
        $diff = [];
24
        foreach ($map as $value) {
25
            $attr = $value['name'];
26
            $exists = isset($endpoint_object[$attr]);
27
28
            if ($value['ensure'] === AttributeMapInterface::ENSURE_EXISTS && ($exists === true || !isset($object[$attr]))) {
29
                continue;
30
            }
31
            if (($value['ensure'] === AttributeMapInterface::ENSURE_LAST || $value['ensure'] === AttributeMapInterface::ENSURE_EXISTS) && isset($object[$attr])) {
32
                if ($exists && is_array($object[$attr]) && is_array($endpoint_object[$attr]) && Helper::arrayEqual($endpoint_object[$attr], $object[$attr])) {
33
                    continue;
34
                }
35
                if ($exists && $object[$attr] === $endpoint_object[$attr]) {
36
                    continue;
37
                }
38
39
                $diff[$attr] = [
40
                    'action' => AttributeMapInterface::ACTION_REPLACE,
41
                    'value' => $object[$attr],
42
                ];
43
            } elseif ($value['ensure'] === AttributeMapInterface::ENSURE_ABSENT && isset($endpoint_object[$attr]) || isset($endpoint_object[$attr]) && !isset($object[$attr]) && $value['ensure'] !== AttributeMapInterface::ENSURE_MERGE) {
44
                $diff[$attr] = [
45
                    'action' => AttributeMapInterface::ACTION_REMOVE,
46
                ];
47
            } elseif ($value['ensure'] === AttributeMapInterface::ENSURE_MERGE && isset($object[$attr])) {
48
                $new_values = [];
49
50
                foreach ($object[$attr] as $val) {
51
                    if (!$exists) {
52
                        $new_values[] = $val;
53
                    } elseif (is_array($endpoint_object[$attr]) && in_array($val, $endpoint_object[$attr]) || $val === $endpoint_object[$attr]) {
54
                        continue;
55
                    } else {
56
                        $new_values[] = $val;
57
                    }
58
                }
59
60
                if (!empty($new_values)) {
61
                    $diff[$attr] = [
62
                        'action' => AttributeMapInterface::ACTION_ADD,
63
                        'value' => $new_values,
64
                    ];
65
                }
66
            }
67
        }
68
69
        return $diff;
70
    }
71
}
72