File::getHumanReadableSize()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
namespace Spatie\MediaLibrary\Helpers;
4
5
use Finfo;
6
7
class File
8
{
9
    public static function renameInDirectory(string $fileNameWithDirectory, string $newFileNameWithoutDirectory): string
10
    {
11
        $targetFile = pathinfo($fileNameWithDirectory, PATHINFO_DIRNAME).'/'.$newFileNameWithoutDirectory;
12
13
        rename($fileNameWithDirectory, $targetFile);
14
15
        return $targetFile;
16
    }
17
18
    public static function getHumanReadableSize(int $sizeInBytes): string
19
    {
20
        $units = ['B', 'KB', 'MB', 'GB', 'TB'];
21
22
        if ($sizeInBytes == 0) {
23
            return '0 '.$units[1];
24
        }
25
26
        for ($i = 0; $sizeInBytes > 1024; $i++) {
27
            $sizeInBytes /= 1024;
28
        }
29
30
        return round($sizeInBytes, 2).' '.$units[$i];
31
    }
32
33
    public static function getMimetype(string $path): string
34
    {
35
        $finfo = new Finfo(FILEINFO_MIME_TYPE);
36
37
        return $finfo->file($path);
38
    }
39
}
40