DiskSpaceContributor   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 0
dl 0
loc 25
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A run() 0 7 1
A formatBytes() 0 11 2
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