Run   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 81
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 7
dl 0
loc 81
ccs 41
cts 44
cp 0.9318
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 10 1
C execute() 0 61 11
1
<?php
2
3
namespace uuf6429\ElderBrother\Console\Command;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use uuf6429\ElderBrother\Exception\RecoverableException;
10
11
class Run extends CommandAbstract
12
{
13
    /**
14
     * {@inheritdoc}
15
     */
16 3
    protected function configure()
17
    {
18
        $this
19 3
            ->setName('run')
20 3
            ->setDescription('Runs configured actions for an event.')
21 3
            ->setHelp('This command runs all actions defined in configuration for the specified event.')
22 3
            ->addOption('event', 'e', InputOption::VALUE_REQUIRED, 'The event whose actions will be run.')
23 3
            ->addOption('no-progress', null, InputOption::VALUE_NONE, 'Disables progress bar.')
24
        ;
25 3
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 3
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32 3
        if ($output->isDebug()) {
33
            $output->write(['Running from: <info>', PROJECT_ROOT, '</info>']);
34
            $output->writeln('');
35
        }
36
37 3
        if (extension_loaded('xdebug')) {
38 3
            $output->writeln(
39
                sprintf(
40 3
                    '<bg=yellow;fg=black;>%s</>',
41 3
                    'Xdebug is enabled; performance will likely be affected.'
42
                )
43
            );
44
        }
45
46 3
        $event = $input->getOption('event');
47 3
        $actions = $this->config->getActionsForEvent($event);
48
49 3
        if (!empty($actions)) {
50
            // See https://github.com/symfony/symfony/pull/10356 for multiple bars
51 2
            $progress = $input->hasParameterOption('no-progress')
52 2
                ? null : new ProgressBar($output);
53 2
            if ($progress) {
54 2
                $progress->setFormat(' %current%/%max% [%bar%] %percent:3s%% %message%');
55 2
                $progress->setMessage('');
56 2
                $progress->start(count($actions));
57 2
                $output->write("\n");
58
            }
59
60 2
            foreach ($actions as $action) {
61 2
                if ($progress) {
62 2
                    $output->write("\033[1A");
63 2
                    $progress->setMessage('Running <info>"' . $action->getName() . '"</info>.');
64 2
                    $progress->advance();
65 2
                    $output->write("\n");
66
                } else {
67
                    $output->writeln('Running <info>"' . $action->getName() . '"</info>...');
68
                }
69
70
                try {
71 2
                    $action->execute($input, $output);
72 1
                } catch (\Exception $ex) {
73 1
                    $this->getApplication()->renderException($ex, $output);
74 1
                    if (!($ex instanceof RecoverableException)) {
75 2
                        return 1;
76
                    }
77
                }
78
            }
79
80 1
            if ($progress) {
81 1
                $output->write("\033[1A");
82 1
                $progress->setMessage('Finished.');
83 1
                $progress->finish();
84 1
                $output->writeln(['', '']);
85
            }
86 1
            $output->writeln('Done.');
87
        }
88
89 2
        return 0;
90
    }
91
}
92