Completed
Push — master ( 65a442...e3692b )
by Sebastian
08:39
created

Simple::run()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 7
nc 3
nop 2
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\Command\Runner;
11
12
use RuntimeException;
13
use SebastianFeldmann\Cli\Command;
14
use SebastianFeldmann\Cli\Command\Runner;
15
use SebastianFeldmann\Cli\Command\OutputFormatter;
16
use SebastianFeldmann\Cli\Processor;
17
18
class Simple implements Runner
19
{
20
    /**
21
     * Class handling system calls.
22
     *
23
     * @var \SebastianFeldmann\Cli\Processor
24
     */
25
    private $processor;
26
27
    /**
28
     * Exec constructor.
29
     *
30
     * @param \SebastianFeldmann\Cli\Processor $processor
31
     */
32
    public function __construct(Processor $processor = null)
33
    {
34
        $this->processor = $processor !== null ? $processor : new Processor\ProcOpen();
35
    }
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\Command\Runner\Result
43
     */
44
    public function run(Command $command, OutputFormatter $formatter = null) : Result
45
    {
46
        $cmd = $this->processor->run($command->getCommand());
47
48
        if (!$cmd->isSuccessful()) {
49
            throw new RuntimeException('Command failed and exited with return code \'' . $cmd->getCode() . '\'');
50
        }
51
52
        $formatted = $formatter !== null ? $formatter->format($cmd->getStdOutAsArray()) : [];
53
        $result    = new Result($cmd, $formatted);
54
55
        return $result;
56
    }
57
}
58