AbstractCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
c 2
b 0
f 0
lcom 0
cbo 1
dl 0
loc 42
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 12 2
A validateSystemEnvironment() 0 1 1
A validateExecuteReturn() 0 10 2
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