Completed
Push — develop ( 56aba0...2d5148 )
by Lucas
11s
created

CommandRunner   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 64.15%
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 102
ccs 34
cts 53
cp 0.6415
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B executeCommand() 0 45 3
B getCmd() 0 21 6
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 4
    public function __construct(KernelInterface $kernel, Process $process)
29
    {
30 4
        $this->process = $process;
31 4
        $this->kernel = $kernel;
32 4
    }
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 2
    public function executeCommand(array $args, OutputInterface $output, $message)
45
    {
46 2
        $name = $args[0];
47 2
        $cmd = $this->getCmd($args);
48
49 2
        $output->writeln('');
50 2
        $output->writeln(
51 2
            sprintf(
52 2
                '<info>Running %s</info>',
53
                $name
54 2
            )
55 2
        );
56
57 2
        $output->writeln(
58 2
            sprintf(
59 2
                '<comment>%s</comment>',
60
                $cmd
61 2
            )
62 2
        );
63
64 2
        $this->process->setCommandLine($cmd);
65 2
        $this->process->run(
66
            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
            }
83 2
        );
84
85 2
        if (!$this->process->isSuccessful()) {
86 2
            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 2
    private function getCmd(array $args)
98
    {
99
        // get path to console from kernel..
100 2
        $consolePath = $this->kernel->getRootDir() . '/console';
101
102 2
        $cmd = 'php ' . $consolePath . ' -n ';
103
104 2
        foreach ($args as $key => $val) {
105 2
            if (strlen($key) > 1) {
106
                $cmd .= ' ' . $key;
107
            }
108 2
            if (strlen($key) > 1 && !is_null($val)) {
109
                $cmd .= '=';
110
            }
111 2
            if (strlen($val) > 1) {
112 2
                $cmd .= escapeshellarg($val);
113 2
            }
114 2
        }
115
116 2
        return $cmd;
117
    }
118
}
119