getExtensionFromBase64File()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 7
cts 8
cp 0.875
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 1
crap 2.0078
1
<?php
2
3
namespace AppBundle\Service\File;
4
5
use AppBundle\Exception\UnsupportedTypeException;
6
7
/**
8
 * @author Vehsamrak
9
 */
10
class ImageExtensionChecker
11
{
12
13
    /**
14
     * @throws UnsupportedTypeException
15
     */
16 1
    public function getExtensionFromBase64File(string $image): string
17
    {
18
        $imageMap = [
19 1
            'image/png' => 'png',
20
            'image/gif' => 'gif',
21
            'image/jpeg' => 'jpeg',
22
        ];
23
24 1
        $imagedata = base64_decode($image);
25 1
        $image = finfo_open();
26 1
        $mimeType = finfo_buffer($image, $imagedata, FILEINFO_MIME_TYPE);
27
28 1
        if (!array_key_exists($mimeType, $imageMap)) {
29
        	throw new UnsupportedTypeException();
30
        }
31
32 1
        return $imageMap[$mimeType];
33
    }
34
}
35