Passed
Push — master ( 6bb14b...dc86bc )
by Nils
02:20
created

CollectCommand::execute()   B

Complexity

Conditions 6
Paths 11

Size

Total Lines 39
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 25
nc 11
nop 2
dl 0
loc 39
rs 8.8977
c 1
b 0
f 0
1
<?php
2
3
namespace Startwind\Inventorio\Command;
4
5
use Startwind\Inventorio\Collector\Application\Monitoring\WebProsMonitoringCollector;
6
use Startwind\Inventorio\Collector\Application\ProgrammingLanguage\PhpCollector;
7
use Startwind\Inventorio\Collector\Application\WebServer\Apache\ApacheServerNameCollector;
8
use Startwind\Inventorio\Collector\InventoryAwareCollector;
9
use Startwind\Inventorio\Collector\Hosting\HostingCompany\ASNCollector;
10
use Startwind\Inventorio\Collector\Inventorio\CommandCollector;
11
use Startwind\Inventorio\Collector\Inventorio\InventorioCollector;
12
use Startwind\Inventorio\Collector\OperatingSystem\OperatingSystemCollector;
13
use Startwind\Inventorio\Collector\Package\Brew\BrewPackageCollector;
14
use Startwind\Inventorio\Collector\Package\Dpkg\DpkgPackageCollector;
15
use Startwind\Inventorio\Collector\System\Cron\CronCollector;
16
use Startwind\Inventorio\Collector\System\General\ConfigurationCollector;
17
use Startwind\Inventorio\Collector\System\General\IpCollector;
18
use Startwind\Inventorio\Collector\System\General\UptimeCollector;
19
use Startwind\Inventorio\Collector\System\Ports\OpenPortsCollector;
20
use Startwind\Inventorio\Collector\System\UserCollector;
21
use Startwind\Inventorio\Reporter\InventorioReporter;
22
use Symfony\Component\Console\Command\Command;
23
use Symfony\Component\Console\Input\InputInterface;
24
use Symfony\Component\Console\Output\OutputInterface;
25
26
class CollectCommand extends InventorioCommand
27
{
28
    protected static $defaultName = 'collect';
29
    protected static $defaultDescription = 'Collect metrics for Inventorio';
30
31
    private const NOT_APPLICABLE = 'not applicable';
32
33
    /**
34
     * @var \Startwind\Inventorio\Collector\Collector[]
35
     */
36
    private array $collectors = [];
37
38
    /**
39
     * @inheritDoc
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43
        $this->initConfiguration($input->getOption('configFile'));
44
45
        if (!$this->isInitialized()) {
46
            $output->writeln('<error>System was not initialized. Please run inventorio init.</error>');
47
            return Command::FAILURE;
48
        }
49
50
        $this->initCollectors();
51
52
        $inventory = [];
53
54
        foreach ($this->collectors as $collector) {
55
            if($collector instanceof  InventoryAwareCollector) {
56
                $collector->setInventory($inventory);
57
            }
58
            $collected = $collector->collect();
0 ignored issues
show
Bug introduced by
The method collect() does not exist on Startwind\Inventorio\Col...InventoryAwareCollector. Since it exists in all sub-types, consider adding an abstract or default implementation to Startwind\Inventorio\Col...InventoryAwareCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            /** @scrutinizer ignore-call */ 
59
            $collected = $collector->collect();
Loading history...
59
            if ($collected) {
60
                $inventory[$collector->getIdentifier()] = $collected;
0 ignored issues
show
Bug introduced by
The method getIdentifier() does not exist on Startwind\Inventorio\Col...InventoryAwareCollector. Since it exists in all sub-types, consider adding an abstract or default implementation to Startwind\Inventorio\Col...InventoryAwareCollector. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                $inventory[$collector->/** @scrutinizer ignore-call */ getIdentifier()] = $collected;
Loading history...
61
            } else {
62
                $inventory[$collector->getIdentifier()] = self::NOT_APPLICABLE;
63
            }
64
        }
65
66
        $reporter = new InventorioReporter($output, $this->config->getInventorioServer(), $this->getServerId(), $this->getUserId());
67
68
        try {
69
            $reporter->report($inventory);
70
        } catch (\Exception $exception) {
71
            $output->writeln('<error>                           ');
72
            $output->writeln('  Unable to run reporter.  ');
73
            $output->writeln('                           </error>');
74
            $output->writeln('');
75
            $output->writeln(' <comment>Message: ' . $exception->getMessage() . '</comment>');
76
            return Command::FAILURE;
77
        }
78
79
        return Command::SUCCESS;
80
    }
81
82
    /**
83
     * Initialize all collectors.
84
     *
85
     * @todo use a config file to
86
     */
87
    private function initCollectors(): void
88
    {
89
        // Inventorio
90
        $this->collectors[] = new InventorioCollector();
91
        $this->collectors[] = new CommandCollector($this->config);
92
        // $this->collectors[] = new RandomCollector();
93
94
        // General
95
        $this->collectors[] = new OperatingSystemCollector();
96
97
        // Hosting
98
        $this->collectors[] = new ASNCollector();
99
100
        // System / General
101
        $this->collectors[] = new IpCollector();
102
        $this->collectors[] = new UptimeCollector();
103
        $this->collectors[] = new OpenPortsCollector();
104
        $this->collectors[] = new ConfigurationCollector();
105
        $this->collectors[] = new CronCollector();
106
        $this->collectors[] = new UserCollector();
107
108
        // Package Managers
109
        $this->collectors[] = new BrewPackageCollector();
110
        $this->collectors[] = new DpkgPackageCollector();
111
112
        // Application / Programming Language
113
        $this->collectors[] = new PhpCollector();
114
        $this->collectors[] = new WebProsMonitoringCollector();
115
116
        // Application / WebServer
117
        $this->collectors[] = new ApacheServerNameCollector();
118
119
        // INVENTORY AWARE
120
121
    }
122
}
123