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

ListChecksCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

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