Passed
Push — master ( dc86bc...a4710b )
by Nils
02:19
created

WordPressCollector::collect()   B

Complexity

Conditions 8
Paths 9

Size

Total Lines 37
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 20
c 1
b 0
f 0
nc 9
nop 0
dl 0
loc 37
rs 8.4444
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Website\WordPress;
4
5
use Startwind\Inventorio\Collector\Application\WebServer\Apache\ApacheServerNameCollector;
6
use Startwind\Inventorio\Collector\BasicCollector;
7
use Startwind\Inventorio\Collector\InventoryAwareCollector;
8
9
class WordPressCollector extends BasicCollector implements InventoryAwareCollector
10
{
11
    protected string $identifier = "WordPress";
12
13
    private array $inventory;
14
15
    public function setInventory(array $inventory): void
16
    {
17
        $this->inventory = $inventory;
18
    }
19
20
    public function collect(): array
21
    {
22
        if (!array_key_exists(ApacheServerNameCollector::COLLECTION_IDENTIFIER, $this->inventory)
23
            || !is_array($this->inventory[ApacheServerNameCollector::COLLECTION_IDENTIFIER])
24
        ) return [];
25
26
        $configs = $this->inventory[ApacheServerNameCollector::COLLECTION_IDENTIFIER];
27
28
        $wordPressInstallations = [];
29
30
        foreach ($configs as $config) {
31
            $domain = $config[ApacheServerNameCollector::FIELD_SERVER_NAME];
32
            $documentRoot = $config[ApacheServerNameCollector::FIELD_DOCUMENT_ROOT];
33
34
            if (file_exists($documentRoot . '/wp-config.php')) {
35
36
                if (!str_ends_with($documentRoot, '/')) $documentRoot = $documentRoot . '/';
37
38
                $wpVersionFile = $documentRoot . 'wp-includes/version.php';
39
40
                $version = 'unknown';
41
42
                if (file_exists($wpVersionFile)) {
43
                    $content = file_get_contents($wpVersionFile);
44
                    if (preg_match("/\\\$wp_version\s*=\s*'([^']+)'/", $content, $matches)) {
45
                        $version = $matches[1];
46
                    }
47
                }
48
49
                $wordPressInstallations[] = [
50
                    'domain' => $domain,
51
                    'version' => $version
52
                ];
53
            }
54
        }
55
56
        return $wordPressInstallations;
57
    }
58
59
}
60