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

CollectCommand::initCollectors()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 13
Bugs 0 Features 0
Metric Value
cc 1
eloc 30
c 13
b 0
f 0
nc 1
nop 0
dl 0
loc 56
rs 9.44

How to fix   Long Method   

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\InventorioReporter;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class CollectCommand extends CollectorCommand
16
{
17
    protected static $defaultName = 'collect';
18
    protected static $defaultDescription = 'Collect metrics for Inventorio';
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
        $debugMode = $input->getOption('debug');
29
30
        if (!$this->isInitialized()) {
31
            $output->writeln('<error>System was not initialized. Please run inventorio init.</error>');
32
            return Command::FAILURE;
33
        }
34
35
        $this->initCollectors();
36
37
        $inventory = [];
38
39
        $client = new \Startwind\Inventorio\Util\Client(new Client());
40
41
        foreach ($this->collectors as $collector) {
42
            if ($collector instanceof InventoryAwareCollector) {
43
                $collector->setInventory($inventory);
44
            }
45
            if ($collector instanceof ClientAwareCollector) {
46
                $collector->setClient($client);
47
            }
48
49
            if ($debugMode) $start = time();
50
            $collected = $collector->collect();
51
            if ($collected) {
52
                $inventory[$collector->getIdentifier()] = $collected;
53
            } else {
54
                $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE;
55
            }
56
            if ($debugMode) {
57
                $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...
58
            }
59
        }
60
61
        if ($debugMode) {
62
            $output->writeln('DEBUG: collection result:');
63
            $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT));
64
        }
65
66
        Memory::getInstance()->setCollection($inventory);
67
68
        $reporter = new InventorioReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId());
69
70
        try {
71
            $reporter->report($inventory);
72
        } catch (Exception $exception) {
73
            $output->writeln('<error>                           ');
74
            $output->writeln('  Unable to run reporter.  ');
75
            $output->writeln('                           </error>');
76
            $output->writeln('');
77
            $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>');
78
            return Command::FAILURE;
79
        }
80
81
        return Command::SUCCESS;
82
    }
83
}
84