Executor::executeCommand()   C
last analyzed

Complexity

Conditions 7
Paths 30

Size

Total Lines 29
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 25
CRAP Score 7

Importance

Changes 5
Bugs 1 Features 2
Metric Value
c 5
b 1
f 2
dl 0
loc 29
ccs 25
cts 25
cp 1
rs 6.7272
cc 7
eloc 21
nc 30
nop 2
crap 7
1
<?php
2
3
namespace App;
4
5
use App\Exception\ExecutionFailed;
6
7
8
class Executor
9
{
10
11
	/**
12
	 * @param string|array $scriptPath
13
	 * @param array $env
14
	 * @return string
15
	 * @throws ExecutionFailed
16
	 */
17 4
	public function executeCommand($scriptPath, array $env = [])
18
	{
19 4
		$oldCwd = NULL;
20 4
		if (is_array($scriptPath)) {
21 1
			if (isset($scriptPath['cwd'])) {
22 1
				$cwd = $scriptPath['cwd'];
23 1
				unset($scriptPath['cwd']);
24 1
				$oldCwd = getcwd();
25 1
				chdir($cwd);
26 1
			}
27 1
			$commands = $scriptPath;
28 1
		} else {
29 3
			$commands = [$scriptPath];
30
		}
31 4
		foreach ($env as $key => $value) {
32 4
			putenv($key . '=' . $value);
33 4
		}
34 4
		$result = [];
35 4
		foreach ($commands as $command) {
36 4
			exec($command, $result, $return);
37 4
			if ($return !== 0) {
38 1
				throw new ExecutionFailed('Command ' . $command . 'resulted in error: ' . $return, $return);
39
			}
40 3
		}
41 3
		if ($oldCwd !== NULL) {
42 1
			chdir($oldCwd);
43 1
		}
44 3
		return implode("\n", $result);
45
	}
46
47
}
48