Executor   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 7
Bugs 3 Features 2
Metric Value
wmc 7
c 7
b 3
f 2
lcom 0
cbo 1
dl 0
loc 40
ccs 25
cts 25
cp 1
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C executeCommand() 0 29 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