BrewPackageCollector::getIdentifier()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
nc 1
nop 0
dl 0
loc 3
c 0
b 0
f 0
cc 1
rs 10
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