Completed
Push — master ( fa2f9e...da6516 )
by Gabriel
02:23
created

Image::toBase64()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Waredesk;
4
5
use Waredesk\Exceptions\UnknownImageTypeException;
6
7
class Image
8
{
9
    private const IMAGE_TYPE_MAP = [
10
        1 => 'gif',
11
        2 => 'jpg',
12
        3 => 'png',
13
        5 => 'psd',
14
        6 => 'bmp',
15
        7 => 'tiff',
16
        8 => 'tiff'
17
    ];
18
19
    const IMAGE_TYPE_PNG = 'png';
20
    const IMAGE_TYPE_JPG = 'jpg';
21
    const IMAGE_TYPE_GIF = 'gif';
22
    const IMAGE_TYPE_PSD = 'psd';
23
    const IMAGE_TYPE_TIFF = 'tiff';
24
    const IMAGE_TYPE_BMP = 'bmp';
25
26
    private $content;
27
    private $type;
28
29 3
    public function __construct(string $path, string $type = null)
30
    {
31 3
        $content = $path;
32 3
        if (substr_count($path, PHP_EOL) === 0 && file_exists($path)) {
33 2
            $type = exif_imagetype($path);
34 2
            if (!isset(self::IMAGE_TYPE_MAP[$type])) {
35
                throw new UnknownImageTypeException();
36
            }
37 2
            $type = self::IMAGE_TYPE_MAP[$type];
38 2
            $content = file_get_contents($path);
39
        }
40 3
        if (!in_array($type, [
41 3
            self::IMAGE_TYPE_BMP, self::IMAGE_TYPE_GIF, self::IMAGE_TYPE_JPG, self::IMAGE_TYPE_PNG,
42 3
            self::IMAGE_TYPE_PSD, self::IMAGE_TYPE_TIFF
43
        ])
44
        ) {
45
            throw new UnknownImageTypeException();
46
        }
47 3
        $this->content = $content;
48 3
        $this->type = $type;
49 3
    }
50
51 2
    public function getContent(): string
52
    {
53 2
        return $this->content;
54
    }
55
56 2
    public function getType(): string
57
    {
58 2
        return $this->type;
59
    }
60
61 1
    public function toBase64()
62
    {
63 1
        return base64_encode($this->content);
64
    }
65
}
66