Passed
Push — master ( 3cd5ab...f863cd )
by Nils
02:29
created

DaemonCommand::execute()   B

Complexity

Conditions 8
Paths 38

Size

Total Lines 50
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 8
eloc 30
nc 38
nop 2
dl 0
loc 50
rs 8.1954
c 3
b 0
f 0
1
<?php
2
3
namespace Startwind\Inventorio\Command;
4
5
use Startwind\Inventorio\Data\Collector\Collector;
6
use Startwind\Inventorio\Data\Reporter\InventorioCloudReporter;
7
use Startwind\Inventorio\Remote\RemoteConnect;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
class DaemonCommand extends InventorioCommand
12
{
13
    protected static $defaultName = 'daemon';
14
    protected static $defaultDescription = 'Start long running daemon';
15
16
    private array $intervals = [
17
        'default' => 60 * 60,
18
        'remote' => 10,
19
        'collect' => 5 * 60
20
    ];
21
22
    /**
23
     * @inheritDoc
24
     */
25
    protected function execute(InputInterface $input, OutputInterface $output): int
26
    {
27
        $this->initConfiguration($input->getOption('configFile'));
28
29
        $lastRun = [
30
            'default' => time() - 7200,
31
            'remote' => time() - 7200,
32
            'collect' => time() - 7200,
33
        ];
34
35
        $serverId = $this->getServerId();
36
37
        $remoteConnect = new RemoteConnect(
38
            $this->config->getInventorioServer(),
39
            $serverId,
40
            $this->config->getCommands(),
41
            $this->config->getSecret()
42
        );
43
44
        $remoteEnabled = $this->isRemoteEnabled();
45
        $collectEnabled = $this->isCollectEnabled();
46
47
        if ($collectEnabled) {
48
            $collectReporter = new InventorioCloudReporter();
49
            $collectCollector = new Collector();
50
        }
51
52
        var_dump($collectEnabled);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($collectEnabled) looks like debug code. Are you sure you do not want to remove it?
Loading history...
53
54
        while (true) {
55
            if ($lastRun['default'] < time() - $this->intervals['default']) {
56
                $this->getApplication()->find('collect')->run($input, $output);
57
                $lastRun['default'] = time();
58
            }
59
60
            if ($remoteEnabled) {
61
                if ($lastRun['remote'] < time() - $this->intervals['remote']) {
62
                    $remoteConnect->run();
63
                    $lastRun['remote'] = time();
64
                }
65
            }
66
67
            if ($collectEnabled) {
68
                if ($lastRun['collect'] < time() - $this->intervals['collect']) {
69
                    $collectReporter->report($serverId, $collectCollector->collect());
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collectReporter does not seem to be defined for all execution paths leading up to this point.
Loading history...
Comprehensibility Best Practice introduced by
The variable $collectCollector does not seem to be defined for all execution paths leading up to this point.
Loading history...
70
                    $lastRun['collect'] = time();
71
                }
72
            }
73
74
            sleep($this->getInterval());
75
        }
0 ignored issues
show
Bug Best Practice introduced by
In this branch, the function will implicitly return null which is incompatible with the type-hinted return integer. Consider adding a return statement or allowing null as return value.

For hinted functions/methods where all return statements with the correct type are only reachable via conditions, ?null? gets implicitly returned which may be incompatible with the hinted type. Let?s take a look at an example:

interface ReturnsInt {
    public function returnsIntHinted(): int;
}

class MyClass implements ReturnsInt {
    public function returnsIntHinted(): int
    {
        if (foo()) {
            return 123;
        }
        // here: null is implicitly returned
    }
}
Loading history...
76
    }
77
78
    private function getInterval(): int
79
    {
80
        if ($this->isRemoteEnabled()) {
81
            return $this->intervals['remote'];
82
        }
83
84
        if ($this->isCollectEnabled()) {
85
            return $this->intervals['collect'];
86
        }
87
88
        return $this->intervals['default'];
89
    }
90
}
91