DiskStatsService::getDiskStats()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 12
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 19
rs 9.8666
1
<?php
2
3
namespace App\Service;
4
5
use Exception;
6
7
class DiskStatsService {
8
    /**
9
     * @return array<String, float>
10
     */
11
    public function getDiskStats(string $directory): array
12
    {
13
        $disk = [];
14
15
        $free = disk_free_space($directory);
16
        if ($free === false) {
17
            throw new Exception("Couldn't discover free disk space");
18
        }
19
20
        $total = disk_total_space($directory);
21
        if ($total === false) {
22
            throw new Exception("Couldn't discover free disk total space");
23
        }
24
25
        $disk['free'] = $free;
26
        $disk['total'] = $total;
27
        $disk['used'] = $disk['total'] - $disk['free'];
28
        $disk['percent'] = $disk['used'] / $disk['total'];
29
        return $disk;
30
    }
31
}
32