Passed
Push — master ( fa9d64...8721e7 )
by Mihail
02:08
created

Application::deploidStructureInit()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 64
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 32
CRAP Score 15.6888

Importance

Changes 0
Metric Value
cc 10
eloc 47
nc 32
nop 1
dl 0
loc 64
ccs 32
cts 52
cp 0.6153
crap 15.6888
rs 7.2896
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Deploid;
4
5
use Symfony\Component\Console\Application as ConsoleApplication;
6
use Psr\Log\LoggerAwareInterface;
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\Process\Process;
9
10
class Application extends ConsoleApplication implements LoggerAwareInterface {
11
12
	/** @var LoggerInterface */
13
	private $logger;
14
15
	/** @var array */
16
	private $structure = [
17
		'current',
18
		'releases',
19
		'shared',
20
		'deploid.log',
21
	];
22
23
	/* mutators */
24
25
	/**
26
	 * @return LoggerInterface
27
	 */
28
	public function getLogger() {
29
		return $this->logger;
30
	}
31
32
	/**
33
	 * @param LoggerInterface $logger
34
	 * @return void
35
	 */
36
	public function setLogger(LoggerInterface $logger) {
37
		$this->logger = $logger;
38
	}
39
40
	public function getStructure() {
41
		return $this->structure;
42
	}
43
44
	public function setStructure($structure) {
45
		$this->structure = $structure;
46
	}
47
48
	/* tools */
49
50
	/**
51
	 * @param string $path
52
	 * @return \Deploid\Payload
53
	 */
54
	public function deploidStructureValidate($path) {
55
		$payload = new Payload();
56
57
		$path = $this->absolutePath($path, getcwd());
58
59
		$releasesDir = realpath($path) . DIRECTORY_SEPARATOR . 'releases';
60
		if (!is_dir($releasesDir)) {
61
			$payload->setType(Payload::STRUCTURE_VALIDATE_FAIL);
62
			$payload->setMessage('directory "' . $releasesDir . '" does not exist');
63
			$payload->setCode(255);
64
			return $payload;
65
		}
66
67
		$sharedDir = realpath($path) . DIRECTORY_SEPARATOR . 'shared';
68
		if (!is_dir($sharedDir)) {
69
			$payload->setType(Payload::STRUCTURE_VALIDATE_FAIL);
70
			$payload->setMessage('directory "' . $sharedDir . '" does not exist');
71
			$payload->setCode(255);
72
			return $payload;
73
		}
74
75
		$logFile = realpath($path) . DIRECTORY_SEPARATOR . 'deploid.log';
76
		if (!is_file($logFile)) {
77
			$payload->setType(Payload::STRUCTURE_VALIDATE_FAIL);
78
			$payload->setMessage('file "' . $logFile . '" does not exist');
79
			$payload->setCode(255);
80
			return $payload;
81
		}
82
83
		$payload->setType(Payload::STRUCTURE_VALIDATE_SUCCESS);
84
		$payload->setMessage('structure is valid');
85
		$payload->setCode(0);
86
		return $payload;
87
	}
88
89
	/**
90
	 * @param string $path
91
	 * @return \Deploid\Payload
92
	 */
93 1
	public function deploidStructureInit($path) {
94 1
		$payload = new Payload();
95
96 1
		if (!strlen($path)) {
97
			$payload->setType(Payload::STRUCTURE_INIT_FAIL);
98
			$payload->setMessage('empty path');
99
			$payload->setCode(255);
100
			return $payload;
101
		}
102
103 1
		$path = $this->absolutePath($path, getcwd());
104 1
		$messages = [];
105
106 1
		if (!is_dir($path)) {
107 1
			if (mkdir($path, 0777, true)) {
108 1
				$messages[] = 'directory "' . realpath($path) . '" created';
109 1
			} else {
110
				$payload->setType(Payload::STRUCTURE_INIT_FAIL);
111
				$payload->setMessage('directory "' . $path . '" does not created');
112
				$payload->setCode(255);
113
				return $payload;
114
			}
115 1
		}
116
117 1
		$releasesDir = $path . DIRECTORY_SEPARATOR . 'releases';
118 1
		if (!is_dir($releasesDir)) {
119 1
			if (mkdir($releasesDir, 0777, true)) {
120 1
				$messages[] = 'directory "' . realpath($releasesDir) . '" created';
121 1
			} else {
122
				$payload->setType(Payload::STRUCTURE_INIT_FAIL);
123
				$payload->setMessage('directory "' . $releasesDir . '" does not created');
124
				$payload->setCode(255);
125
				return $payload;
126
			}
127 1
		}
128
129 1
		$sharedDir = $path . DIRECTORY_SEPARATOR . 'shared';
130 1
		if (!is_dir($sharedDir)) {
131 1
			if (mkdir($sharedDir, 0777, true)) {
132 1
				$messages[] = 'directory "' . realpath($sharedDir) . '" created';
133 1
			} else {
134
				$payload->setType(Payload::STRUCTURE_INIT_FAIL);
135
				$payload->setMessage('directory "' . $sharedDir . '" does not created');
136
				$payload->setCode(255);
137
				return $payload;
138
			}
139 1
		}
140
141 1
		$logFile = $path . DIRECTORY_SEPARATOR . 'deploid.log';
142 1
		if (!is_file($logFile)) {
143 1
			if (touch($logFile)) {
144 1
				$messages[] = 'file "' . realpath($logFile) . '" created';
145 1
			} else {
146
				$payload->setType(Payload::STRUCTURE_INIT_FAIL);
147
				$payload->setMessage('file"' . $logFile . '" does not created');
148
				$payload->setCode(255);
149
				return $payload;
150
			}
151 1
		}
152
153 1
		$payload->setType(Payload::STRUCTURE_INIT_SUCCESS);
154 1
		$payload->setMessage($messages);
155 1
		$payload->setCode(0);
156 1
		return $payload;
157
	}
158
159
	/**
160
	 * @param string $path
161
	 * @return \Deploid\Payload
162
	 */
163 1
	public function deploidStructureClean($path) {
164 1
		$payload = new Payload();
165
166 1
		if (!strlen($path)) {
167
			$payload->setType(Payload::STRUCTURE_CLEAN_FAIL);
168
			$payload->setMessage('empty path');
169
			$payload->setCode(255);
170
			return $payload;
171
		}
172
173 1
		$path = $this->absolutePath($path, getcwd());
174
175 1
		$paths = glob(realpath($path) . DIRECTORY_SEPARATOR . '*');
176
177
		$paths = array_filter($paths, function ($path) {
178 1
			return !in_array(basename($path), $this->structure);
179 1
		});
180
181 1
		foreach ($paths as $pathname) {
182 1
			if (is_dir($pathname)) {
183 1
				rmdir($pathname);
184 1
			} else if (is_file($pathname)) {
185 1
				unlink($pathname);
186 1
			}
187 1
		}
188
189 1
		$payload->setType(Payload::STRUCTURE_CLEAN_SUCCESS);
190 1
		$payload->setMessage(array_merge(['cleaned items:'], $paths));
191 1
		$payload->setCode(0);
192 1
		return $payload;
193
	}
194
195
	/**
196
	 * @param string $release
197
	 * @param string $path
198
	 * @return \Deploid\Payload
199
	 */
200
	public function deploidReleaseExist($release, $path) {
201
		$payload = new Payload();
202
203
		if (!strlen($release)) {
204
			$payload->setType(Payload::RELEASE_EXIST_FAIL);
205
			$payload->setMessage('empty release name');
206
			$payload->setCode(255);
207
			return $payload;
208
		}
209
210
		if (!strlen($path)) {
211
			$payload->setType(Payload::RELEASE_EXIST_FAIL);
212
			$payload->setMessage('empty path');
213
			$payload->setCode(255);
214
			return $payload;
215
		}
216
217
		$path = $this->absolutePath($path, getcwd());
218
219
		$releaseDir = $path . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . $release;
220
		if (!is_dir($releaseDir)) {
221
			$payload->setType(Payload::RELEASE_EXIST_FAIL);
222
			$payload->setMessage('release "' . $release . '" does not exist');
223
			$payload->setCode(255);
224
			return $payload;
225
		}
226
227
		$payload->setType(Payload::RELEASE_EXIST_SUCCESS);
228
		$payload->setMessage('release "' . $release . '" exist');
229
		$payload->setCode(0);
230
		return $payload;
231
	}
232
233
	/**
234
	 * @param string $release
235
	 * @param string $path
236
	 * @return \Deploid\Payload
237
	 */
238
	public function deploidReleaseCreate($release, $path) {
239
		$payload = new Payload();
240
241
		if (!strlen($release)) {
242
			$payload->setType(Payload::RELEASE_CREATE_FAIL);
243
			$payload->setMessage('empty release name');
244
			$payload->setCode(255);
245
			return $payload;
246
		}
247
248
		if (!strlen($path)) {
249
			$payload->setType(Payload::RELEASE_CREATE_FAIL);
250
			$payload->setMessage('empty path');
251
			$payload->setCode(255);
252
			return $payload;
253
		}
254
255
		$path = $this->absolutePath($path, getcwd());
256
257
		$releaseDir = realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . $release;
258
259
		if (!mkdir($releaseDir, 0777, true)) {
260
			$payload->setType(Payload::RELEASE_CREATE_FAIL);
261
			$payload->setMessage('release "' . $release . '" does not created');
262
			$payload->setCode(255);
263
			return $payload;
264
		}
265
266
		$payload->setType(Payload::RELEASE_CREATE_SUCCESS);
267
		$payload->setMessage('release "' . $release . '" created');
268
		$payload->setCode(0);
269
		return $payload;
270
	}
271
272
	/**
273
	 * @param string $release
274
	 * @param string $path
275
	 * @return \Deploid\Payload
276
	 */
277
	public function deploidReleaseRemove($release, $path) {
278
		$payload = new Payload();
279
280
		if (!strlen($release)) {
281
			$payload->setType(Payload::RELEASE_REMOVE_FAIL);
282
			$payload->setMessage('empty release name');
283
			$payload->setCode(255);
284
			return $payload;
285
		}
286
287
		if (!strlen($path)) {
288
			$payload->setType(Payload::RELEASE_REMOVE_FAIL);
289
			$payload->setMessage('empty path');
290
			$payload->setCode(255);
291
			return $payload;
292
		}
293
294
		$path = $this->absolutePath($path, getcwd());
295
296
		$proccess = new Process('rm -r ' . realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . $release);
297
		$proccess->run();
298
299
		if (!$proccess->isSuccessful()) {
300
			$payload->setType(Payload::RELEASE_REMOVE_FAIL);
301
			$payload->setMessage($proccess->getErrorOutput());
302
			$payload->setCode($proccess->getExitCode());
303
			return $payload;
304
		}
305
306
		$payload->setType(Payload::RELEASE_REMOVE_SUCCESS);
307
		$payload->setMessage('release "' . ($release == '*' ? 'all' : $release) . '" removed');
308
		$payload->setCode(0);
309
		return $payload;
310
	}
311
312
	/**
313
	 * @param string $path
314
	 * @return \Deploid\Payload
315
	 */
316
	public function deploidReleaseList($path) {
317
		$payload = new Payload();
318
319
		if (!strlen($path)) {
320
			$payload->setType(Payload::RELEASE_LIST_FAIL);
321
			$payload->setMessage('path "' . $path . '" invalid');
322
			$payload->setCode(255);
323
			return $payload;
324
		}
325
326
		$path = $this->absolutePath($path, getcwd());
327
328
		$dirs = glob(realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
329
330
		if (empty($dirs)) {
331
			$payload->setType(Payload::RELEASE_LIST_FAIL);
332
			$payload->setMessage('release not found');
333
			$payload->setCode(0);
334
			return $payload;
335
		}
336
337
		$dirs = array_map(function ($path) {
338
			return basename($path);
339
		}, $dirs);
340
341
		$payload->setType(Payload::RELEASE_LIST_SUCCESS);
342
		$payload->setMessage($dirs);
343
		$payload->setCode(0);
344
		return $payload;
345
	}
346
347
	/**
348
	 * @param string $path
349
	 * @return \Deploid\Payload
350
	 */
351
	public function deploidReleaseLatest($path) {
352
		$payload = new Payload();
353
354
		if (!strlen($path)) {
355
			$payload->setType(Payload::RELEASE_LATEST_FAIL);
356
			$payload->setMessage('empty path');
357
			$payload->setCode(255);
358
			return $payload;
359
		}
360
361
		$path = $this->absolutePath($path, getcwd());
362
363
		$dirs = glob(realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
364
365
		if (empty($dirs)) {
366
			$payload->setType(Payload::RELEASE_LATEST_FAIL);
367
			$payload->setMessage('release not found');
368
			$payload->setCode(255);
369
			return $payload;
370
		}
371
372
		$dirs = array_map(function ($path) {
373
			return basename($path);
374
		}, $dirs);
375
376
		if (!rsort($dirs)) {
377
			$payload->setType(Payload::RELEASE_LATEST_FAIL);
378
			$payload->setMessage('fail sorted');
379
			$payload->setCode(255);
380
			return $payload;
381
		}
382
383
		$payload->setType(Payload::RELEASE_LATEST_SUCCESS);
384
		$payload->setMessage(current($dirs));
385
		$payload->setCode(0);
386
		return $payload;
387
	}
388
389
	/**
390
	 * @param string $path
391
	 * @return \Deploid\Payload
392
	 */
393
	public function deploidReleaseCurrent($path) {
394
		$payload = new Payload();
395
396
		if (!strlen($path)) {
397
			$payload->setType(Payload::RELEASE_CURRENT_FAIL);
398
			$payload->setMessage('empty path');
399
			$payload->setCode(255);
400
			return $payload;
401
		}
402
403
		$path = $this->absolutePath($path, getcwd());
404
405
		$link = realpath($path) . DIRECTORY_SEPARATOR . 'current';
406
407
		if (!file_exists($link)) {
408
			$payload->setType(Payload::RELEASE_CURRENT_FAIL);
409
			$payload->setMessage('current release does not exist');
410
			$payload->setCode(255);
411
			return $payload;
412
		}
413
414
		if (!is_link($link)) {
415
			$payload->setType(Payload::RELEASE_CURRENT_FAIL);
416
			$payload->setMessage('link to current release does not exist');
417
			$payload->setCode(255);
418
			return $payload;
419
		}
420
421
		$linkpath = readlink($link);
422
423
		if (!$linkpath) {
424
			$payload->setType(Payload::RELEASE_CURRENT_FAIL);
425
			$payload->setMessage('fail read link to current release');
426
			$payload->setCode(255);
427
			return $payload;
428
		}
429
430
		$payload->setType(Payload::RELEASE_CURRENT_SUCCESS);
431
		$payload->setMessage(basename($linkpath));
432
		$payload->setCode(0);
433
		return $payload;
434
	}
435
436
	/**
437
	 * @param string $release
438
	 * @param string $path
439
	 * @return \Deploid\Payload
440
	 */
441
	public function deploidReleaseSetup($release, $path) {
442
		$payload = new Payload();
443
444
		if (!strlen($path)) {
445
			$payload->setType(Payload::RELEASE_SETUP_FAIL);
446
			$payload->setMessage('path "' . $path . '" invalid');
447
			$payload->setCode(255);
448
			return $payload;
449
		}
450
451
		$path = $this->absolutePath($path, getcwd());
452
453
		$releaseDir = realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . $release;
454
		$currentDir = realpath($path) . DIRECTORY_SEPARATOR . 'current';
455
456
		$proccess = new Process('ln -sfn ' . $releaseDir . ' ' . $currentDir);
457
		$proccess->run();
458
459
		if (!$proccess->isSuccessful()) {
460
			$payload->setType(Payload::RELEASE_SETUP_FAIL);
461
			$payload->setMessage($proccess->getErrorOutput());
462
			$payload->setCode($proccess->getExitCode());
463
			return $payload;
464
		}
465
466
		$payload->setType(Payload::RELEASE_SETUP_SUCCESS);
467
		$payload->setMessage('release "' . $release . '" setup');
468
		$payload->setCode(0);
469
		return $payload;
470
	}
471
472
	/**
473
	 * @param int $quantity
474
	 * @param string $path
475
	 * @return \Deploid\Payload
476
	 */
477
	public function deploidReleaseRotate($quantity, $path) {
478
		$payload = new Payload();
479
480
		if (!$quantity || $quantity < 1) {
481
			$payload->setType(Payload::RELEASE_ROTATE_FAIL);
482
			$payload->setMessage('empty or invalid quantity');
483
			$payload->setCode(255);
484
			return $payload;
485
		}
486
487
		if (!strlen($path)) {
488
			$payload->setType(Payload::RELEASE_ROTATE_FAIL);
489
			$payload->setMessage('empty path');
490
			$payload->setCode(255);
491
			return $payload;
492
		}
493
494
		$path = $this->absolutePath($path, getcwd());
495
496
		$releases = glob(realpath($path) . DIRECTORY_SEPARATOR . 'releases' . DIRECTORY_SEPARATOR . '*', GLOB_ONLYDIR);
497
498
		if (count($releases) <= $quantity) {
499
			$payload->setType(Payload::RELEASE_ROTATE_SUCCESS);
500
			$payload->setMessage('not found releases to rotate');
501
			$payload->setCode(0);
502
		}
503
504
		foreach (array_reverse($releases) as $idx => $release) {
505
			if ($idx <= ($quantity - 1)) continue;
506
			$proccess = new Process('rm -r ' . $release);
507
			$proccess->run();
508
		}
509
510
		$payload->setType(Payload::RELEASE_ROTATE_SUCCESS);
511
		$payload->setMessage('releases are rotated');
512
		$payload->setCode(0);
513
		return $payload;
514
	}
515
516
	public function absolutePath($path, $cwd) {
517
		if ($path[0] == '/') return $path;
518
		return $cwd . DIRECTORY_SEPARATOR . $path;
519
	}
520
521
}