SymfonyCollector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setInventory() 0 3 1
B collect() 0 29 7
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Frameworks\Php;
4
5
use Startwind\Inventorio\Collector\BasicCollector;
6
use Startwind\Inventorio\Collector\InventoryAwareCollector;
7
use Startwind\Inventorio\Exec\File;
8
use Startwind\Inventorio\Util\WebserverUtil;
9
10
class SymfonyCollector extends BasicCollector implements InventoryAwareCollector
11
{
12
    protected string $identifier = 'FrameworkPHPSymfony';
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
        $file = File::getInstance();
23
24
        $symfonyDomains = [];
25
26
        $documentRoots = WebserverUtil::extractDocumentRoots($this->inventory);
27
28
        foreach ($documentRoots as $domain => $documentRoot) {
29
            if ($file->fileExists($documentRoot . '/../composer.lock')) {
30
                $composerLockRaw = File::getInstance()->getContents($documentRoot . '/../composer.lock');
31
                $composerLock = json_decode($composerLockRaw, true);
32
33
                if (array_key_exists('packages', $composerLock) && is_array($composerLock['packages'])) {
34
                    foreach ($composerLock['packages'] as $package) {
35
                        if ($package['name'] === 'symfony/framework-bundle') {
36
                            $symfonyDomains[$domain] = [
37
                                'version' => trim($package['version'], 'v'),
38
                                'path' => $file->realPath($documentRoot . '/../')
39
                            ];
40
                            break;
41
                        }
42
                    }
43
                }
44
            }
45
        }
46
47
        return [
48
            'framework' => $symfonyDomains
49
        ];
50
    }
51
}
52