Completed
Push — develop ( f71a4a...08d532 )
by Franck
17s
created

Base64::getExtension()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
crap 2
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
class Base64 extends AbstractDrawingAdapter
6
{
7
    /**
8
     * @var string
9
     */
10
    protected $data;
11
12
    /**
13
     * Unique name
14
     *
15
     * @var string
16
     */
17
    protected $uniqueName;
18
19
    /**
20
     * @var array
21
     */
22
    protected $arrayMimeExtension = array(
23
        'image/jpeg' => 'jpg',
24
    );
25
26
    /**
27
     * Base64 constructor.
28
     */
29 7
    public function __construct()
30
    {
31 7
        parent::__construct();
32 7
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
33 7
    }
34
35
    /**
36
     * @return mixed
37
     */
38 7
    public function getData()
39
    {
40 7
        return $this->data;
41
    }
42
43
    /**
44
     * @param mixed $data
45
     * @return Base64
46
     */
47 7
    public function setData($data)
48
    {
49 7
        $this->data = $data;
50 7
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 4
    public function getContents()
57
    {
58 4
        list(, $imageContents) = explode(';', $this->getData());
59 4
        list(, $imageContents) = explode(',', $imageContents);
60 4
        return base64_decode($imageContents);
61
    }
62
63
    /**
64
     * @return string
65
     * @throws \Exception
66
     */
67 4
    public function getExtension()
68
    {
69 4
        list($data, ) = explode(';', $this->getData());
70 4
        list(, $mime) = explode(':', $data);
71
72 4
        if (!array_key_exists($mime, $this->arrayMimeExtension)) {
73 1
            throw new \Exception('Type Mime not found : "'.$mime.'"');
74
        }
75 3
        return $this->arrayMimeExtension[$mime];
76
    }
77
78
    /**
79
     * @return string
80
     */
81 2
    public function getIndexedFilename()
82
    {
83 2
        return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension();
84
    }
85
86
    /**
87
     * @return string
88
     */
89 4
    public function getMimeType()
90
    {
91 4
        $sImage = $this->getContents();
92 4
        if (!function_exists('getimagesizefromstring')) {
93 3
            $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
94 3
            $image = getimagesize($uri);
95
        } else {
96 1
            $image = getimagesizefromstring($sImage);
97
        }
98 4
        return image_type_to_mime_type($image[2]);
99
    }
100
}
101