Passed
Push — master ( ae6bea...b24e8f )
by Nils
02:39
created

LogrotateCollector::getLogFileStatus()   C

Complexity

Conditions 12
Paths 72

Size

Total Lines 61
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 35
c 1
b 0
f 0
nc 72
nop 0
dl 0
loc 61
rs 6.9666

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Startwind\Inventorio\Collector\System\Logs;
4
5
use Startwind\Inventorio\Collector\Collector;
6
7
class LogrotateCollector implements Collector
8
{
9
    public function getIdentifier(): string
10
    {
11
        return 'SystemLogLogrotate';
12
    }
13
14
    public function collect(): array
15
    {
16
        return [
17
            'logfiles' => $this->getLogFileStatus()
18
        ];
19
    }
20
21
    private function getLogFileStatus(): array
22
    {
23
        $searchPath = '/var/log';
24
        $logrotateConfs = ['/etc/logrotate.conf', ...glob('/etc/logrotate.d/*')];
25
26
        // Step 1: Find all .log files under /var/log
27
        $allLogs = [];
28
        $iterator = new \RecursiveIteratorIterator(
29
            new \RecursiveDirectoryIterator($searchPath, \FilesystemIterator::SKIP_DOTS)
30
        );
31
32
        foreach ($iterator as $file) {
33
            if (preg_match('/\.log$/', $file->getFilename())) {
34
                $realPath = realpath($file->getPathname());
35
                if ($realPath !== false && is_file($realPath)) {
36
                    $allLogs[$realPath] = [
37
                        'size' => filesize($realPath),
38
                        'last_modified' => filemtime($realPath)
39
                    ];
40
                }
41
            }
42
        }
43
44
        // Step 2: Extract managed log paths from logrotate config files
45
        $managedLogs = [];
46
        foreach ($logrotateConfs as $confFile) {
47
            if (!is_readable($confFile)) continue;
0 ignored issues
show
Bug introduced by
It seems like $confFile can also be of type array; however, parameter $filename of is_readable() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

47
            if (!is_readable(/** @scrutinizer ignore-type */ $confFile)) continue;
Loading history...
48
            $lines = file($confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
0 ignored issues
show
Bug introduced by
It seems like $confFile can also be of type array; however, parameter $filename of file() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

48
            $lines = file(/** @scrutinizer ignore-type */ $confFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
Loading history...
49
            foreach ($lines as $line) {
50
                if (preg_match('#^\s*/[^\s{}]+\.log#', $line, $matches)) {
51
                    $path = realpath(trim($matches[0]));
52
                    if ($path !== false) {
53
                        $managedLogs[] = $path;
54
                    }
55
                }
56
            }
57
        }
58
59
        $managedLogs = array_unique($managedLogs);
60
61
        // Step 3: Build result arrays
62
        $result = [
63
            'managed' => [],
64
            'unmanaged' => []
65
        ];
66
67
        foreach ($allLogs as $path => $info) {
68
            $entry = [
69
                'path' => $path,
70
                'size' => $info['size'],
71
                'last_modified' => date('c', $info['last_modified']) // ISO 8601 format
72
            ];
73
74
            if (in_array($path, $managedLogs)) {
75
                $result['managed'][] = $entry;
76
            } else {
77
                $result['unmanaged'][] = $entry;
78
            }
79
        }
80
81
        return $result;
82
    }
83
}
84