Completed
Push — master ( b5231e...2792e5 )
by Christian
02:36
created

Run   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 4
dl 0
loc 78
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 9 1
B execute() 0 44 6
1
<?php
2
3
namespace uuf6429\ElderBrother\Console\Command;
4
5
use Symfony\Component\Console\Command\Command;
6
use Symfony\Component\Console\Helper\ProgressBar;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use uuf6429\ElderBrother\Config;
10
use uuf6429\ElderBrother\Exception\RecoverableException;
11
12
class Run extends Command
13
{
14
    /**
15
     * @var Config
16
     */
17
    protected $config;
18
19
    /**
20
     * @param Config $config
21
     */
22
    public function __construct(Config $config)
23
    {
24
        parent::__construct(null);
25
26
        $this->config = $config;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    protected function configure()
33
    {
34
        $this
35
            ->setName('run')
36
            ->setDescription('Runs configured actions.')
37
            ->setHelp('This command runs all actions defined in configuration.')
38
        ;
39
        // TODO add argument to specify which even to trigger
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function execute(InputInterface $input, OutputInterface $output)
46
    {
47
        if ($output->isDebug()) {
48
            $output->writeln('<info>Running from:</info> ' . PROJECT_ROOT);
49
        }
50
51
        if (extension_loaded('xdebug')) {
52
            $output->writeln(
53
                sprintf(
54
                    '<bg=yellow;fg=black;>%s</>',
55
                    'Xdebug is enabled; performance will likely be affected.'
56
                )
57
            );
58
        }
59
60
        $event = ''; // TODO get from param
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
61
62
        $actions = $this->config->get($event);
63
64
        if (!empty($actions)) {
65
            // See https://github.com/symfony/symfony/pull/10356 for multiple bars
66
            $progress = new ProgressBar($output, count($actions));
67
            $progress->start();
68
            $output->write("\n");
69
70
            foreach ($actions as $action) {
71
                $output->write("\033[1A");
72
                $progress->setMessage('Running "' . $action->getName() . '".');
73
                $progress->advance();
74
                $output->write("\n");
75
76
                try {
77
                    $action->execute($input, $output);
78
                } catch (RecoverableException $ex) {
79
                    $this->getApplication()->renderException($ex, $output); // TODO customize this for recoverable exceptions
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
80
                }
81
            }
82
83
            $progress->finish();
84
            $output->writeln(['', 'FINISHED']);
85
        } else {
86
            $output->writeln('<info>No actions have been set up yet!</info>');
87
        }
88
    }
89
}
90