ListChecksCommand   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 18
lcom 2
cbo 5
dl 0
loc 101
ccs 0
cts 57
cp 0
rs 10
c 0
b 0
f 0

8 Methods

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