|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
class DeploymentStrategy extends ViewableData { |
|
4
|
|
|
|
|
5
|
|
|
const SUCCESS_CODE = 'success'; |
|
6
|
|
|
|
|
7
|
|
|
const WARNING_CODE = 'warning'; |
|
8
|
|
|
|
|
9
|
|
|
const ERROR_CODE = 'error'; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @var DNEnvironment |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $environment; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var string |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $actionTitle = 'Deploy'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $actionCode = 'default'; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var int |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $estimatedTime = 0; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var array |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $changes = []; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $options; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Validation code |
|
43
|
|
|
* |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $validationCode = DeploymentStrategy::SUCCESS_CODE; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $messages = []; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param DNEnvironment $environment |
|
56
|
|
|
* @param array $options |
|
57
|
|
|
*/ |
|
58
|
|
|
public function __construct(DNEnvironment $environment, $options = array()) { |
|
59
|
|
|
$this->environment = $environment; |
|
60
|
|
|
$this->options = $options; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $title |
|
65
|
|
|
*/ |
|
66
|
|
|
public function setActionTitle($title) { |
|
67
|
|
|
$this->actionTitle = $title; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @return string |
|
72
|
|
|
*/ |
|
73
|
|
|
public function getActionTitle() { |
|
74
|
|
|
return $this->actionTitle; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
*/ |
|
79
|
|
|
public function setActionCode($code) { |
|
80
|
|
|
$this->actionCode = $code; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getActionCode() { |
|
87
|
|
|
return $this->actionCode; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param int |
|
92
|
|
|
*/ |
|
93
|
|
|
public function setEstimatedTime($seconds) { |
|
94
|
|
|
$this->estimatedTime = $seconds; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @return int Time in minutes |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getEstimatedTime() { |
|
101
|
|
|
return $this->estimatedTime; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param string $title |
|
106
|
|
|
* @param string $from |
|
107
|
|
|
* @param string $to |
|
108
|
|
|
*/ |
|
109
|
|
|
public function setChange($title, $from, $to) { |
|
110
|
|
|
return $this->changes[$title] = array( |
|
111
|
|
|
'from' => $from, |
|
112
|
|
|
'to' => $to |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $title |
|
118
|
|
|
* @param string $desc |
|
119
|
|
|
*/ |
|
120
|
|
|
public function setChangeDescriptionOnly($title, $desc) { |
|
121
|
|
|
return $this->changes[$title] = array( |
|
122
|
|
|
'description' => $desc |
|
123
|
|
|
); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* Filter the changeset where modification was not required. |
|
128
|
|
|
* |
|
129
|
|
|
* @return array |
|
130
|
|
|
*/ |
|
131
|
|
|
public function getChangesModificationNeeded() { |
|
132
|
|
|
$filtered = []; |
|
133
|
|
|
foreach ($this->getChanges() as $change => $details) { |
|
134
|
|
|
if (array_key_exists('description', $details)) { |
|
135
|
|
|
$filtered[$change] = $details; |
|
136
|
|
|
} else if ( |
|
137
|
|
|
(array_key_exists('from', $details) || array_key_exists('to', $details)) |
|
138
|
|
|
&& $details['from'] !== $details['to'] |
|
139
|
|
|
) { |
|
140
|
|
|
$filtered[$change] = $details; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
return $filtered; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return array Associative array of changes, e.g. |
|
149
|
|
|
* array( |
|
150
|
|
|
* 'SHA' => array( |
|
151
|
|
|
* 'from' => 'abc', |
|
152
|
|
|
* 'to' => 'def' |
|
153
|
|
|
* ) |
|
154
|
|
|
* ) |
|
155
|
|
|
*/ |
|
156
|
|
|
public function getChanges() { |
|
157
|
|
|
// Normalise "empty" values into empty strings so comparisons are done properly. |
|
158
|
|
|
// This means there is no diference between an empty string and a null |
|
159
|
|
|
// but "0" is considered to be non-empty. |
|
160
|
|
|
$changes = $this->changes; |
|
161
|
|
|
foreach($changes as $field => $events) { |
|
162
|
|
|
foreach($events as $event => $value) { |
|
163
|
|
|
if(empty($value) && !strlen($value) || $value === '-') { |
|
164
|
|
|
$changes[$field][$event] = ''; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
return $changes; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* Returns a change or a given key. |
|
173
|
|
|
* |
|
174
|
|
|
* @return ArrayData|null |
|
175
|
|
|
*/ |
|
176
|
|
|
public function getChange($key) { |
|
177
|
|
|
$changes = $this->getChanges(); |
|
178
|
|
|
if(array_key_exists($key, $changes)) { |
|
179
|
|
|
return new ArrayData($changes[$key]); |
|
180
|
|
|
} |
|
181
|
|
|
return null; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $option |
|
186
|
|
|
* @param string $value |
|
187
|
|
|
*/ |
|
188
|
|
|
public function setOption($option, $value) { |
|
189
|
|
|
$this->options[$option] = $value; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* @param string $option |
|
194
|
|
|
* @return string|null |
|
195
|
|
|
*/ |
|
196
|
|
|
public function getOption($option) { |
|
197
|
|
|
if(!empty($this->options[$option])) { |
|
198
|
|
|
return $this->options[$option]; |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* @return string |
|
204
|
|
|
*/ |
|
205
|
|
|
public function getOptions() { |
|
206
|
|
|
return $this->options; |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @param string $code |
|
211
|
|
|
*/ |
|
212
|
|
|
public function setValidationCode($code) { |
|
213
|
|
|
$this->validationCode = $code; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @return string |
|
218
|
|
|
*/ |
|
219
|
|
|
public function getValidationCode() { |
|
220
|
|
|
return $this->validationCode; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @param string $msg |
|
225
|
|
|
*/ |
|
226
|
|
|
public function setMessage($msg, $code = self::ERROR_CODE) { |
|
227
|
|
|
$this->messages[] = [ |
|
228
|
|
|
'text' => $msg, |
|
229
|
|
|
'code' => $code |
|
230
|
|
|
]; |
|
231
|
|
|
|
|
232
|
|
|
$current = $this->getValidationCode(); |
|
233
|
|
|
$map = [ |
|
234
|
|
|
DeploymentStrategy::SUCCESS_CODE => 0, |
|
235
|
|
|
DeploymentStrategy::WARNING_CODE => 1, |
|
236
|
|
|
DeploymentStrategy::ERROR_CODE => 2 |
|
237
|
|
|
]; |
|
238
|
|
|
if($map[$current] < $map[$code]) { |
|
239
|
|
|
$this->setValidationCode($code); |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* @return array |
|
245
|
|
|
*/ |
|
246
|
|
|
public function getMessages() { |
|
247
|
|
|
return $this->messages; |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Transform the deployment strategy to an array. |
|
252
|
|
|
* |
|
253
|
|
|
* @return array |
|
254
|
|
|
*/ |
|
255
|
|
|
public function toArray() { |
|
256
|
|
|
$fields = array( |
|
257
|
|
|
'actionTitle', |
|
258
|
|
|
'actionCode', |
|
259
|
|
|
'estimatedTime', |
|
260
|
|
|
'changes', |
|
261
|
|
|
'options', |
|
262
|
|
|
'validationCode', |
|
263
|
|
|
'messages' |
|
264
|
|
|
); |
|
265
|
|
|
|
|
266
|
|
|
$output = array(); |
|
267
|
|
|
foreach($fields as $field) { |
|
268
|
|
|
if(method_exists($this, 'get' . $field)) { |
|
269
|
|
|
$output[$field] = $this->{'get' . $field}(); |
|
270
|
|
|
} else { |
|
271
|
|
|
$output[$field] = $this->$field; |
|
272
|
|
|
} |
|
273
|
|
|
} |
|
274
|
|
|
return $output; |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @return string |
|
279
|
|
|
*/ |
|
280
|
|
|
public function toJSON() { |
|
281
|
|
|
return json_encode($this->toArray(), JSON_PRETTY_PRINT); |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
/** |
|
285
|
|
|
* Load from JSON associative array. |
|
286
|
|
|
* Environment must be set by the callee when creating this object. |
|
287
|
|
|
* |
|
288
|
|
|
* @param string $json |
|
289
|
|
|
*/ |
|
290
|
|
|
public function fromJSON($json) { |
|
291
|
|
|
$decoded = json_decode($json, true); |
|
292
|
|
|
return $this->fromArray($decoded); |
|
293
|
|
|
} |
|
294
|
|
|
|
|
295
|
|
|
/** |
|
296
|
|
|
* Load from array. |
|
297
|
|
|
* Environment must be set by the callee when creating this object. |
|
298
|
|
|
* |
|
299
|
|
|
* @param string $data |
|
300
|
|
|
*/ |
|
301
|
|
|
public function fromArray($data) { |
|
302
|
|
|
$fields = array( |
|
303
|
|
|
'actionTitle', |
|
304
|
|
|
'actionCode', |
|
305
|
|
|
'estimatedTime', |
|
306
|
|
|
'changes', |
|
307
|
|
|
'options', |
|
308
|
|
|
'validationCode', |
|
309
|
|
|
'messages' |
|
310
|
|
|
); |
|
311
|
|
|
|
|
312
|
|
|
foreach($fields as $field) { |
|
313
|
|
|
if(!empty($data[$field])) { |
|
314
|
|
|
$this->$field = $data[$field]; |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
/** |
|
320
|
|
|
* @return DNDeployment |
|
321
|
|
|
*/ |
|
322
|
|
|
public function createDeployment() { |
|
323
|
|
|
$deployment = DNDeployment::create(); |
|
324
|
|
|
$deployment->EnvironmentID = $this->environment->ID; |
|
325
|
|
|
// Pull out the SHA from the options so we can make it queryable. |
|
326
|
|
|
$deployment->SHA = $this->getOption('sha'); |
|
327
|
|
|
$deployment->Strategy = $this->toJSON(); |
|
|
|
|
|
|
328
|
|
|
$deployment->write(); |
|
329
|
|
|
|
|
330
|
|
|
return $deployment; |
|
331
|
|
|
} |
|
332
|
|
|
|
|
333
|
|
|
} |
|
334
|
|
|
|
|
335
|
|
|
|
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.