Passed
Push — master ( b6a7ed...d6f101 )
by Nils
02:45
created

NginxConfigurationCollector   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
c 1
b 0
f 0
dl 0
loc 84
rs 10
wmc 16

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getActiveNginxModules() 0 22 5
A isNginxInstalled() 0 4 1
A extractServerName() 0 10 3
A getRealConfigFilesWithServerNames() 0 28 6
A collect() 0 5 1
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Application\WebServer\Nginx;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Exec\File;
7
use Startwind\Inventorio\Exec\Runner;
8
use Symfony\Component\Console\Command\Command;
9
10
class NginxConfigurationCollector extends BasicCollector
11
{
12
    protected string $identifier = 'NginxConfiguration';
13
14
    private string $sitesEnabledPath = '/etc/nginx/sites-enabled';
15
16
    public function collect(): array
17
    {
18
        return [
19
            'modules' => $this->getActiveNginxModules(),
20
            'nonLinkedSitesEnabled' => $this->getRealConfigFilesWithServerNames()
21
        ];
22
    }
23
24
    private function getActiveNginxModules(): array
25
    {
26
        $runner = Runner::getInstance();
27
        $result = $runner->run('nginx -V 2>&1');
28
29
        if ($result->getExitCode() !== Command::SUCCESS) {
30
            return [];
31
        }
32
33
        $output = Runner::outputToArray($result->getOutput());
34
35
        $modules = [];
36
37
        foreach ($output as $line) {
38
            if (preg_match_all('/--with-(\S+)/', $line, $matches)) {
39
                foreach ($matches[1] as $module) {
40
                    $modules[] = $module;
41
                }
42
            }
43
        }
44
45
        return $modules;
46
    }
47
48
    public function isNginxInstalled(): bool
49
    {
50
        $runner = Runner::getInstance();
51
        return $runner->commandExists('nginx');
52
    }
53
54
    private function getRealConfigFilesWithServerNames(): array
55
    {
56
        if (!$this->isNginxInstalled()) {
57
            return [];
58
        }
59
60
        $fileHandler = File::getInstance();
61
62
        if (!$fileHandler->isDir($this->sitesEnabledPath)) {
63
            return [];
64
        }
65
66
        $configFiles = $fileHandler->scanDir($this->sitesEnabledPath);
67
        $results = [];
68
69
        foreach ($configFiles as $file) {
70
            $filePath = $this->sitesEnabledPath . DIRECTORY_SEPARATOR . $file;
71
72
            if ($fileHandler->isFile($filePath) && !$fileHandler->isLink($filePath)) {
73
                $serverName = $this->extractServerName($filePath);
74
                $results[] = [
75
                    'file' => $file,
76
                    'serverName' => $serverName,
77
                ];
78
            }
79
        }
80
81
        return $results;
82
    }
83
84
    private function extractServerName(string $filePath): ?string
85
    {
86
        $lines = File::getInstance()->getContents($filePath, true);
87
        foreach ($lines as $line) {
88
            if (preg_match('/^\s*server_name\s+([^;]+);/i', $line, $matches)) {
89
                $names = preg_split('/\s+/', trim($matches[1]));
90
                return $names[0] ?? null;
91
            }
92
        }
93
        return null;
94
    }
95
}