Completed
Push — develop ( d383bc...d9bd45 )
by Adrien
23:59
created

MemoryDrawing::getHashCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
ccs 7
cts 7
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace PhpOffice\PhpSpreadsheet\Worksheet;
4
5
class MemoryDrawing extends BaseDrawing
6
{
7
    // Rendering functions
8
    const RENDERING_DEFAULT = 'imagepng';
9
    const RENDERING_PNG = 'imagepng';
10
    const RENDERING_GIF = 'imagegif';
11
    const RENDERING_JPEG = 'imagejpeg';
12
13
    // MIME types
14
    const MIMETYPE_DEFAULT = 'image/png';
15
    const MIMETYPE_PNG = 'image/png';
16
    const MIMETYPE_GIF = 'image/gif';
17
    const MIMETYPE_JPEG = 'image/jpeg';
18
19
    /**
20
     * Image resource.
21
     *
22
     * @var resource
23
     */
24
    private $imageResource;
25
26
    /**
27
     * Rendering function.
28
     *
29
     * @var string
30
     */
31
    private $renderingFunction;
32
33
    /**
34
     * Mime type.
35
     *
36
     * @var string
37
     */
38
    private $mimeType;
39
40
    /**
41
     * Unique name.
42
     *
43
     * @var string
44
     */
45
    private $uniqueName;
46
47
    /**
48
     * Create a new MemoryDrawing.
49
     */
50 4
    public function __construct()
51
    {
52
        // Initialise values
53 4
        $this->imageResource = null;
54 4
        $this->renderingFunction = self::RENDERING_DEFAULT;
55 4
        $this->mimeType = self::MIMETYPE_DEFAULT;
56 4
        $this->uniqueName = md5(rand(0, 9999) . time() . rand(0, 9999));
57
58
        // Initialize parent
59 4
        parent::__construct();
60 4
    }
61
62
    /**
63
     * Get image resource.
64
     *
65
     * @return resource
66
     */
67 4
    public function getImageResource()
68
    {
69 4
        return $this->imageResource;
70
    }
71
72
    /**
73
     * Set image resource.
74
     *
75
     * @param resource $value
76
     *
77
     * @return MemoryDrawing
78
     */
79 4
    public function setImageResource($value)
80
    {
81 4
        $this->imageResource = $value;
82
83 4
        if ($this->imageResource !== null) {
84
            // Get width/height
85 4
            $this->width = imagesx($this->imageResource);
86 4
            $this->height = imagesy($this->imageResource);
87
        }
88
89 4
        return $this;
90
    }
91
92
    /**
93
     * Get rendering function.
94
     *
95
     * @return string
96
     */
97 4
    public function getRenderingFunction()
98
    {
99 4
        return $this->renderingFunction;
100
    }
101
102
    /**
103
     * Set rendering function.
104
     *
105
     * @param string $value see self::RENDERING_*
106
     *
107
     * @return MemoryDrawing
108
     */
109 4
    public function setRenderingFunction($value)
110
    {
111 4
        $this->renderingFunction = $value;
112
113 4
        return $this;
114
    }
115
116
    /**
117
     * Get mime type.
118
     *
119
     * @return string
120
     */
121 4
    public function getMimeType()
122
    {
123 4
        return $this->mimeType;
124
    }
125
126
    /**
127
     * Set mime type.
128
     *
129
     * @param string $value see self::MIMETYPE_*
130
     *
131
     * @return MemoryDrawing
132
     */
133 4
    public function setMimeType($value)
134
    {
135 4
        $this->mimeType = $value;
136
137 4
        return $this;
138
    }
139
140
    /**
141
     * Get indexed filename (using image index).
142
     *
143
     * @return string
144
     */
145 4
    public function getIndexedFilename()
146
    {
147 4
        $extension = strtolower($this->getMimeType());
148 4
        $extension = explode('/', $extension);
149 4
        $extension = $extension[1];
150
151 4
        return $this->uniqueName . $this->getImageIndex() . '.' . $extension;
152
    }
153
154
    /**
155
     * Get hash code.
156
     *
157
     * @return string Hash code
158
     */
159 4
    public function getHashCode()
160
    {
161 4
        return md5(
162 4
            $this->renderingFunction .
163 4
            $this->mimeType .
164 4
            $this->uniqueName .
165 4
            parent::getHashCode() .
166 4
            __CLASS__
167
        );
168
    }
169
}
170