|
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\AppEntity\DockerPortMapping; |
|
17
|
|
|
use Chapi\Entity\Marathon\MarathonAppEntity; |
|
18
|
|
|
use Chapi\Service\JobRepository\JobRepositoryInterface; |
|
19
|
|
|
|
|
20
|
|
|
class MarathonJobComparisonBusinessCase extends AbstractJobComparisionBusinessCase |
|
21
|
|
|
{ |
|
22
|
|
|
/** |
|
23
|
|
|
* @param JobRepositoryInterface $localRepository |
|
24
|
|
|
* @param JobRepositoryInterface $remoteRepository |
|
25
|
|
|
* @param DiffCompareInterface $diffCompare |
|
26
|
|
|
*/ |
|
27
|
24 |
|
public function __construct( |
|
28
|
|
|
JobRepositoryInterface $localRepository, |
|
29
|
|
|
JobRepositoryInterface $remoteRepository, |
|
30
|
|
|
DiffCompareInterface $diffCompare |
|
31
|
|
|
) { |
|
32
|
24 |
|
$this->remoteRepository = $remoteRepository; |
|
33
|
24 |
|
$this->localRepository = $localRepository; |
|
34
|
24 |
|
$this->diffCompare = $diffCompare; |
|
35
|
24 |
|
} |
|
36
|
|
|
|
|
37
|
4 |
|
protected function preCompareModifications(JobEntityInterface &$localJob, JobEntityInterface &$remoteJob) |
|
38
|
|
|
{ |
|
39
|
4 |
|
if (!$localJob instanceof MarathonAppEntity || |
|
40
|
4 |
|
!$remoteJob instanceof MarathonAppEntity |
|
41
|
|
|
) { |
|
42
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
|
43
|
|
|
} |
|
44
|
|
|
// marathon returns portDefinitions values for auto configured port as well |
|
45
|
|
|
// we want to only check if the port is defined in local file. |
|
46
|
|
|
// otherwise we ignore the remote values. |
|
47
|
4 |
|
if (!$localJob->portDefinitions) { |
|
|
|
|
|
|
48
|
4 |
|
$remoteJob->portDefinitions = null; |
|
|
|
|
|
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
4 |
|
if ($localJob->container && $localJob->container->docker && |
|
52
|
4 |
|
$remoteJob->container && $remoteJob->container->docker) { |
|
53
|
|
|
|
|
54
|
1 |
|
foreach ($localJob->container->docker->portMappings as $index => $localPortMapping) { |
|
55
|
1 |
|
if ($localPortMapping->servicePort !== 0) { |
|
56
|
|
|
continue; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
1 |
|
if (!isset($remoteJob->container->docker->portMappings, $index)) { |
|
60
|
|
|
continue; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
$remotePortMapping = $remoteJob->container->docker->portMappings[$index]; |
|
64
|
|
|
|
|
65
|
1 |
|
if ($remotePortMapping != $localPortMapping) { |
|
66
|
1 |
|
$fixedPortMapping = clone $remotePortMapping; |
|
67
|
1 |
|
$fixedPortMapping->servicePort = 0; |
|
68
|
|
|
|
|
69
|
1 |
|
if ($fixedPortMapping == $localPortMapping) { |
|
70
|
1 |
|
unset($localJob->container->docker->portMappings[$index]); |
|
71
|
1 |
|
unset($remoteJob->container->docker->portMappings[$index]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
4 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* @return JobEntityInterface |
|
80
|
|
|
*/ |
|
81
|
|
|
protected function getEntitySetWithDefaults() |
|
82
|
|
|
{ |
|
83
|
|
|
return new MarathonAppEntity(); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityA |
|
88
|
|
|
* @param JobEntityInterface|ChronosJobEntity $jobEntityB |
|
89
|
|
|
* @return bool |
|
90
|
|
|
*/ |
|
91
|
|
|
public function hasSameJobType(JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
|
92
|
|
|
{ |
|
93
|
|
|
// for now we don't have a concrete seperation |
|
94
|
|
|
// of types for marathon. |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @param $property |
|
100
|
|
|
* @param $jobEntityA |
|
101
|
|
|
* @param $jobEntityB |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
18 |
|
protected function isEntityEqual($property, JobEntityInterface $jobEntityA, JobEntityInterface $jobEntityB) |
|
105
|
|
|
{ |
|
106
|
18 |
|
if (!$jobEntityA instanceof MarathonAppEntity || |
|
107
|
18 |
|
!$jobEntityB instanceof MarathonAppEntity |
|
108
|
|
|
) { |
|
109
|
|
|
throw new \RuntimeException('Required MarathonAppEntity. Something else encountered.'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
18 |
|
return $this->isEqual($jobEntityA->{$property}, $jobEntityB->{$property}); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param mixed $valueA |
|
117
|
|
|
* @param mixed $valueB |
|
118
|
|
|
* @return bool |
|
119
|
|
|
*/ |
|
120
|
19 |
|
private function isEqual($valueA, $valueB) |
|
121
|
|
|
{ |
|
122
|
19 |
|
if (is_array($valueA) && is_array($valueB)) { |
|
123
|
7 |
|
return $this->isArrayEqual($valueA, $valueB); |
|
124
|
18 |
|
} elseif (is_object($valueA) && is_object($valueB)) { |
|
125
|
8 |
|
return $this->isArrayEqual(get_object_vars($valueA), get_object_vars($valueB)); |
|
126
|
16 |
|
} elseif ((is_scalar($valueA) && is_scalar($valueB)) || (is_null($valueA) && is_null($valueB))) { |
|
127
|
12 |
|
return $valueA == $valueB; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
5 |
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param array $valuesA |
|
135
|
|
|
* @param array $valuesB |
|
136
|
|
|
* @return bool |
|
137
|
|
|
*/ |
|
138
|
12 |
|
private function isArrayEqual(array $valuesA, array $valuesB) |
|
139
|
|
|
{ |
|
140
|
12 |
|
return $this->isArrayHalfEqual($valuesA, $valuesB) && $this->isArrayHalfEqual($valuesB, $valuesA); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* @param array $valuesA |
|
145
|
|
|
* @param array $valuesB |
|
146
|
|
|
* @return bool |
|
147
|
|
|
*/ |
|
148
|
12 |
|
private function isArrayHalfEqual(array $valuesA, array $valuesB) |
|
149
|
|
|
{ |
|
150
|
12 |
|
foreach ($valuesA as $keyA => $valueA) { |
|
151
|
11 |
|
if (is_string($keyA)) { |
|
152
|
8 |
|
if (!array_key_exists($keyA, $valuesB) || !$this->isEqual($valueA, $valuesB[$keyA])) { |
|
153
|
8 |
|
return false; |
|
154
|
|
|
} |
|
155
|
|
|
} else { |
|
156
|
6 |
|
foreach ($valuesB as $valueB) { |
|
157
|
6 |
|
if ($this->isEqual($valueA, $valueB)) { |
|
158
|
6 |
|
continue 2; |
|
159
|
|
|
} |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
8 |
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
9 |
|
return true; |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
|
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.