Completed
Push — feature/bigfix_proxy_httploade... ( 5efe01 )
by Bastian
43:53 queued 29:53
created

CommandRunner::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1.008
Metric Value
dl 0
loc 5
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1.008
1
<?php
2
/**
3
 * Runs a command using the process component.
4
 */
5
6
namespace Graviton\GeneratorBundle;
7
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\HttpKernel\KernelInterface;
10
use Symfony\Component\Process\Process;
11
12
/**
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class CommandRunner
18
{
19
    /** @var Process */
20
    private $process;
21
    /** @var KernelInterface */
22
    private $kernel;
23
24
    /**
25
     * @param KernelInterface $kernel  Application kernel
26
     * @param Process         $process Process component
27
     */
28 2
    public function __construct(KernelInterface $kernel, Process $process)
29
    {
30 2
        $this->process = $process;
31 2
        $this->kernel = $kernel;
32 2
    }
33
34
35
    /**
36
     * Executes a app/console command
37
     *
38
     * @param array           $args    Arguments
39
     * @param OutputInterface $output  Output
40
     * @param string          $message Message to be shown on error.
41
     *
42
     * @return integer|null Exit code
43
     */
44 1
    public function executeCommand(array $args, OutputInterface $output, $message)
45
    {
46 1
        $name = $args[0];
47 1
        $cmd = $this->getCmd($args);
48
49 1
        $output->writeln('');
50 1
        $output->writeln(
51
            sprintf(
52 1
                '<info>Running %s</info>',
53
                $name
54
            )
55
        );
56
57 1
        $output->writeln(
58
            sprintf(
59 1
                '<comment>%s</comment>',
60
                $cmd
61
            )
62
        );
63
64 1
        $this->process->setCommandLine($cmd);
65 1
        $this->process->run(
66 1
            function ($type, $buffer) use ($output, $cmd) {
67
                if (Process::ERR === $type) {
68
                    $output->writeln(
69
                        sprintf(
70
                            '<error>%s</error>',
71
                            $buffer
72
                        )
73
                    );
74
                } else {
75
                    $output->writeln(
76
                        sprintf(
77
                            '<comment>%s</comment>',
78
                            $buffer
79
                        )
80
                    );
81
                }
82 1
            }
83
        );
84
85 1
        if (!$this->process->isSuccessful()) {
86 1
            throw new \RuntimeException($message . '<error>' . $this->process->getErrorOutput() . '</error>');
87
        }
88
    }
89
90
    /**
91
     * get subcommand
92
     *
93
     * @param array $args args
94
     *
95
     * @return string
96
     */
97 1
    private function getCmd(array $args)
98
    {
99
        // get path to console from kernel..
100 1
        $consolePath = $this->kernel->getRootDir() . '/console';
101
102 1
        $cmd = 'php ' . $consolePath . ' -n ';
103
104 1
        foreach ($args as $key => $val) {
105 1
            if (strlen($key) > 1) {
106
                $cmd .= ' ' . $key;
107
            }
108 1
            if (strlen($key) > 1 && !is_null($val)) {
109
                $cmd .= '=';
110
            }
111 1
            if (strlen($val) > 1) {
112 1
                $cmd .= escapeshellarg($val);
113
            }
114
        }
115
116 1
        return $cmd;
117
    }
118
}
119