ApacheServerNameCollector   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 44
c 5
b 0
f 0
dl 0
loc 89
rs 10
wmc 15

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getIdentifier() 0 3 1
A collect() 0 23 5
A getAllConfigurations() 0 14 3
A extractServerData() 0 33 6
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Application\WebServer\Apache;
4
5
use Startwind\Inventorio\Collector\Collector;
6
use Startwind\Inventorio\Exec\File;
7
use Startwind\Inventorio\Exec\Runner;
8
9
class ApacheServerNameCollector implements Collector
10
{
11
    private const CONFIG_DIRECTORY = '/etc/apache2/sites-enabled';
12
13
    public const COLLECTION_IDENTIFIER = 'ApacheServerName';
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
        if (count($configurations) == 0) {
35
            return [];
36
        }
37
38
        foreach ($configurations as $configuration) {
39
            $config = $this->extractServerData($configuration);
40
            if ($config['serverName']) {
41
                $result[$config['serverName']] = $config;
42
            }
43
        }
44
45
        return $result;
46
    }
47
48
    private function extractServerData(string $vhostFile): array
49
    {
50
        $lines = File::getInstance()->getContents($vhostFile, true);
51
52
        $serverName = '';
53
        $aliases = [];
54
        $documentRoot = '';
55
56
        foreach ($lines as $line) {
57
            $line = trim($line);
58
59
            if (preg_match('/^\s*(#|\/\/)/', $line)) {
60
                continue;
61
            }
62
63
            if (preg_match('/^ServerName\s+([^\s]+)/i', $line, $match)) {
64
                $serverName = $match[1];
65
            }
66
67
            if (preg_match('/^ServerAlias\s+(.+)/i', $line, $match)) {
68
                $aliases[] = preg_split('/\s+/', trim($match[1]));
69
            }
70
71
            if (preg_match('/DocumentRoot\s+(.+)/', $line, $matches)) {
72
                $documentRoot = trim($matches[1]);
73
            }
74
        }
75
76
        return [
77
            self::FIELD_SERVER_NAME => $serverName,
78
            self::FIELD_DOCUMENT_ROOT => $documentRoot,
79
            'vhostFile' => $vhostFile,
80
            'aliases' => $aliases
81
        ];
82
    }
83
84
    private function getAllConfigurations(string $vhostDir): array
85
    {
86
        $fileHandler = File::getInstance();
87
88
        $files = $fileHandler->scanDir($vhostDir);
89
        $configurations = [];
90
91
        foreach ($files as $file) {
92
            if (pathinfo($file, PATHINFO_EXTENSION) === 'conf') {
93
                $configurations[] = $vhostDir . '/' . $file;
94
            }
95
        }
96
97
        return $configurations;
98
    }
99
}
100