1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: bthapaliya |
5
|
|
|
* Date: 15/02/17 |
6
|
|
|
* Time: 15:01 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Chapi\BusinessCase\Comparison; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
use Chapi\Component\Comparison\DiffCompareInterface; |
13
|
|
|
use Chapi\Entity\Chronos\JobCollection; |
14
|
|
|
use Chapi\Entity\JobEntityInterface; |
15
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
16
|
|
|
|
17
|
|
|
abstract class AbstractJobComparisionBusinessCase implements JobComparisonInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var JobRepositoryInterface |
21
|
|
|
*/ |
22
|
|
|
protected $oRemoteRepository; |
23
|
|
|
/** |
24
|
|
|
* @var JobRepositoryInterface |
25
|
|
|
*/ |
26
|
|
|
protected $oLocalRepository; |
27
|
|
|
/** |
28
|
|
|
* @var DiffCompareInterface |
29
|
|
|
*/ |
30
|
|
|
protected $oDiffCompare; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @inheritdoc |
34
|
|
|
*/ |
35
|
1 |
|
public function getLocalMissingJobs() |
36
|
|
|
{ |
37
|
1 |
|
return $this->getMissingJobsInCollectionA( |
38
|
1 |
|
$this->oLocalRepository->getJobs(), |
39
|
1 |
|
$this->oRemoteRepository->getJobs() |
40
|
1 |
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @inheritdoc |
45
|
|
|
*/ |
46
|
1 |
|
public function getRemoteMissingJobs() |
47
|
|
|
{ |
48
|
1 |
|
return $this->getMissingJobsInCollectionA( |
49
|
1 |
|
$this->oRemoteRepository->getJobs(), |
50
|
1 |
|
$this->oLocalRepository->getJobs() |
51
|
1 |
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @inheritdoc |
56
|
|
|
*/ |
57
|
1 |
|
public function isJobAvailable($sJobName) |
58
|
|
|
{ |
59
|
1 |
|
$_bLocallyAvailable = $this->oLocalRepository->getJob($sJobName); |
60
|
1 |
|
$_bRemotelyAvailable = $this->oRemoteRepository->getJob($sJobName); |
61
|
1 |
|
return $_bLocallyAvailable || $_bRemotelyAvailable; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param JobCollection $oJobCollectionA |
67
|
|
|
* @param JobCollection $oJobCollectionB |
68
|
|
|
* @return array<string> |
|
|
|
|
69
|
|
|
*/ |
70
|
2 |
|
protected function getMissingJobsInCollectionA(JobCollection $oJobCollectionA, JobCollection $oJobCollectionB) |
71
|
|
|
{ |
72
|
2 |
|
return array_diff( |
73
|
2 |
|
array_keys($oJobCollectionB->getArrayCopy()), |
74
|
2 |
|
array_keys($oJobCollectionA->getArrayCopy()) |
75
|
2 |
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
7 |
|
protected function getDifference(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) |
79
|
|
|
{ |
80
|
7 |
|
$aJobACopy = []; |
81
|
7 |
|
$aJobBCopy = []; |
82
|
|
|
|
83
|
|
|
if ($oJobEntityA) |
84
|
7 |
|
{ |
85
|
7 |
|
$aJobACopy = $oJobEntityA->getSimpleArrayCopy(); |
86
|
7 |
|
} |
87
|
|
|
|
88
|
|
|
if ($oJobEntityB) |
89
|
7 |
|
{ |
90
|
7 |
|
$aJobBCopy = $oJobEntityB->getSimpleArrayCopy(); |
91
|
7 |
|
} |
92
|
|
|
|
93
|
7 |
|
return array_merge( |
94
|
7 |
|
array_diff_assoc( |
95
|
7 |
|
$aJobACopy, |
96
|
|
|
$aJobBCopy |
97
|
7 |
|
), |
98
|
7 |
|
array_diff_assoc( |
99
|
7 |
|
$aJobBCopy, |
100
|
|
|
$aJobACopy |
101
|
7 |
|
) |
102
|
7 |
|
); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param JobEntityInterface $oJobEntityA |
108
|
|
|
* @param JobEntityInterface $oJobEntityB |
109
|
|
|
* @return array |
110
|
|
|
*/ |
111
|
7 |
|
protected function compareJobEntities(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB) |
112
|
|
|
{ |
113
|
7 |
|
$_aNonidenticalProperties = []; |
114
|
|
|
|
115
|
7 |
|
$_aDiff = $this->getDifference($oJobEntityA, $oJobEntityB); |
116
|
|
|
|
117
|
7 |
|
if (count($_aDiff) > 0) |
118
|
7 |
|
{ |
119
|
7 |
|
$_aDiffKeys = array_keys($_aDiff); |
120
|
|
|
|
121
|
7 |
|
foreach ($_aDiffKeys as $_sDiffKey) |
122
|
5 |
|
{ |
123
|
7 |
|
if (!$this->isEntityEqual($_sDiffKey, $oJobEntityA, $oJobEntityB)) |
124
|
7 |
|
{ |
125
|
7 |
|
$_aNonidenticalProperties[] = $_sDiffKey; |
126
|
7 |
|
} |
127
|
7 |
|
} |
128
|
7 |
|
} |
129
|
|
|
|
130
|
7 |
|
return $_aNonidenticalProperties; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
1 |
|
public function getJobDiff($sJobName) |
138
|
|
|
{ |
139
|
1 |
|
$_aDifferences = []; |
140
|
1 |
|
$_oLocalEntity = $this->oLocalRepository->getJob($sJobName); |
141
|
1 |
|
$_oRemoteEntity = $this->oRemoteRepository->getJob($sJobName); |
142
|
|
|
|
143
|
1 |
|
if (!$_oLocalEntity && !$_oRemoteEntity) |
144
|
1 |
|
{ |
145
|
|
|
// return as jobs doesnt exist |
146
|
|
|
return []; |
147
|
|
|
} |
148
|
|
|
|
149
|
1 |
|
if (!$_oLocalEntity) |
150
|
1 |
|
{ |
151
|
|
|
$_oLocalEntity = $this->getEntitySetWithDefaults(); |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
if (!$_oRemoteEntity) |
155
|
1 |
|
{ |
156
|
|
|
$_oRemoteEntity = $this->getEntitySetWithDefaults(); |
157
|
|
|
} |
158
|
|
|
|
159
|
1 |
|
$_aNonIdenticalProps = $this->compareJobEntities( |
160
|
1 |
|
$_oLocalEntity, |
161
|
|
|
$_oRemoteEntity |
162
|
1 |
|
); |
163
|
|
|
|
164
|
1 |
|
foreach ($_aNonIdenticalProps as $_sProperty) |
165
|
|
|
{ |
166
|
1 |
|
$_aDifferences[$_sProperty] = $this->oDiffCompare->compare( |
167
|
1 |
|
$_oRemoteEntity->{$_sProperty}, |
168
|
1 |
|
$_oLocalEntity->{$_sProperty} |
169
|
1 |
|
) ; |
170
|
1 |
|
} |
171
|
|
|
|
172
|
1 |
|
return $_aDifferences; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @inheritdoc |
179
|
|
|
*/ |
180
|
6 |
|
public function getLocalJobUpdates() |
181
|
|
|
{ |
182
|
6 |
|
$_aLocallyUpdatedJobs = []; |
183
|
6 |
|
$_aLocalJobs = $this->oLocalRepository->getJobs(); |
184
|
|
|
|
185
|
|
|
/** @var JobEntityInterface $_oLocalJob */ |
186
|
6 |
|
foreach ($_aLocalJobs as $_oLocalJob) |
187
|
|
|
{ |
188
|
|
|
|
189
|
|
|
/** @var JobEntityInterface $_oRemoteJob */ |
190
|
6 |
|
$_oRemoteJob = $this->oRemoteRepository->getJob($_oLocalJob->getKey()); |
191
|
6 |
|
if (!$_oRemoteJob) |
192
|
6 |
|
{ |
193
|
|
|
// if doesn't exist in remote, its not update. its new |
194
|
|
|
continue; |
195
|
|
|
} |
196
|
|
|
|
197
|
6 |
|
$this->preCompareModifications($_oLocalJob, $_oRemoteJob); |
198
|
|
|
|
199
|
6 |
|
$_aNonidenticalProperties = $this->compareJobEntities($_oLocalJob, $_oRemoteJob); |
200
|
|
|
|
201
|
6 |
|
if (!empty($_aNonidenticalProperties)) |
202
|
6 |
|
{ |
203
|
6 |
|
$_aLocallyUpdatedJobs[] = $_oLocalJob->getKey(); |
204
|
6 |
|
} |
205
|
6 |
|
} |
206
|
6 |
|
return $_aLocallyUpdatedJobs; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* This method should perform any operation that is desired before comparing remote and local entities. |
212
|
|
|
* Why this is required? |
213
|
|
|
* For system like marathon, it is essential to set/unset certain values before comparing to make sane |
214
|
|
|
* comparision. |
215
|
|
|
* |
216
|
|
|
* Note: Should be careful with the parameters as they are passed by value. |
217
|
|
|
* |
218
|
|
|
* @param JobEntityInterface $oLocalJob |
219
|
|
|
* @param JobEntityInterface $oRemoteJob |
220
|
|
|
* @return null |
221
|
|
|
*/ |
222
|
|
|
abstract protected function preCompareModifications(JobEntityInterface &$oLocalJob, JobEntityInterface &$oRemoteJob); |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Gets entity for each system with defaults set |
226
|
|
|
* @return JobEntityInterface |
227
|
|
|
*/ |
228
|
|
|
abstract protected function getEntitySetWithDefaults(); |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Verify if two entities are equal. |
232
|
|
|
* |
233
|
|
|
* @param $sProperty |
234
|
|
|
* @param JobEntityInterface $oJobEntityA |
235
|
|
|
* @param JobEntityInterface $oJobEntityB |
236
|
|
|
* @return mixed |
237
|
|
|
*/ |
238
|
|
|
abstract protected function isEntityEqual($sProperty, JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB); |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @param JobEntityInterface $oJobEntityA |
242
|
|
|
* @param JobEntityInterface $oJobEntityB |
243
|
|
|
* @return bool |
244
|
|
|
*/ |
245
|
|
|
abstract public function hasSameJobType(JobEntityInterface $oJobEntityA, JobEntityInterface $oJobEntityB); |
246
|
|
|
} |
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.