Executor   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 8.33%

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 2
dl 0
loc 92
ccs 3
cts 36
cp 0.0833
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
B execute() 0 30 6
A createProcess() 0 14 4
A processCallback() 0 6 2
A __construct() 0 4 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Tool\Container;
8
9
use Symfony\Component\Process\Process;
10
11
/**
12
 * Runs the commands in the shell.
13
 */
14
class Executor
15
{
16
    /**
17
     * Constructor
18
     *
19
     * @param Container $container
20
     */
21 8
    public function __construct(Container $container)
22
    {
23 8
        $this->container = $container;
0 ignored issues
show
Bug introduced by
The property container does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
24 8
    }
25
26
27
    /**
28
     * Executes the passed command line in the shell.
29
     *
30
     * @param string $cmd
31
     * @param null &$captureOutput
32
     * @param string $captureOutput
33
     * @return int
34
     *
35
     * @throws ExecutionAbortedException
36
     * @throws \UnexpectedValueException
37
     */
38
    public function execute($cmd, &$captureOutput = null)
39
    {
40
        $isInteractive = $this->container->get('INTERACTIVE');
41
        $process = $this->createProcess($isInteractive);
42
43
        if ($isInteractive) {
44
            $process->setCommandLine(sprintf('/bin/bash -c \'%s\'', $cmd));
45
        } else {
46
            $process->setInput($cmd);
47
        }
48
49
        if (null !== $captureOutput && false === $isInteractive) {
50
            $process->run(function ($type, $data) use(&$captureOutput) {
51
                $captureOutput .= $data;
52
            });
53
        } else {
54
            $process->run(array($this, 'processCallback'));
55
        }
56
57
        $ret = $process->getExitCode();
58
59
        if ($ret != 0) {
0 ignored issues
show
Bug introduced by
It seems like you are loosely comparing $ret of type integer|null to 0; this is ambiguous as not only 0 == 0 is true, but null == 0 is true, too. Consider using a strict comparison ===.
Loading history...
60
            if ((int)$ret == Container::ABORT_EXIT_CODE) {
61
                throw new ExecutionAbortedException("Command '$cmd' was aborted");
62
            } else {
63
                throw new \UnexpectedValueException("Command '$cmd' failed with exit code {$ret}");
64
            }
65
        }
66
        return $ret;
67
    }
68
69
70
    /**
71
     * Create the process instance to use for non-interactive handling
72
     *
73
     * @var     bool $interactive
74
     * @return \Symfony\Component\Process\Process
75
     */
76
    protected function createProcess($interactive = false)
77
    {
78
        $process = new Process($this->container->resolve('SHELL'), null, null, null, null, []);
79
80
        if ($interactive) {
81
            $process->setTty(true);
82
        } else {
83
            if ($this->container->has('TIMEOUT') && $timeout = $this->container->get('TIMEOUT')) {
84
                $process->setTimeout($this->container->get('TIMEOUT'));
85
            }
86
        }
87
88
        return $process;
89
    }
90
91
92
    /**
93
     * The callback used for the process executed by Process
94
     *
95
     * @param mixed $mode
96
     * @param string $data
97
     * @return void
98
     */
99
    public function processCallback($mode, $data)
100
    {
101
        if (isset($this->container->output)) {
102
            $this->container->output->write($data);
103
        }
104
    }
105
}
106