ValidatesBase64   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 11
dl 0
loc 47
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A removeScheme() 0 9 2
A getSize() 0 3 1
A getMimeType() 0 11 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