Completed
Pull Request — develop (#640)
by Narcotic
08:00 queued 01:02
created

CommandRunner   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 114
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 67.19%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 114
ccs 43
cts 64
cp 0.6719
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
D getCmd() 0 33 9
B executeCommand() 0 45 3
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
use Symfony\Component\Process\PhpExecutableFinder;
12
13
/**
14
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
15
 * @license  https://opensource.org/licenses/MIT MIT License
16
 * @link     http://swisscom.ch
17
 */
18
class CommandRunner
19
{
20
    /** @var Process */
21
    private $process;
22
    /** @var KernelInterface */
23
    private $kernel;
24
25
    /**
26
     * @param KernelInterface $kernel  Application kernel
27
     * @param Process         $process Process component
28
     */
29 6
    public function __construct(KernelInterface $kernel, Process $process)
30
    {
31 6
        $this->process = $process;
32 6
        $this->kernel = $kernel;
33 6
    }
34
35
36
    /**
37
     * Executes a app/console command
38
     *
39
     * @param array           $args    Arguments
40
     * @param OutputInterface $output  Output
41
     * @param string          $message Message to be shown on error.
42
     *
43
     * @return integer|null Exit code
44
     */
45 2
    public function executeCommand(array $args, OutputInterface $output, $message)
46
    {
47 2
        $name = $args[0];
48 2
        $cmd = $this->getCmd($args);
49
50 2
        $output->writeln('');
51 2
        $output->writeln(
52 2
            sprintf(
53 2
                '<info>Running %s</info>',
54 1
                $name
55 1
            )
56 1
        );
57
58 2
        $output->writeln(
59 2
            sprintf(
60 2
                '<comment>%s</comment>',
61 1
                $cmd
62 1
            )
63 1
        );
64
65 2
        $this->process->setCommandLine($cmd);
66 2
        $this->process->run(
67 1
            function ($type, $buffer) use ($output, $cmd) {
68
                if (Process::ERR === $type) {
69
                    $output->writeln(
70
                        sprintf(
71
                            '<error>%s</error>',
72
                            $buffer
73
                        )
74
                    );
75
                } else {
76
                    $output->writeln(
77
                        sprintf(
78
                            '<comment>%s</comment>',
79
                            $buffer
80
                        )
81
                    );
82
                }
83 1
            }
84 1
        );
85
86 2
        if (!$this->process->isSuccessful()) {
87 2
            throw new \RuntimeException($message . '<error>' . $this->process->getErrorOutput() . '</error>');
88
        }
89
    }
90
91
    /**
92
     * get subcommand
93
     *
94
     * @param array $args args
95
     *
96
     * @return string
97
     */
98 2
    private function getCmd(array $args)
99
    {
100
        // get path to console from kernel..
101 2
        $consolePath = $this->kernel->getRootDir() . '/console';
102
103
        // this code was copied from Symfony\Component\Process\PhpProcess and deserves a cleanup
104 2
        $executableFinder = new PhpExecutableFinder();
105 2
        if (false === $php = $executableFinder->find()) {
106
            $php = null;
107
        }
108 2
        if ('\\' !== DIRECTORY_SEPARATOR && null !== $php) {
109
            // exec is mandatory to deal with sending a signal to the process
110
            // see https://github.com/symfony/symfony/issues/5030 about prepending
111
            // command with exec
112 2
            $php = 'exec '.$php;
113 1
        }
114
115 2
        $cmd = $php.' '.$consolePath.' -n ';
116
117 2
        foreach ($args as $key => $val) {
118 2
            if (strlen($key) > 1) {
119
                $cmd .= ' ' . $key;
120
            }
121 2
            if (strlen($key) > 1 && !is_null($val)) {
122
                $cmd .= '=';
123
            }
124 2
            if (strlen($val) > 1) {
125 2
                $cmd .= escapeshellarg($val);
126 1
            }
127 1
        }
128
129 2
        return $cmd;
130
    }
131
}
132