Failed Conditions
Push — 0.3 ( 530096...907796 )
by jean
04:25 queued 10s
created

ProcessRunnerCommand::execute()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 19
nc 6
nop 2
dl 0
loc 33
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
            );
52
53
            $outputHelper->newLine();
54
        }
55
    }
56
57
    protected function configure()
58
    {
59
        $this->setName('process:run')
60
            ->addOption('context', 'c', InputOption::VALUE_IS_ARRAY | InputOption::VALUE_REQUIRED, 'context', [])
61
            ->addOption('input-from-stdin', null, InputOption::VALUE_NONE, 'enable data pass in stdin with json body')
62
            ->addOption('force-color', null, InputOption::VALUE_NONE, 'force use color when not autodetect support')
63
            ->addArgument('process', InputArgument::IS_ARRAY, 'process');
64
    }
65
66
    private function resolveContext(array $context)
67
    {
68
        $contextResolved = [];
69
70
        // Use context option
71
        foreach ($context as $keyValue) {
72
            list($key, $value) = explode('=', $keyValue);
73
74
            $contextResolved[$key] = $value;
75
        }
76
77
        return $contextResolved;
78
    }
79
}
80