Issues (45)

src/Command/CollectCommand.php (1 issue)

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
        $debugFile = $input->getOption('debugFile');
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
        if (!$debugFile) {
37
            $this->initCollectors($debugMode);
38
            $inventory = [];
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
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
            Memory::getInstance()->setCollection($inventory);
63
        } else {
64
            $inventory = json_decode(file_get_contents(__DIR__ . '/../../debug/debug.json'), true);
65
        }
66
67
        if ($debugMode) {
68
            $output->writeln('DEBUG: collection result:');
69
            $output->writeln(json_encode($inventory, JSON_PRETTY_PRINT));
70
        }
71
72
        $reporter = new InventorioReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId());
73
74
        try {
75
            $reporter->report($inventory);
76
        } catch (Exception $exception) {
77
            $output->writeln('<error>                           ');
78
            $output->writeln('  Unable to run reporter.  ');
79
            $output->writeln('                           </error>');
80
            $output->writeln('');
81
            $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>');
82
            return Command::FAILURE;
83
        }
84
85
        return Command::SUCCESS;
86
    }
87
}
88