ProcessRunnerCommand::resolveContext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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