Passed
Push — master ( 6bb14b...dc86bc )
by Nils
02:20
created

WordPressCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 20
dl 0
loc 42
rs 10
c 1
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setInventory() 0 3 1
A collect() 0 33 6
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
    private array $inventory;
12
13
    public function setInventory(array $inventory)
14
    {
15
        $this->inventory = $inventory;
16
    }
17
18
    public function collect(): array
19
    {
20
        if (!array_key_exists(ApacheServerNameCollector::COLLECTION_IDENTIFIER, $this->inventory)) return [];
21
22
        $configs = $this->inventory[ApacheServerNameCollector::COLLECTION_IDENTIFIER];
23
24
        $wordPressInstallations = [];
25
26
        foreach ($configs as $config) {
27
            $domain = $config[ApacheServerNameCollector::FIELD_SERVER_NAME];
28
            $documentRoot = $config[ApacheServerNameCollector::FIELD_DOCUMENT_ROOT];
29
30
            if (file_exists($documentRoot . '/wp-config.php')) {
31
32
                $wpVersionFile = $documentRoot . '/wp-includes/version.php';
33
34
                $version = 'unknown';
35
36
                if (file_exists($wpVersionFile)) {
37
                    $content = file_get_contents($wpVersionFile);
38
                    if (preg_match("/\\\$wp_version\s*=\s*'([^']+)'/", $content, $matches)) {
39
                        $version = $matches[1];
40
                    }
41
                }
42
43
                $wordPressInstallations[] = [
44
                    'domain' => $domain,
45
                    'version' => $version
46
                ];
47
            }
48
        }
49
50
        return $wordPressInstallations;
51
    }
52
53
}
54