Comparator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 137
rs 10
c 0
b 0
f 0
ccs 40
cts 40
cp 1
wmc 13
lcom 1
cbo 2

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A compare() 0 4 1
B compareChanges() 0 21 5
B getDeleted() 0 24 4
A isSaved() 0 4 1
A isUpdated() 0 4 1
1
<?php namespace Arcanesoft\Settings\Helpers;
2
3
use Arcanedev\Support\Collection;
4
5
/**
6
 * Class     Comparator
7
 *
8
 * @package  Arcanesoft\Settings\Helpers
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class Comparator
12
{
13
    /* ------------------------------------------------------------------------------------------------
14
     |  Properties
15
     | ------------------------------------------------------------------------------------------------
16
     */
17
    /**
18
     * Unsaved data.
19
     *
20
     * @var \Arcanedev\Support\Collection
21
     */
22
    private $unsaved;
23
24
    /**
25
     * Saved data.
26
     *
27
     * @var \Arcanedev\Support\Collection
28
     */
29
    private $saved;
30
31
    /* ------------------------------------------------------------------------------------------------
32
     |  Constructor
33
     | ------------------------------------------------------------------------------------------------
34
     */
35
    /**
36
     * Comparator constructor.
37
     *
38
     * @param  array  $unsaved
39
     * @param  array  $saved
40
     */
41 21
    public function __construct(array $unsaved, array $saved)
42
    {
43 21
        $this->unsaved = Collection::make($unsaved);
44 21
        $this->saved   = Collection::make($saved);
45 21
    }
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Main Functions
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * Compare the changes.
53
     *
54
     * @param  array  $unsaved
55
     * @param  array  $saved
56
     *
57
     * @return array
58
     */
59 21
    public static function compare(array $unsaved, array $saved)
60
    {
61 21
        return (new self($unsaved, $saved))->compareChanges();
62
    }
63
64
    /**
65
     * Compare the changes.
66
     *
67
     * @return array
68
     */
69 21
    private function compareChanges()
70
    {
71 21
        $inserted = $updated = [];
72
73 21
        foreach ($this->unsaved as $domain => $values) {
74 18
            foreach ($values as $key => $value) {
75 18
                if ($this->isSaved($domain, $key)) {
76 6
                    if ($this->isUpdated($domain, $key, $value)) {
77 4
                        $updated[$domain][$key] = $value; // Updated
78 2
                    }
79 4
                }
80
                else {
81 18
                    $inserted[$domain][$key] = $value; // Inserted
82
                }
83 12
            }
84 14
        }
85
86 21
        $deleted = $this->getDeleted();
87
88 21
        return compact('inserted', 'updated', 'deleted');
89
    }
90
91
    /**
92
     * Compare the deleted entries.
93
     *
94
     * @return array
95
     */
96 21
    private function getDeleted()
97
    {
98 21
        if ($this->unsaved->isEmpty()) {
99
            // Delete all saved settings.
100 12
            return $this->saved->map(function (array $settings) {
101 9
                return array_keys($settings);
102 12
            })->toArray();
103
        }
104
105 18
        $deleted = [];
106
107 18
        foreach ($this->unsaved as $domain => $values) {
108 18
            $diff = array_diff(
109 18
                array_keys($this->saved->get($domain, [])),
110 12
                array_keys($values)
111 12
            );
112
113 18
            if ( ! empty($diff)) {
114 8
                $deleted[$domain] = $diff;
115 2
            }
116 12
        }
117
118 18
        return $deleted;
119
    }
120
121
    /**
122
     * Check if the entry is saved.
123
     *
124
     * @param  string  $domain
125
     * @param  string  $key
126
     *
127
     * @return bool
128
     */
129 18
    private function isSaved($domain, $key)
130
    {
131 18
        return Arr::has($this->saved->get($domain, []), $key);
132
    }
133
134
    /**
135
     * Check if the entry is updated.
136
     *
137
     * @param  string  $domain
138
     * @param  string  $key
139
     * @param  mixed   $value
140
     *
141
     * @return bool
142
     */
143 6
    private function isUpdated($domain, $key, $value)
144
    {
145 6
        return array_get($this->saved->get($domain, []), $key) !== $value;
146
    }
147
}
148