HealthCheckCommand::execute()   D
last analyzed

Complexity

Conditions 14
Paths 256

Size

Total Lines 61

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 210

Importance

Changes 0
Metric Value
dl 0
loc 61
ccs 0
cts 34
cp 0
rs 4.7333
c 0
b 0
f 0
cc 14
nc 256
nop 2
crap 210

How to fix   Long Method    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 Liip\MonitorBundle\Helper\ConsoleReporter;
6
use Liip\MonitorBundle\Helper\RawConsoleReporter;
7
use Liip\MonitorBundle\Helper\RunnerManager;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputArgument;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class HealthCheckCommand extends Command
15
{
16
    private $rawReporter;
17
    private $reporter;
18
    private $runnerManager;
19
20
    public function __construct(ConsoleReporter $reporter, RawConsoleReporter $rawReporter, RunnerManager $runnerManager, $name = null)
21
    {
22
        $this->rawReporter = $rawReporter;
23
        $this->reporter = $reporter;
24
        $this->runnerManager = $runnerManager;
25
26
        parent::__construct($name);
27
    }
28
29
    protected function configure()
30
    {
31
        $this
32
            ->setName('monitor:health')
33
            ->setDescription('Runs Health Checks')
34
            ->addArgument(
35
                'checkName',
36
                InputArgument::OPTIONAL,
37
                'The name of the service to be used to perform the health check.'
38
            )
39
            ->addOption(
40
                'reporter',
41
                null,
42
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
43
                'Additional reporters to run.'
44
            )
45
            ->addOption('nagios', null, InputOption::VALUE_NONE, 'Suitable for using as a nagios NRPE command.')
46
            ->addOption(
47
                'group',
48
                'g',
49
                InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
50
                'Run Health Checks for given group'
51
            )
52
            ->addOption('all', 'a', InputOption::VALUE_NONE, 'Run Health Checks of all groups')
53
        ;
54
    }
55
56
    /**
57
     * @return int
58
     */
59
    protected function execute(InputInterface $input, OutputInterface $output)
60
    {
61
        $failureCount = 0;
62
63
        $groups = $input->getOption('group') ?: [null];
64
        $allGroups = $input->getOption('all');
65
        $checkName = $input->getArgument('checkName');
66
        $nagios = $input->getOption('nagios');
67
        $additionalReporters = $input->getOption('reporter');
68
69
        if ($nagios) {
70
            $reporter = $this->rawReporter;
71
        } else {
72
            $reporter = $this->reporter;
73
        }
74
75
        $reporter->setOutput($output);
76
77
        if ($allGroups) {
78
            $groups = $this->runnerManager->getGroups();
79
        }
80
81
        foreach ($groups as $group) {
0 ignored issues
show
Bug introduced by
The expression $groups of type string|boolean|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
82
            if (count($groups) > 1 || $allGroups) {
83
                $output->writeln(sprintf('<fg=yellow;options=bold>%s</>', $group));
84
            }
85
86
            $runner = $this->runnerManager->getRunner($group);
87
88
            if (null === $runner) {
89
                $output->writeln('<error>No such group.</error>');
90
91
                return 1;
92
            }
93
94
            $runner->addReporter($reporter);
95
            $runner->useAdditionalReporters($additionalReporters);
96
97
            if (0 === count($runner->getChecks())) {
98
                $output->writeln('<error>No checks configured.</error>');
99
            }
100
101
            $results = $runner->run($checkName);
102
103
            if ($nagios) {
104
                if ($results->getUnknownCount()) {
105
                    return 3;
106
                }
107
                if ($results->getFailureCount()) {
108
                    return 2;
109
                }
110
                if ($results->getWarningCount()) {
111
                    return 1;
112
                }
113
            }
114
115
            $failureCount += $results->getFailureCount();
116
        }
117
118
        return $failureCount > 0 ? 1 : 0;
119
    }
120
}
121