ProcessRunner   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 2
lcom 1
cbo 3
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A run() 0 18 1
1
<?php
2
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Utility;
9
10
use Deployer\Deployer;
11
use Symfony\Component\Process\Exception\ProcessFailedException;
12
use Symfony\Component\Process\Process;
13
14
class ProcessRunner
15
{
16
    /**
17
     * @var ProcessOutputPrinter
18
     */
19
    private $pop;
20
21 11
    public function __construct(ProcessOutputPrinter $pop)
22
    {
23 11
        $this->pop = $pop;
24 11
    }
25
26
    /**
27
     * Runs a command, consider deployer global configs (timeout,...)
28
     *
29
     * @param string $hostname
30
     * @param string $command
31
     * @param array $config
32
     *
33
     * @return string
34
     *
35
     * @throws ProcessFailedException When the process does not return a 0 exit code.
36
     */
37 11
    public function run($hostname, string $command, array $config = [])
38
    {
39
        $defaults = [
40 11
            'timeout' => Deployer::getDefault('default_timeout', 300),
41
            'tty' => false,
42
        ];
43 11
        $config = array_merge($defaults, $config);
44
45 11
        $this->pop->command($hostname, $command);
46
47 11
        $process = new Process($command);
48
        $process
49 11
            ->setTimeout($config['timeout'])
50 11
            ->setTty($config['tty'])
51 11
            ->mustRun($this->pop->callback($hostname));
52
53 11
        return $process->getOutput();
54
    }
55
}
56