FileFormatter::formatSize()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Damax\Media\Domain;
6
7
final class FileFormatter
8
{
9
    private const UNITS = ['B', 'KB', 'MB', 'GB'];
10
    private const PRECISION = [0, 0, 1, 2];
11
12
    public function formatSize(int $value): string
13
    {
14
        $len = count(self::UNITS) - 1;
15
16
        for ($index = 0; $value >= 1024 && $index < $len; ++$index) {
17
            $value = $value / 1024;
18
        }
19
20
        return number_format($value, self::PRECISION[$index], '.', '') . ' ' . self::UNITS[$index];
21
    }
22
}
23