BrewPackageCollector   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 41
c 0
b 0
f 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A collect() 0 26 4
A getIdentifier() 0 3 1
1
<?php
2
3
namespace Startwind\Inventorio\Collector\Package\Brew;
4
5
use Startwind\Inventorio\Collector\Collector;
6
use Startwind\Inventorio\Exec\Runner;
7
8
/**
9
 * This collector returns details about all installed HomeBrew packages.
10
 */
11
class BrewPackageCollector implements Collector
12
{
13
    protected const COLLECTION_IDENTIFIER = 'HomeBrewPackages';
14
15
    /**
16
     * @inheritDoc
17
     */
18
    public function getIdentifier(): string
19
    {
20
        return self::COLLECTION_IDENTIFIER;
21
    }
22
23
    /**
24
     * @inheritDoc
25
     */
26
    public function collect(): array
27
    {
28
        $installed = Runner::getInstance()->run('command -v brew')->getOutput();
29
30
        if (!$installed) {
31
            return [];
32
        }
33
34
        $output = Runner::getInstance()->run('brew info --installed --json=v2')->getOutput();
35
36
        $rawData = json_decode($output, true);
37
38
        $packages = [];
39
40
        foreach ($rawData['formulae'] as $package) {
41
            $versions = [];
42
43
            foreach ($package['installed'] as $item) {
44
                $versions[] = $item['version'];
45
            }
46
47
            $packages[$package['name']] = $versions;
48
        }
49
50
        return [
51
            'packages' => $packages
52
        ];
53
    }
54
}
55