CommandExecutor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
nc 1
cc 1
nop 3
1
<?php
2
3
/*
4
 * This file is part of AppName.
5
 *
6
 * (c) Monofony
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace App\Command\Installer;
13
14
use Symfony\Component\Console\Application;
15
use Symfony\Component\Console\Input\ArrayInput;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Output\NullOutput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Process\Exception\RuntimeException;
20
21
final class CommandExecutor
22
{
23
    /**
24
     * @var InputInterface
25
     */
26
    private $input;
27
28
    /**
29
     * @var OutputInterface
30
     */
31
    private $output;
32
33
    /**
34
     * @var Application
35
     */
36
    private $application;
37
38
    /**
39
     * @param InputInterface  $input
40
     * @param OutputInterface $output
41
     * @param Application     $application
42
     */
43
    public function __construct(InputInterface $input, OutputInterface $output, Application $application)
44
    {
45
        $this->input = $input;
46
        $this->output = $output;
47
        $this->application = $application;
48
    }
49
50
    /**
51
     * @param $command
52
     * @param array           $parameters
53
     * @param OutputInterface $output
54
     *
55
     * @return $this
56
     *
57
     * @throws \Exception
58
     */
59
    public function runCommand($command, $parameters = [], OutputInterface $output = null)
60
    {
61
        $parameters = array_merge(
62
            ['command' => $command],
63
            $this->getDefaultParameters(),
64
            $parameters
65
        );
66
67
        $this->application->setAutoExit(false);
68
        $exitCode = $this->application->run(new ArrayInput($parameters), $output ?: new NullOutput());
69
70
        if (1 === $exitCode) {
71
            throw new RuntimeException('This command terminated with a permission error');
72
        }
73
74
        if (0 !== $exitCode) {
75
            $this->application->setAutoExit(true);
76
77
            $errorMessage = sprintf('The command terminated with an error code: %u.', $exitCode);
78
            $this->output->writeln("<error>$errorMessage</error>");
79
            $exception = new \Exception($errorMessage, $exitCode);
80
81
            throw $exception;
82
        }
83
84
        return $this;
85
    }
86
87
    /**
88
     * Get default parameters.
89
     *
90
     * @return array
91
     */
92
    private function getDefaultParameters()
93
    {
94
        $defaultParameters = ['--no-debug' => true];
95
96
        if ($this->input->hasOption('env')) {
97
            $defaultParameters['--env'] = $this->input->hasOption('env') ? $this->input->getOption('env') : 'dev';
98
        }
99
100
        if ($this->input->hasOption('no-interaction') && true === $this->input->getOption('no-interaction')) {
101
            $defaultParameters['--no-interaction'] = true;
102
        }
103
104
        if ($this->input->hasOption('verbose') && true === $this->input->getOption('verbose')) {
105
            $defaultParameters['--verbose'] = true;
106
        }
107
108
        return $defaultParameters;
109
    }
110
}
111