Failed Conditions
Pull Request — 0.3 (#20)
by jean
03:05
created

ProcessRunnerCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 48

Duplication

Lines 0
Ratio 0 %

Importance

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