Passed
Push — master ( a385bc...b6a7ed )
by Nils
02:38
created

NginxServerNameCollector::extractServerData()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 19
c 1
b 0
f 0
nc 6
nop 1
dl 0
loc 31
rs 9.3222
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Application\WebServer\Nginx;
4
5
use Startwind\Inventorio\Collector\Collector;
6
use Startwind\Inventorio\Exec\File;
7
use Startwind\Inventorio\Exec\Runner;
8
9
class NginxServerNameCollector implements Collector
10
{
11
    private const CONFIG_DIRECTORY = '/etc/nginx/sites-enabled';
12
13
    public const COLLECTION_IDENTIFIER = 'NginxServerName';
14
15
    public const FIELD_DOCUMENT_ROOT = 'documentRoot';
16
    public const FIELD_SERVER_NAME = 'serverName';
17
18
    public function getIdentifier(): string
19
    {
20
        return self::COLLECTION_IDENTIFIER;
21
    }
22
23
    public function collect(): array
24
    {
25
        $runner = Runner::getInstance();
26
27
        if (!$runner->fileExists(self::CONFIG_DIRECTORY)) {
28
            return [];
29
        }
30
31
        $configurations = $this->getAllConfigurations(self::CONFIG_DIRECTORY);
32
        $result = [];
33
34
        foreach ($configurations as $configuration) {
35
            $config = $this->extractServerData($configuration);
36
            if (!empty($config['serverName'])) {
37
                $result[$config['serverName']] = $config;
38
            }
39
        }
40
41
        return array_values($result);
42
    }
43
44
    private function extractServerData(string $configFile): array
45
    {
46
        $lines = File::getInstance()->getContents($configFile, true);
47
48
        $serverName = '';
49
        $documentRoot = '';
50
        $aliases = [];
51
52
        foreach ($lines as $line) {
53
            $line = trim($line);
54
55
            if (preg_match('/^\s*(#|\/\/)/', $line)) {
56
                continue;
57
            }
58
59
            if (preg_match('/^\s*server_name\s+([^;]+);/i', $line, $match)) {
60
                $names = preg_split('/\s+/', trim($match[1]));
61
                $serverName = $names[0] ?? '';
62
                $aliases = array_slice($names, 1);
63
            }
64
65
            if (preg_match('/^\s*root\s+([^;]+);/i', $line, $match)) {
66
                $documentRoot = trim($match[1]);
67
            }
68
        }
69
70
        return [
71
            self::FIELD_SERVER_NAME => $serverName,
72
            self::FIELD_DOCUMENT_ROOT => $documentRoot,
73
            'configFile' => $configFile,
74
            'aliases' => $aliases
75
        ];
76
    }
77
78
    private function getAllConfigurations(string $vhostDir): array
79
    {
80
        $fileHandler = File::getInstance();
81
        $files = $fileHandler->scanDir($vhostDir);
82
        $configurations = [];
83
84
        foreach ($files as $file) {
85
            if (pathinfo($file, PATHINFO_EXTENSION) === 'conf') {
86
                $configurations[] = $vhostDir . '/' . $file;
87
            }
88
        }
89
90
        return $configurations;
91
    }
92
}