Completed
Pull Request — master (#115)
by Sascha
02:24
created

ListChecksCommand::listGroups()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 6
rs 9.4286
c 2
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
namespace Liip\MonitorBundle\Command;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Input\InputOption;
7
use Symfony\Component\Console\Output\OutputInterface;
8
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9
10
class ListChecksCommand extends ContainerAwareCommand
11
{
12
    protected function configure()
13
    {
14
        $this
15
            ->setName('monitor:list')
16
            ->setDescription('Lists Health Checks')
17
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Lists Health Checks of all groups')
18
            ->addOption('reporters', 'r', InputOption::VALUE_NONE, 'List registered additional reporters')
19
            ->addOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group')
20
            ->addOption('groups', 'G', InputOption::VALUE_NONE, 'List all registered groups')
21
        ;
22
    }
23
24
    protected function execute(InputInterface $input, OutputInterface $output)
25
    {
26
        switch (true) {
27
            case $input->getOption('reporters'):
28
                $this->listReporters($output);
29
                break;
30
            case $input->getOption('all'):
31
                $this->listAllChecks($output);
32
                break;
33
            case $input->getOption('groups'):
34
                $this->listGroups($output);
35
                break;
36
            default:
37
                $this->listChecks($input, $output);
38
                break;
39
        }
40
    }
41
42
    protected function listChecks(InputInterface $input, OutputInterface $output)
43
    {
44
        $group = $input->getOption('group');
45
46
        if (is_null($group)) {
47
            $group = $this->getContainer()->getParameter('liip_monitor.default_group');
48
        }
49
50
        $runnerServiceId = 'liip_monitor.runner_'.$group;
51
52
        if (!$this->getContainer()->has($runnerServiceId)) {
53
            $output->writeln('<error>No such group.</error>');
54
55
            return;
56
        }
57
58
        $this->doListChecks($output, $runnerServiceId);
59
    }
60
61
    /**
62
     * @param OutputInterface $output
63
     */
64
    protected function listAllChecks(OutputInterface $output)
65
    {
66
        foreach ($this->getGroups() as $runnerServiceId => $group) {
67
            $output->writeln(sprintf('<fg=yellow;options=bold>%s</>', $group));
68
69
            $this->doListChecks($output, $runnerServiceId);
70
        }
71
    }
72
73
    /**
74
     * @param OutputInterface $output
75
     */
76
    protected function listReporters(OutputInterface $output)
77
    {
78
        $reporters = $this->getContainer()->get('liip_monitor.runner')->getAdditionalReporters();
79
        if (0 === count($reporters)) {
80
            $output->writeln('<error>No additional reporters configured.</error>');
81
        }
82
83
        foreach (array_keys($reporters) as $reporter) {
84
            $output->writeln($reporter);
85
        }
86
    }
87
88
    /**
89
     * @param OutputInterface $output
90
     */
91
    protected function listGroups(OutputInterface $output)
92
    {
93
        foreach ($this->getGroups() as $group) {
94
            $output->writeln($group);
95
        }
96
    }
97
98
    /**
99
     * @param OutputInterface $output
100
     * @param $runnerServiceId
101
     */
102
    private function doListChecks(OutputInterface $output, $runnerServiceId)
103
    {
104
        $runner = $this->getContainer()->get($runnerServiceId);
105
        $checks = $runner->getChecks();
106
107
        if (0 === count($checks)) {
108
            $output->writeln('<error>No checks configured.</error>');
109
        }
110
111
        foreach ($runner->getChecks() as $alias => $check) {
112
            $output->writeln(sprintf('<info>%s</info> %s', $alias, $check->getLabel()));
113
        }
114
    }
115
116
    /**
117
     * @return array key/value $serviceId/$group
118
     */
119
    private function getGroups()
120
    {
121
        $runnerServiceIds = $this->getContainer()->getParameter('liip_monitor.runners');
122
123
        $groups = array();
124
125
        foreach ($runnerServiceIds as $serviceId) {
126
            if (preg_match('/liip_monitor.runner_(.+)/', $serviceId, $matches)) {
127
                $groups[$serviceId] = $matches[1];
128
            }
129
        }
130
131
        return $groups;
132
    }
133
}
134