ValidatesBase64::getSize()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Owowagency\LaravelMedia\Rules\Concerns;
4
5
trait ValidatesBase64
6
{
7
    /**
8
     * Reads mime type of string by using file info buffer.
9
     *
10
     * @param  string  $value
11
     * @return string
12
     */
13
    protected static function getMimeType(string $value): string
14
    {
15
        $fileData = base64_decode(static::removeScheme($value));
16
17
        $f = finfo_open();
18
19
        $mimeType = finfo_buffer($f, $fileData, FILEINFO_MIME_TYPE);
20
21
        finfo_close($f);
22
23
        return $mimeType;
24
    }
25
26
    /**
27
     * Removes data scheme from base64.
28
     *
29
     * @param  string  $value
30
     * @return string
31
     */
32
    protected static function removeScheme(string $value): string
33
    {
34
        if (strpos($value, ';base64') !== false) {
35
            list(, $value) = explode(';', $value);
36
37
            list(, $value) = explode(',', $value);
38
        }
39
40
        return $value;
41
    }
42
43
    /**
44
     * Returns the size of a base64 string in kilobytes.
45
     *
46
     * @param  string  $value
47
     * @return float
48
     */
49
    protected function getSize(string $value): float
50
    {
51
        return ((float) strlen(base64_decode($value))) / 1024;
52
    }
53
}
54