DaemonCommand::getInterval()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 3
nop 0
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Startwind\Inventorio\Command;
4
5
use Monolog\Handler\StreamHandler;
6
use Monolog\Logger;
7
use Startwind\Inventorio\Metrics\Collector\Collector;
8
use Startwind\Inventorio\Metrics\Memory\Memory;
9
use Startwind\Inventorio\Metrics\Reporter\InventorioCloudReporter;
10
use Startwind\Inventorio\Remote\RemoteConnect;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
14
class DaemonCommand extends InventorioCommand
15
{
16
    protected static $defaultName = 'daemon';
17
    protected static $defaultDescription = 'Start long running daemon';
18
19
    private array $intervals = [
20
        'default' => 60 * 60 * 1, // 1 hour
21
        'remote' => 10, // 10 seconds
22
        'smartCare' => 10, // 10 seconds
23
        'collect' => 5 * 60 // 5 minutes
24
    ];
25
26
    /**
27
     * @inheritDoc
28
     */
29
    protected function execute(InputInterface $input, OutputInterface $output): int
30
    {
31
        $this->initConfiguration($input->getOption('configFile'));
32
33
        $isDebug = $input->getOption('debug');
34
35
        if ($isDebug) {
36
            $logger = new Logger('name');
37
            $logger->pushHandler(new StreamHandler('daemon.log', Logger::DEBUG));
38
        }
39
40
        $lastRun = [
41
            'default' => time() - (24 * 60 * 60),
42
            'remote' => time() - (24 * 60 * 60),
43
            'collect' => time() - (24 * 60 * 60),
44
        ];
45
46
        $serverId = $this->getServerId();
47
        $memory = Memory::getInstance();
48
49
        $remoteConnect = new RemoteConnect(
50
            $this->config->getInventorioServer(),
51
            $serverId,
52
            $this->config->getCommands(),
53
            $this->config->getSecret()
54
        );
55
56
        $remoteEnabled = $this->isRemoteEnabled();
57
        $collectEnabled = $this->isCollectEnabled();
58
        $smartCareEnabled = $this->isSmartCareEnabled();
59
60
        if ($collectEnabled) {
61
            $collectReporter = new InventorioCloudReporter();
62
            $collectCollector = new Collector();
63
        }
64
65
        while (true) {
66
            if ($lastRun['default'] <= time() - $this->intervals['default']) {
67
                $this->getApplication()->find('collect')->run($input, $output);
68
                $lastRun['default'] = time();
69
            }
70
71
            if ($remoteEnabled || $smartCareEnabled) {
72
                if ($lastRun['remote'] <= time() - $this->intervals['remote']) {
73
                    $result = $remoteConnect->run($remoteEnabled, $smartCareEnabled);
74
                    if ($isDebug && $result) {
75
                        $logger->debug('Running command: ' . $result);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $logger does not seem to be defined for all execution paths leading up to this point.
Loading history...
76
                    }
77
                    $lastRun['remote'] = time();
78
                }
79
            }
80
81
            if ($collectEnabled) {
82
                if ($lastRun['collect'] <= time() - $this->intervals['collect']) {
83
                    $dataset = $collectCollector->collect();
0 ignored issues
show
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...
84
                    $memory->addDataSet($dataset);
85
                    $collectReporter->report($serverId, $dataset);
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...
86
                    $lastRun['collect'] = time();
87
                }
88
            }
89
90
            sleep($this->getInterval());
91
        }
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...
92
    }
93
94
    private function getInterval(): int
95
    {
96
        if ($this->isRemoteEnabled() || $this->isSmartCareEnabled()) {
97
            return $this->intervals['remote'];
98
        }
99
100
        if ($this->isCollectEnabled()) {
101
            return $this->intervals['collect'];
102
        }
103
104
        return $this->intervals['default'];
105
    }
106
}
107