Passed
Push — master ( 993b38...6edd3f )
by Nils
03:11
created

CronCollector::collect()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 7
eloc 20
c 3
b 0
f 0
nc 5
nop 0
dl 0
loc 37
rs 8.6666
1
<?php
2
3
namespace Startwind\Inventorio\Collector\System\Cron;
4
5
use Startwind\Inventorio\Collector\Collector;
6
use Startwind\Inventorio\Exec\Runner;
7
8
/**
9
 * This collector returns details about the operating system.
10
 *
11
 * - family: the OS family (MacOs, Linux, Windows). Please use the provided constants.
12
 * - version: the OS version
13
 *
14
 */
15
class CronCollector implements Collector
16
{
17
    protected const COLLECTION_IDENTIFIER = 'CronJobs';
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function getIdentifier(): string
23
    {
24
        return self::COLLECTION_IDENTIFIER;
25
    }
26
27
    /**
28
     * @inheritDoc
29
     */
30
    public function collect(): array
31
    {
32
        $runner = Runner::getInstance();
33
34
        if(!$runner->commandExists('crontab')) {
35
            return [];
36
        }
37
38
        $cronJobs = Runner::getInstance()->run('crontab -l 2>&1')->getOutput();
39
40
        if (strpos($cronJobs, 'no crontab for') !== false) {
41
            return [];
42
        }
43
44
        $cronJobs = explode("\n", $cronJobs);
45
46
        $cronJobsResult = [];
47
48
        foreach ($cronJobs as $cronJob) {
49
            if ($cronJob == "" || str_starts_with($cronJob, '#') || $cronJob == 'SHELL=/bin/bash') {
50
                continue;
51
            }
52
53
            $parts = preg_split('/\s+/', $cronJob, 6);
54
55
            $cronJobsResult[] = [
56
                'minute' => $parts[0],
57
                'hour' => $parts[1],
58
                'dayOfMonth' => $parts[2],
59
                'month' => $parts[3],
60
                'dayOfWeek' => $parts[4],
61
                'command' => $parts[5]
62
            ];
63
        }
64
65
66
        return ['cronjobs' => $cronJobsResult];
67
    }
68
}
69