GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — develop ( dd0027...dc3f07 )
by Baptiste
03:07
created

UnixDisk   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 95
Duplicated Lines 0 %

Test Coverage

Coverage 97.96%

Importance

Changes 0
Metric Value
wmc 5
eloc 54
dl 0
loc 95
c 0
b 0
f 0
ccs 48
cts 49
cp 0.9796
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A parse() 0 52 1
A volumes() 0 4 1
A get() 0 5 1
A run() 0 10 2
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Server\Status\Server\Disk;
5
6
use Innmind\Server\Status\{
7
    Server\Disk,
8
    Server\Disk\Volume\MountPoint,
9
    Server\Disk\Volume\Usage,
10
    Server\Memory\Bytes,
11
    Exception\DiskUsageNotAccessible
12
};
13
use Innmind\Immutable\{
14
    MapInterface,
15
    Str,
16
    StreamInterface,
17
    Sequence,
18
    Map
19
};
20
use Symfony\Component\Process\Process;
21
22
final class UnixDisk implements Disk
23
{
24
    private static $columns = [
25
        'Size' => 'size',
26
        'Used' => 'used',
27
        'Avail' => 'available',
28
        'Use%' => 'usage',
29
        'Capacity' => 'usage',
30
        'Mounted' => 'mountPoint',
31
    ];
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 4
    public function volumes(): MapInterface
37
    {
38 4
        return $this->parse(
39 4
            $this->run('df -lh')
40
        );
41
    }
42
43 2
    public function get(MountPoint $point): Volume
44
    {
45
        return $this
46 2
            ->volumes()
47 2
            ->get((string) $point);
48
    }
49
50 4
    private function run(string $command): Str
51
    {
52 4
        $process = new Process($command);
53 4
        $process->run();
54
55 4
        if (!$process->isSuccessful()) {
56
            throw new DiskUsageNotAccessible;
57
        }
58
59 4
        return new Str($process->getOutput());
60
    }
61
62
    /**
63
     * @return MapInterface<string, Volume>
64
     */
65 4
    private function parse(Str $output): MapInterface
66
    {
67
        $lines = $output
68 4
            ->trim()
69 4
            ->split("\n");
70
        $columns = $lines
71 4
            ->first()
72 4
            ->pregSplit('~ +~')
73 4
            ->reduce(
74 4
                new Sequence,
75 4
                static function(Sequence $columns, Str $column): Sequence {
76 4
                    $column = (string) $column;
77
78 4
                    return $columns->add(self::$columns[$column] ?? $column);
79 4
                }
80
            );
81
82
        return $lines
83 4
            ->drop(1)
84 4
            ->reduce(
85 4
                new Sequence,
86 4
                static function(Sequence $lines, Str $line) use ($columns): Sequence {
87 4
                    return $lines->add(
88 4
                        $line->pregSplit('~ +~', $columns->size())
89
                    );
90 4
                }
91
            )
92 4
            ->map(function(StreamInterface $parts) use ($columns): Volume {
93 4
                return new Volume(
94 4
                    new MountPoint(
95 4
                        (string) $parts->get($columns->indexOf('mountPoint'))
96
                    ),
97 4
                    Bytes::fromString(
98 4
                        (string) $parts->get($columns->indexOf('size'))
99
                    ),
100 4
                    Bytes::fromString(
101 4
                        (string) $parts->get($columns->indexOf('available'))
102
                    ),
103 4
                    Bytes::fromString(
104 4
                        (string) $parts->get($columns->indexOf('used'))
105
                    ),
106 4
                    new Usage(
107 4
                        (float) (string) $parts->get($columns->indexOf('usage'))
108
                    )
109
                );
110 4
            })
111 4
            ->reduce(
112 4
                new Map('string', Volume::class),
113 4
                static function(Map $volumes, Volume $volume): Map {
114 4
                    return $volumes->put(
115 4
                        (string) $volume->mountPoint(),
116 4
                        $volume
117
                    );
118 4
                }
119
            );
120
    }
121
}
122