Passed
Push — master ( c9bbee...06663f )
by Nils
02:53
created

WebserverUtil::extractDocumentRootByKey()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 15
rs 10
1
<?php
2
3
namespace Startwind\Inventorio\Util;
4
5
use Startwind\Inventorio\Collector\Application\WebServer\Apache\ApacheServerNameCollector;
6
use Startwind\Inventorio\Collector\Application\WebServer\Nginx\NginxServerNameCollector;
7
8
abstract class WebserverUtil
9
{
10
    public static function extractDocumentRoots(array $inventory): array
11
    {
12
        $documentRoots = self::extractDocumentRootByKey($inventory, ApacheServerNameCollector::COLLECTION_IDENTIFIER);
13
        $documentRoots = array_merge($documentRoots, self::extractDocumentRootByKey($inventory, NginxServerNameCollector::COLLECTION_IDENTIFIER));
14
15
        return array_unique($documentRoots);
16
    }
17
18
    private static function extractDocumentRootByKey(array $inventory, string $key): array
19
    {
20
        if (!array_key_exists($key, $inventory)
21
            || !is_array($inventory[$key])
22
        ) return [];
23
24
        $configs = $inventory[$key];
25
26
        $documentRoots = [];
27
28
        foreach ($configs as $config) {
29
            $documentRoots[$config[ApacheServerNameCollector::FIELD_SERVER_NAME]] = $config[ApacheServerNameCollector::FIELD_DOCUMENT_ROOT];
30
        }
31
32
        return $documentRoots;
33
    }
34
}