DiskSpaceContributor::run()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Nwidart\Actuator\Health;
6
7
class DiskSpaceContributor implements HealthContributor
8
{
9 1
    public function run(): array
10
    {
11
        return [
12 1
            'free' => $this->formatBytes(disk_free_space('/')),
13 1
            'total' => $this->formatBytes(disk_total_space('/')),
14
        ];
15
    }
16
17
    /*
18
     * Source https://stackoverflow.com/questions/2510434/format-bytes-to-kilobytes-megabytes-gigabytes
19
     */
20 1
    private function formatBytes($bytes, $precision = 2): string
21
    {
22 1
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
23
24 1
        $bytes = max($bytes, 0);
25 1
        $pow = floor(($bytes ? log($bytes) : 0) / log(1024));
26 1
        $pow = min($pow, count($units) - 1);
27 1
        $bytes /= (1 << (10 * $pow));
28
29 1
        return round($bytes, $precision) . ' ' . $units[$pow];
30
    }
31
}
32