@@ -20,6 +20,17 @@ class DataDifferenceCalculator
20
20
*/
21
21
private $ runtimeConfigSource ;
22
22
23
+ /**
24
+ * Scopes identifier
25
+ *
26
+ * @var string[]
27
+ */
28
+ private $ identifiers = [
29
+ 'websites ' => 'website_id ' ,
30
+ 'groups ' => 'group_id ' ,
31
+ 'stores ' => 'store_id ' ,
32
+ ];
33
+
23
34
/**
24
35
* @param ConfigSourceInterface $runtimeConfigSource The config source to retrieve current config
25
36
*/
@@ -28,6 +39,31 @@ public function __construct(ConfigSourceInterface $runtimeConfigSource)
28
39
$ this ->runtimeConfigSource = $ runtimeConfigSource ;
29
40
}
30
41
42
+ /**
43
+ * Update data by checking ID
44
+ *
45
+ * @param string $scope
46
+ * @param array $data
47
+ * @param array $runtimeScopeData
48
+ * @return array
49
+ */
50
+ private function updateDataById (string $ scope , array $ data , array $ runtimeScopeData ): array
51
+ {
52
+ $ diffData = array_diff_key ($ data , $ runtimeScopeData );
53
+ foreach ($ diffData as $ code => $ datum ) {
54
+ foreach ($ runtimeScopeData as $ runTimeScopeCode => $ runtimeScopeDatum ) {
55
+ if (isset ($ datum [$ this ->identifiers [$ scope ]])
56
+ && $ datum [$ this ->identifiers [$ scope ]] === $ runtimeScopeDatum [$ this ->identifiers [$ scope ]]
57
+ ) {
58
+ $ data [$ runTimeScopeCode ] = $ data [$ code ];
59
+ unset($ data [$ code ]);
60
+ }
61
+ }
62
+ }
63
+
64
+ return $ data ;
65
+ }
66
+
31
67
/**
32
68
* Calculates items to delete.
33
69
*
@@ -41,6 +77,7 @@ public function getItemsToDelete($scope, array $data)
41
77
$ runtimeScopeData = $ this ->changeDataKeyToCode (
42
78
$ this ->getRuntimeData ($ scope )
43
79
);
80
+ $ data = $ this ->updateDataById ($ scope , $ data , $ runtimeScopeData );
44
81
45
82
return array_diff_key ($ runtimeScopeData , $ data );
46
83
}
@@ -58,6 +95,7 @@ public function getItemsToCreate($scope, array $data)
58
95
$ runtimeScopeData = $ this ->changeDataKeyToCode (
59
96
$ this ->getRuntimeData ($ scope )
60
97
);
98
+ $ data = $ this ->updateDataById ($ scope , $ data , $ runtimeScopeData );
61
99
62
100
return array_diff_key ($ data , $ runtimeScopeData );
63
101
}
@@ -77,7 +115,7 @@ public function getItemsToUpdate($scope, array $data)
77
115
$ runtimeScopeData = $ this ->changeDataKeyToCode (
78
116
$ this ->getRuntimeData ($ scope )
79
117
);
80
-
118
+ $ data = $ this -> updateDataById ( $ scope , $ data , $ runtimeScopeData );
81
119
foreach ($ runtimeScopeData as $ entityCode => $ entityData ) {
82
120
if (isset ($ data [$ entityCode ]) && array_diff_assoc ($ entityData , $ data [$ entityCode ])) {
83
121
$ itemsToUpdate [$ entityCode ] = array_replace ($ entityData , $ data [$ entityCode ]);
0 commit comments