FileFormatter   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 14
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 14
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A formatSize() 0 9 3
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