Passed
Push — master ( f1175e...1a1d56 )
by Nils
03:05
created

GradeCommand::execute()   B

Complexity

Conditions 10
Paths 133

Size

Total Lines 58
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 36
c 1
b 0
f 0
nc 133
nop 2
dl 0
loc 58
rs 7.3916

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 Startwind\Inventorio\Command;
4
5
use Exception;
6
use GuzzleHttp\Client;
7
use Startwind\Inventorio\Collector\ClientAwareCollector;
8
use Startwind\Inventorio\Collector\InventoryAwareCollector;
9
use Startwind\Inventorio\Metrics\Memory\Memory;
10
use Startwind\Inventorio\Reporter\InventorioGradeReporter;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class GradeCommand extends CollectorCommand
16
{
17
    protected static $defaultName = 'grade';
18
    protected static $defaultDescription = 'Grade this server';
19
20
    private const NOT_APPLICABLE = 'not applicable';
21
22
    /**
23
     * @inheritDoc
24
     */
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        $this->initConfiguration($input->getOption('configFile'));
28
29
        $debugMode = $input->getOption('debug');
30
31
        if (!$this->isInitialized()) {
32
            $output->writeln('<error>System was not initialized. Please run inventorio init.</error>');
33
            return Command::FAILURE;
34
        }
35
36
        $this->initCollectors();
37
38
        $inventory = [];
39
40
        $client = new \Startwind\Inventorio\Util\Client(new Client());
41
42
        foreach ($this->collectors as $collector) {
43
            if ($collector instanceof InventoryAwareCollector) {
44
                $collector->setInventory($inventory);
45
            }
46
            if ($collector instanceof ClientAwareCollector) {
47
                $collector->setClient($client);
48
            }
49
50
            if ($debugMode) $start = time();
51
            $collected = $collector->collect();
52
            if ($collected) {
53
                $inventory[$collector->getIdentifier()] = $collected;
54
            } else {
55
                $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE;
56
            }
57
            if ($debugMode) {
58
                $output->writeln('DEBUG: running ' . $collector->getIdentifier() . ' took ' . time() - $start . ' seconds');
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $start does not seem to be defined for all execution paths leading up to this point.
Loading history...
59
            }
60
        }
61
62
        if ($debugMode) {
63
            $output->writeln('DEBUG: collection result:');
64
            $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT));
65
        }
66
67
        Memory::getInstance()->setCollection($inventory);
68
69
        $reporter = new InventorioGradeReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId());
70
71
        try {
72
            $reporter->report($inventory);
73
        } catch (Exception $exception) {
74
            $output->writeln('<error>                           ');
75
            $output->writeln('  Unable to run reporter.  ');
76
            $output->writeln('                           </error>');
77
            $output->writeln('');
78
            $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>');
79
            return Command::FAILURE;
80
        }
81
82
        return Command::SUCCESS;
83
    }
84
}
85