Passed
Push — master ( d9b17e...f981b4 )
by Zoilo
01:42
created

isAllowedOperatingSystem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Utils\ControlGroup;
4
5
final class FileControlGroupRepository implements ControlGroupRepository
6
{
7
    const ALLOWED_OS = 'Linux';
8
    const PATH = '/proc/self/cgroup';
9
10
    /**
11
     * @return ControlGroup[]
12
     */
13
    public function findAll()
14
    {
15
        if (false === $this->isAllowedOperatingSystem()) {
16
            return [];
17
        }
18
19
        return $this->deserializeFile(self::PATH);
20
    }
21
22
    /**
23
     * @return bool
24
     */
25
    private function isAllowedOperatingSystem()
26
    {
27
        return self::ALLOWED_OS === PHP_OS;
28
    }
29
30
    /**
31
     * @param string $path
32
     *
33
     * @return ControlGroup[]
34
     */
35
    private function deserializeFile($path)
36
    {
37
        if (false === is_readable($path)) {
38
            return [];
39
        }
40
41
        $cGroup = file_get_contents($path);
42
        $lines = explode(PHP_EOL, $cGroup);
43
44
        $result = [];
45
        foreach ($lines as $line) {
46
            $fields = explode(':', $line);
47
            if (3 !== count($fields)) {
48
                continue;
49
            }
50
51
            list($hierarchyId, $controller, $path) = $fields;
52
53
            $result[] = new ControlGroup(
54
                $hierarchyId,
55
                $controller,
56
                $path
57
            );
58
        }
59
60
        return $result;
61
    }
62
}
63