Failed Conditions
Pull Request — master (#8)
by jean
02:35
created

ProcessRunnerCommand::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 6
nop 2
dl 0
loc 34
rs 8.5806
c 0
b 0
f 0
1
<?php
2
3
namespace Darkilliant\ProcessBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Style\SymfonyStyle;
11
use Darkilliant\ProcessBundle\Runner\StepRunner;
12
13
/**
14
 * @internal
15
 * Class ProcessRunnerCommand
16
 *
17
 * @codeCoverageIgnore
18
 */
19
class ProcessRunnerCommand extends ContainerAwareCommand
20
{
21
    public function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        if ($input->getOption('force-color')) {
24
            $output->getFormatter()->setDecorated(true);
25
        }
26
27
        $stepRunner = $this->getContainer()->get(StepRunner::class);
28
29
        $outputHelper = new SymfonyStyle($input, $output);
30
31
        $processList = $input->getArgument('process');
32
33
        foreach ($processList as $processName) {
34
            $data = [];
35
            if ($input->getOption('input-from-stdin')) {
36
                $body = stream_get_contents(STDIN);
37
                $data = json_decode($body, true);
38
            }
39
40
            $outputHelper->section(
41
                sprintf(
42
                    '<info>Launch process %s</info>',
43
                    $processName
44
                )
45
            );
46
47
            $stepRunner->run(
48
                $stepRunner->buildConfigurationProcess($processName),
49
                $this->resolveContext($input->getOption('context')),
50
                $data,
51
                $input->getOption('dry-run')
52
            );
53
54
            $outputHelper->newLine();
55
        }
56
    }
57
58
    protected function configure()
59
    {
60
        $this->setName('process:run')
61
            ->addOption('context', 'c', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'context', [])
62
            ->addOption('input-from-stdin', null, InputOption::VALUE_NONE, 'enable data pass in stdin with json body')
63
            ->addOption('force-color', null, InputOption::VALUE_NONE, 'force use color when not autodetect support')
64
            ->addOption('dry-run', null, InputOption::VALUE_NONE, 'dry run')
65
            ->addArgument('process', InputArgument::IS_ARRAY, 'process');
66
    }
67
68
    private function resolveContext(array $context)
69
    {
70
        $contextResolved = [];
71
72
        // Use context option
73
        foreach ($context as $keyValue) {
74
            list($key, $value) = explode('=', $keyValue);
75
76
            $contextResolved[$key] = $value;
77
        }
78
79
        return $contextResolved;
80
    }
81
}
82