Passed
Pull Request — 2.x (#704)
by Talv
08:50
created

bytesToHuman()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
use Illuminate\Support\Facades\Storage;
4
5 1
if (!function_exists('s3Endpoint')) {
6
    /**
7
     * @param string $disk
8
     * @return string
9
     */
10
    function s3Endpoint($disk = 'libraries')
11
    {
12
        // if a custom s3 endpoint is configured explicitly, return it
13
        $customEndpoint = config("filesystems.disks.{$disk}.endpoint");
14
15
        if ($customEndpoint) {
16
            return $customEndpoint;
17
        }
18
19
        $scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : '';
20
        return $scheme . config("filesystems.disks.{$disk}.bucket") . '.' . Storage::disk($disk)->getAdapter()->getClient()->getEndpoint()->getHost();
0 ignored issues
show
Bug introduced by
The method getAdapter() does not exist on Illuminate\Contracts\Filesystem\Filesystem. It seems like you code against a sub-type of Illuminate\Contracts\Filesystem\Filesystem such as Illuminate\Filesystem\FilesystemAdapter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
        return $scheme . config("filesystems.disks.{$disk}.bucket") . '.' . Storage::disk($disk)->/** @scrutinizer ignore-call */ getAdapter()->getClient()->getEndpoint()->getHost();
Loading history...
21
    }
22
}
23
24 1
if (!function_exists('azureEndpoint')) {
25
    /**
26
     * @param string $disk
27
     * @return string
28
     */
29
    function azureEndpoint($disk = 'libraries')
30
    {
31
        $scheme = config("filesystems.disks.{$disk}.use_https") ? 'https://' : '';
32
        return $scheme . config("filesystems.disks.{$disk}.name") . '.blob.' . config("filesystems.disks.{$disk}.endpoint-suffix") . '/' . config("filesystems.disks.{$disk}.container");
33
    }
34
}
35
36 1
if (!function_exists('bytesToHuman')) {
37
    /**
38
     * @param float $bytes
39
     * @return string
40
     */
41
    function bytesToHuman($bytes)
42
    {
43 3
        $units = ['B', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb'];
44
45 3
        for ($i = 0; $bytes > 1024; $i++) {
46
            $bytes /= 1024;
47
        }
48
49 3
        return round($bytes, 2) . ' ' . $units[$i];
50
    }
51
}
52
53 1
if (!function_exists('replaceAccents')) {
54
    /**
55
     * @param string $str
56
     * @return bool|string
57
     */
58
    function replaceAccents($str)
59
    {
60 3
        return iconv('UTF-8', 'ASCII//TRANSLIT', $str);
61
    }
62
}
63
64 1
if (!function_exists('sanitizeFilename')) {
65
    /**
66
     * @param string $filename
67
     * @return string
68
     */
69
    function sanitizeFilename($filename)
70
    {
71 3
        $sanitizedFilename = replaceAccents($filename);
72
73
        $invalid = array(
74 3
            ' ' => '-',
75
            '%20' => '-',
76
            '_' => '-',
77
        );
78
79 3
        $sanitizedFilename = str_replace(array_keys($invalid), array_values($invalid), $sanitizedFilename);
80
81 3
        $sanitizedFilename = preg_replace('/[^A-Za-z0-9-\. ]/', '', $sanitizedFilename); // Remove all non-alphanumeric except .
82 3
        $sanitizedFilename = preg_replace('/\.(?=.*\.)/', '', $sanitizedFilename); // Remove all but last .
83 3
        $sanitizedFilename = preg_replace('/-+/', '-', $sanitizedFilename); // Replace any more than one - in a row
84 3
        $sanitizedFilename = str_replace('-.', '.', $sanitizedFilename); // Remove last - if at the end
85 3
        $sanitizedFilename = strtolower($sanitizedFilename); // Lowercase
86
87 3
        return $sanitizedFilename;
88
    }
89
}
90