AbstractCommand::validateExecuteReturn()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
/**
3
 * @author stev leibelt <[email protected]>
4
 * @since 2015-09-18
5
 */
6
7
namespace Net\Bazzline\Component\Command;
8
9
/**
10
 * Class AbstractCommand
11
 *
12
 * @package Net\Bazzline\Component\Command
13
 */
14
abstract class AbstractCommand
15
{
16
    /**
17
     * @param string $command
18
     * @param boolean $validateReturnValue
19
     * @return array
20
     * @throws RuntimeException
21
     * @todo add callback as parameter
22
     */
23
    public function execute($command, $validateReturnValue = true)
24
    {
25
        $lines  = [];
26
        $return = null;
27
28
        exec($command, $lines, $return);
29
30
        if ($validateReturnValue) {
31
            $this->validateExecuteReturn($return, $command, $lines);
32
        }
33
        return $lines;
34
    }
35
    /**
36
     * @throws InvalidSystemEnvironmentException
37
     */
38
    public function validateSystemEnvironment() {}
39
    /**
40
     * @param int $return
41
     * @param string $command
42
     * @param mixed|array $lines
43
     * @throws RuntimeException
44
     */
45
    private function validateExecuteReturn($return, $command, $lines)
46
    {
47
        if ($return > 0) {
48
            throw new RuntimeException(
49
                'following command created an error: "' . $command . '"' . PHP_EOL .
50
                'exit code: "' . $return . '"' . PHP_EOL .
51
                'return: ' . var_export($lines, true)
52
            );
53
        }
54
    }
55
}
56