Completed
Push — master ( 5735e1...ecb9c5 )
by Sebastian
02:27
created

Simple::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 13
ccs 7
cts 7
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
crap 3
1
<?php
2
/**
3
 * This file is part of SebastianFeldmann\Cli.
4
 *
5
 * (c) Sebastian Feldmann <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace SebastianFeldmann\Cli\Process\Runner;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command;
14
use SebastianFeldmann\Cli\Command\OutputFormatter;
15
use SebastianFeldmann\Cli\Process;
16
use SebastianFeldmann\Cli\Process\Runner;
17
18
class Simple implements Runner
19
{
20
    /**
21
     * Class handling system calls.
22
     *
23
     * @var \SebastianFeldmann\Cli\Process
24
     */
25
    private $process;
26
27
    /**
28
     * Exec constructor.
29
     *
30
     * @param \SebastianFeldmann\Cli\Process $process
31
     */
32 2
    public function __construct(Process $process = null)
33
    {
34 2
        $this->process = $process !== null ? $process : new Process\ProcOpen();
35 2
    }
36
37
    /**
38
     * Execute a cli command.
39
     *
40
     * @param  \SebastianFeldmann\Cli\Command                 $command
41
     * @param  \SebastianFeldmann\Cli\Command\OutputFormatter $formatter
42
     * @return \SebastianFeldmann\Cli\Process\Runner\Result
43
     */
44 2
    public function run(Command $command, OutputFormatter $formatter = null) : Result
45
    {
46 2
        $cmd = $this->process->run($command->getCommand());
47
48 2
        if (!$cmd->wasSuccessful()) {
49 1
            throw new RuntimeException('Command failed and exited with return code \'' . $cmd->getCode() . '\'');
50
        }
51
52 1
        $formatted = $formatter !== null ? $formatter->format($cmd->getStdOutAsArray()) : [];
53 1
        $result    = new Result($cmd, $formatted);
54
55 1
        return $result;
56
    }
57
}
58