1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* @package: chapi |
5
|
|
|
* |
6
|
|
|
* @author: bthapaliya |
7
|
|
|
* @since: 2016-12-14 |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Chapi\BusinessCase\Comparison; |
12
|
|
|
|
13
|
|
|
use Chapi\Component\Comparison\DiffCompareInterface; |
14
|
|
|
use Chapi\Entity\Chronos\ChronosJobEntity; |
15
|
|
|
use Chapi\Entity\JobEntityInterface; |
16
|
|
|
use Chapi\Entity\Marathon\MarathonAppEntity; |
17
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
18
|
|
|
|
19
|
|
|
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @param JobRepositoryInterface $localRepository |
23
|
|
|
* @param JobRepositoryInterface $remoteRepository |
24
|
|
|
* @param DiffCompareInterface $diffCompare |
25
|
|
|
*/ |
26
|
22 |
|
public function __construct( |
27
|
|
|
JobRepositoryInterface $localRepository, |
28
|
|
|
JobRepositoryInterface $remoteRepository, |
29
|
|
|
DiffCompareInterface $diffCompare |
30
|
|
|
) { |
31
|
22 |
|
$this->remoteRepository = $remoteRepository; |
32
|
22 |
|
$this->localRepository = $localRepository; |
33
|
22 |
|
$this->diffCompare = $diffCompare; |
34
|
22 |
|
} |
35
|
|
|
|
36
|
4 |
|
protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob) |
37
|
|
|
{ |
38
|
4 |
|
if (!$localJob instanceof MarathonAppEntity || |
39
|
4 |
|
!$remoteJob instanceof MarathonAppEntity |
40
|
|
|
) { |
41
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
42
|
|
|
} |
43
|
|
|
// marathon returns portDefinitions values for auto configured port as well |
44
|
|
|
// we want to only check if the port is defined in local file. |
45
|
|
|
// otherwise we ignore the remote values. |
46
|
4 |
|
if (!$localJob->portDefinitions) { |
|
|
|
|
47
|
4 |
|
$remoteJob->portDefinitions = null; |
|
|
|
|
48
|
|
|
} |
49
|
4 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return JobEntityInterface |
53
|
|
|
*/ |
54
|
|
|
protected function getEntitySetWithDefaults() |
55
|
|
|
{ |
56
|
|
|
return new MarathonAppEntity(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityA |
61
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityB |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
65
|
|
|
{ |
66
|
|
|
// for now we don't have a concrete seperation |
67
|
|
|
// of types for marathon. |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param $property |
73
|
|
|
* @param $jobEntityA |
74
|
|
|
* @param $jobEntityB |
75
|
|
|
* @return bool |
76
|
|
|
*/ |
77
|
16 |
|
protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
78
|
|
|
{ |
79
|
16 |
|
if (!$jobEntityA instanceof MarathonAppEntity || |
80
|
16 |
|
!$jobEntityB instanceof MarathonAppEntity |
81
|
|
|
) { |
82
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
83
|
|
|
} |
84
|
|
|
|
85
|
16 |
|
return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property}); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param mixed $valueA |
90
|
|
|
* @param mixed $valueB |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
17 |
|
private function isEqual($valueA, $valueB) |
94
|
|
|
{ |
95
|
17 |
|
if (is_array($valueA) && is_array($valueB)) { |
96
|
7 |
|
return $this->isArrayEqual($valueA, $valueB); |
97
|
16 |
|
} elseif (is_object($valueA) && is_object($valueB)) { |
98
|
6 |
|
return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB)); |
99
|
14 |
|
} elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) { |
100
|
10 |
|
return $valueA == $valueB; |
101
|
|
|
} |
102
|
|
|
|
103
|
5 |
|
return false; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param array $valuesA |
108
|
|
|
* @param array $valuesB |
109
|
|
|
* @return bool |
110
|
|
|
*/ |
111
|
10 |
|
private function isArrayEqual(array $valuesA, array $valuesB) |
112
|
|
|
{ |
113
|
10 |
|
return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param array $valuesA |
118
|
|
|
* @param array $valuesB |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
10 |
|
private function isArrayHalfEqual(array $valuesA, array $valuesB) |
122
|
|
|
{ |
123
|
10 |
|
foreach ($valuesA as $keyA => $valueA) { |
124
|
9 |
|
if (is_string($keyA)) { |
125
|
6 |
|
if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) { |
126
|
6 |
|
return false; |
127
|
|
|
} |
128
|
|
|
} else { |
129
|
6 |
|
foreach ($valuesB as $valueB) { |
130
|
6 |
|
if ($this->isEqual($valueA, $valueB)) { |
131
|
6 |
|
continue 2; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
6 |
|
return false; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
139
|
7 |
|
return true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.