Completed
Pull Request — develop (#207)
by Franck
09:22
created

Base64::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
namespace PhpOffice\PhpPresentation\Shape\Drawing;
4
5
6
class Base64 extends AbstractDrawingAdapter
7
{
8
    /**
9
     * @var string
10
     */
11
    protected $data;
12
13
    /**
14
     * Unique name
15
     *
16
     * @var string
17
     */
18
    protected $uniqueName;
19
20
    /**
21
     * @var array
22
     */
23
    protected $arrayMimeExtension = array(
24
        'image/jpeg' => 'jpg',
25
    );
26
27
    /**
28
     * Base64 constructor.
29
     */
30 6
    public function __construct()
31
    {
32 6
        parent::__construct();
33 6
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
34 6
    }
35
36
    /**
37
     * @return mixed
38
     */
39 6
    public function getData()
40
    {
41 6
        return $this->data;
42
    }
43
44
    /**
45
     * @param mixed $data
46
     * @return Base64
47
     */
48 6
    public function setData($data)
49
    {
50 6
        $this->data = $data;
51 6
        return $this;
52
    }
53
54
    /**
55
     * @return string
56
     */
57 3
    public function getContents()
58
    {
59 3
        list(, $imageContents) = explode(';', $this->getData());
60 3
        list(, $imageContents) = explode(',', $imageContents);
61 3
        return base64_decode($imageContents);
62
    }
63
64
    /**
65
     * @return string
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 3
    public function getMimeType()
90
    {
91 3
        $sImage = $this->getContents();
92 3
        if (!function_exists('getimagesizefromstring')) {
93
            $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
94
            $image = getimagesize($uri);
95
        } else {
96 3
            $image = getimagesizefromstring($sImage);
97
        }
98 3
        return image_type_to_mime_type($image[2]);
99
    }
100
}