Completed
Pull Request — master (#115)
by Sascha
03:53
created

HealthCheckCommand::execute()   D

Complexity

Conditions 10
Paths 58

Size

Total Lines 46
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 9
Bugs 1 Features 1
Metric Value
c 9
b 1
f 1
dl 0
loc 46
rs 4.9831
cc 10
eloc 27
nc 58
nop 2

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Component\Console\Input\InputArgument;
9
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
10
11
class HealthCheckCommand extends ContainerAwareCommand
12
{
13
    protected function configure()
14
    {
15
        $this
16
            ->setName('monitor:health')
17
            ->setDescription('Runs Health Checks')
18
            ->setDefinition(array(
19
                new InputArgument(
20
                    'checkName',
21
                    InputArgument::OPTIONAL,
22
                    'The name of the service to be used to perform the health check.'
23
                ),
24
                new InputOption(
25
                    'reporter',
26
                    null,
27
                    InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
28
                    'Additional reporters to run.',
29
                    array()
30
                ),
31
                new InputOption(
32
                    'nagios',
33
                    null,
34
                    InputOption::VALUE_NONE,
35
                    'Suitable for using as a nagios NRPE command.'
36
                ),
37
                new InputOption('group', 'g', InputOption::VALUE_REQUIRED, 'List checks for given group'),
38
            ));
39
    }
40
41
    /**
42
     * @param InputInterface  $input
43
     * @param OutputInterface $output
44
     *
45
     * @return int
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $checkName = $input->getArgument('checkName');
50
        $group = $input->getOption('group');
51
52
        if (is_null($group)) {
53
            $group = $this->getContainer()->getParameter('liip_monitor.default_group');
54
        }
55
56
        $runnerServiceId = 'liip_monitor.runner_'.$group;
57
58
        if (!$this->getContainer()->has($runnerServiceId)) {
59
            $output->writeln('<error>No such group.</error>');
60
61
            return 1;
62
        }
63
64
        $runner = $this->getContainer()->get('liip_monitor.runner_'.$group);
65
66
        if ($input->getOption('nagios')) {
67
            $reporter = $this->getContainer()->get('liip_monitor.helper.raw_console_reporter');
68
        } else {
69
            $reporter = $this->getContainer()->get('liip_monitor.helper.console_reporter');
70
        }
71
72
        $runner->addReporter($reporter);
73
        $runner->useAdditionalReporters($input->getOption('reporter'));
74
75
        if (0 === count($runner->getChecks())) {
76
            $output->writeln('<error>No checks configured.</error>');
77
        }
78
79
        /** @var \ZendDiagnostics\Result\Collection $results */
80
        $results = $runner->run($checkName);
81
        if ($input->getOption('nagios')) {
82
            if ($results->getUnknownCount()) {
83
                return 3;
84
            } elseif ($results->getFailureCount()) {
85
                return 2;
86
            } elseif ($results->getWarningCount()) {
87
                return 1;
88
            }
89
        }
90
91
        return $results->getFailureCount() ? 1 : 0;
92
    }
93
}
94