Completed
Push — master ( 269eb1...62642d )
by Lukas Kahwe
01:25
created

ListChecksCommand   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
lcom 2
cbo 5
dl 0
loc 112
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 11 1
A execute() 0 17 4
A listChecks() 0 14 2
A listAllChecks() 0 8 2
A listReporters() 0 11 3
A listGroups() 0 6 2
A doListChecks() 0 12 3
1
<?php
2
3
namespace Liip\MonitorBundle\Command;
4
5
use Liip\MonitorBundle\Helper\RunnerManager;
6
use Liip\MonitorBundle\Runner;
7
use Symfony\Component\Console\Command\Command;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Input\InputOption;
10
use Symfony\Component\Console\Output\OutputInterface;
11
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
12
13
class ListChecksCommand extends Command
14
{
15
    private $runnerManager;
16
    private $runner;
17
18
    public function __construct(RunnerManager $runnerManager, Runner $runner, $name = null)
19
    {
20
        $this->runnerManager = $runnerManager;
21
        $this->runner = $runner;
22
23
        parent::__construct($name);
24
    }
25
26
    protected function configure()
27
    {
28
        $this
29
            ->setName('monitor:list')
30
            ->setDescription('Lists Health Checks')
31
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Lists Health Checks of all groups')
32
            ->addOption('reporters', 'r', InputOption::VALUE_NONE, 'List registered additional reporters')
33
            ->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group')
34
            ->addOption('groups', 'G', InputOption::VALUE_NONE, 'List all registered groups')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        switch (true) {
41
            case $input->getOption('reporters'):
42
                $this->listReporters($output);
43
                break;
44
            case $input->getOption('all'):
45
                $this->listAllChecks($output);
46
                break;
47
            case $input->getOption('groups'):
48
                $this->listGroups($output);
49
                break;
50
            default:
51
                $this->listChecks($input, $output);
52
                break;
53
        }
54
    }
55
56
    protected function listChecks(InputInterface $input, OutputInterface $output)
57
    {
58
        $group = $input->getOption('group');
59
60
        $runner = $this->runnerManager->getRunner($group);
61
62
        if (null === $runner) {
63
            $output->writeln('<error>No such group.</error>');
64
65
            return;
66
        }
67
68
        $this->doListChecks($output, $runner);
69
    }
70
71
    /**
72
     * @param OutputInterface $output
73
     */
74
    protected function listAllChecks(OutputInterface $output)
75
    {
76
        foreach ($this->runnerManager->getRunners() as $group => $runner) {
77
            $output->writeln(sprintf('<fg=yellow;options=bold>%s</>', $group));
78
79
            $this->doListChecks($output, $runner);
80
        }
81
    }
82
83
    /**
84
     * @param OutputInterface $output
85
     */
86
    protected function listReporters(OutputInterface $output)
87
    {
88
        $reporters = $this->runner->getAdditionalReporters();
89
        if (0 === count($reporters)) {
90
            $output->writeln('<error>No additional reporters configured.</error>');
91
        }
92
93
        foreach (array_keys($reporters) as $reporter) {
94
            $output->writeln($reporter);
95
        }
96
    }
97
98
    /**
99
     * @param OutputInterface $output
100
     */
101
    protected function listGroups(OutputInterface $output)
102
    {
103
        foreach ($this->runnerManager->getGroups() as $group) {
104
            $output->writeln($group);
105
        }
106
    }
107
108
    /**
109
     * @param OutputInterface $output
110
     * @param Runner          $runner
111
     */
112
    private function doListChecks(OutputInterface $output, Runner $runner)
113
    {
114
        $checks = $runner->getChecks();
115
116
        if (0 === count($checks)) {
117
            $output->writeln('<error>No checks configured.</error>');
118
        }
119
120
        foreach ($runner->getChecks() as $alias => $check) {
121
            $output->writeln(sprintf('<info>%s</info> %s', $alias, $check->getLabel()));
122
        }
123
    }
124
}
125