Completed
Pull Request — develop (#322)
by Thomas
08:13 queued 03:00
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 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
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 6
    public function __construct()
30
    {
31 6
        parent::__construct();
32 6
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
33 6
    }
34
35
    /**
36
     * @return mixed
37
     */
38 6
    public function getData()
39
    {
40 6
        return $this->data;
41
    }
42
43
    /**
44
     * @param mixed $data
45
     * @return Base64
46
     */
47 6
    public function setData($data)
48
    {
49 6
        $this->data = $data;
50 6
        return $this;
51
    }
52
53
    /**
54
     * @return string
55
     */
56 3
    public function getContents()
57
    {
58 3
        list(, $imageContents) = explode(';', $this->getData());
59 3
        list(, $imageContents) = explode(',', $imageContents);
60 3
        return base64_decode($imageContents);
61
    }
62
63
    /**
64
     * @return string
65
     */
66 4
    public function getExtension()
67
    {
68 4
        list($data, ) = explode(';', $this->getData());
69 4
        list(, $mime) = explode(':', $data);
70
71 4
        if (!array_key_exists($mime, $this->arrayMimeExtension)) {
72 1
            throw new \Exception('Type Mime not found : "'.$mime.'"');
73
        }
74 3
        return $this->arrayMimeExtension[$mime];
75
    }
76
77
    /**
78
     * @return string
79
     */
80 2
    public function getIndexedFilename()
81
    {
82 2
        return $this->uniqueName . $this->getImageIndex() . '.' . $this->getExtension();
83
    }
84
85
    /**
86
     * @return string
87
     */
88 3
    public function getMimeType()
89
    {
90 3
        $sImage = $this->getContents();
91 3
        if (!function_exists('getimagesizefromstring')) {
92
            $uri = 'data://application/octet-stream;base64,' . base64_encode($sImage);
93
            $image = getimagesize($uri);
94
        } else {
95 3
            $image = getimagesizefromstring($sImage);
96
        }
97 3
        return image_type_to_mime_type($image[2]);
98
    }
99
}
100