File   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 33
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A renameInDirectory() 0 8 1
A getHumanReadableSize() 0 14 3
A getMimetype() 0 6 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