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

Diff::calculate()   D

Complexity

Conditions 27
Paths 15

Size

Total Lines 50

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 756

Importance

Changes 0
Metric Value
dl 0
loc 50
ccs 0
cts 31
cp 0
rs 4.1666
c 0
b 0
f 0
cc 27
nc 15
nop 3
crap 756

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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