Image   B
last analyzed

Complexity

Total Complexity 37

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 42.98%

Importance

Changes 5
Bugs 1 Features 4
Metric Value
wmc 37
c 5
b 1
f 4
lcom 1
cbo 3
dl 0
loc 238
ccs 52
cts 121
cp 0.4298
rs 8.6

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A __call() 0 11 2
A create() 0 4 1
C getMimeTypeFromBinary() 0 32 7
A getMimeType() 0 4 1
A createFromPath() 0 4 1
A createFromGd() 0 6 1
A createFromString() 0 7 1
A filter() 0 6 1
A isInitialized() 0 4 1
A getResource() 0 8 2
C output() 0 31 7
C outputRaw() 0 29 7
A getFactory() 0 4 1
A setFactory() 0 4 1
A __clone() 0 6 2
1
<?php
2
3
/*
4
 * This file is part of the kaloa/image package.
5
 *
6
 * For full copyright and license information, please view the LICENSE file
7
 * that was distributed with this source code.
8
 */
9
10
namespace Kaloa\Image;
11
12
use Kaloa\Image\Filter\FilterInterface;
13
use Kaloa\Image\ImageException;
14
use Kaloa\Image\ImageFactory;
15
16
/**
17
 *
18
 */
19
final class Image
20
{
21
    /**
22
     *
23
     * @var resource
24
     */
25
    private $resource = null;
26
27
    const TYPE_UNKNOWN = 'application/octet-stream';
28
    const TYPE_JPEG    = 'image/jpeg';
29
    const TYPE_PNG     = 'image/png';
30
    const TYPE_GIF     = 'image/gif';
31
    const TYPE_BMP     = 'image/x-windows-bmp';
32
    const TYPE_TIFF    = 'image/tiff';
33
34
    /**
35
     *
36
     * @var resource
37
     */
38
    private $mimeType = null;
39
40
    /**
41
     *
42
     * @var ImageFactory
43
     */
44
    private $factory;
45
46
    /**
47
     *
48
     * @param ImageFactory $factory
49
     */
50 6
    public function __construct(ImageFactory $factory)
51
    {
52 6
        $this->mimeType = self::TYPE_UNKNOWN;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::TYPE_UNKNOWN of type string is incompatible with the declared type resource of property $mimeType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
53 6
        $this->factory = $factory;
54 6
    }
55
56 1
    public function __call($name, $arguments)
57
    {
58 1
        if (!$this->factory->_hasFilter($name)) {
59
            throw new ImageException('Unknown filter or unknown method: '
60
                    . $name);
61
        }
62
63 1
        $this->filter($this->factory->_getFilterInstance($name, $arguments));
64
65 1
        return $this;
66
    }
67
68 6
    private function create($resource)
69
    {
70 6
        $this->resource = $resource;
71 6
    }
72
73
    /**
74
     *
75
     *
76
     * @param  string $binary
77
     * @return string TYPE_* constant
78
     */
79 6
    private function getMimeTypeFromBinary($binary)
80
    {
81 6
        $type = self::TYPE_UNKNOWN;
0 ignored issues
show
Unused Code introduced by
$type is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83 6
        switch (true) {
84 6
            case (1 === preg_match('/\A\xff\xd8\xff/', $binary)):
85 6
                $type = self::TYPE_JPEG;
86 6
                break;
87
            case (1 === preg_match('/\AGIF8[79]a/', $binary)):
88
                $type = self::TYPE_GIF;
89
                break;
90
            case (1 === preg_match('/\A\x89PNG\x0d\x0a/', $binary)):
91
                $type = self::TYPE_PNG;
92
                break;
93
            case (1 === preg_match('/\ABM/', $binary)):
94
                $type = self::TYPE_BMP;
95
                break;
96
            case (1 === preg_match('/\A\x49\x49(?:\x2a\x00|\x00\x4a)/',
97
                        $binary)):
98
                $type = self::TYPE_TIFF;
99
                break;
100
            case (1 === preg_match('/\AFORM.{4}ILBM/', $binary)):
101
                /** TODO Find out what this is */
102
                $type = self::TYPE_UNKNOWN;
103
                break;
104
            default:
105
                $type = self::TYPE_UNKNOWN;
106
                break;
107
        }
108
109 6
        return $type;
110
    }
111
112 1
    public function getMimeType()
113
    {
114 1
        return $this->mimeType;
115
    }
116
117 6
    public function createFromPath($path)
118
    {
119 6
        return $this->createFromString(file_get_contents($path));
120
    }
121
122
    public function createFromGd($resource)
123
    {
124
        $this->create($resource);
125
126
        return $this;
127
    }
128
129 6
    public function createFromString($imageData)
130
    {
131 6
        $this->mimeType = $this->getMimeTypeFromBinary($imageData);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->getMimeTypeFromBinary($imageData) of type string is incompatible with the declared type resource of property $mimeType.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
132 6
        $this->create(imagecreatefromstring($imageData));
133
134 6
        return $this;
135
    }
136
137 2
    public function filter(FilterInterface $filter)
138
    {
139 2
        $this->resource = $filter->render($this);
140
141 2
        return $this;
142
    }
143
144 2
    public function isInitialized()
145
    {
146 2
        return ($this->resource !== null);
147
    }
148
149 2
    public function getResource()
150
    {
151 2
        if (!$this->isInitialized()) {
152
            throw new ImageException('Image is not initialized');
153
        }
154
155 2
        return $this->resource;
156
    }
157
158
    /**
159
     * Output image directly
160
     *
161
     * Appopriate MIME types will be set.
162
     */
163
    public function output()
164
    {
165
        switch ($this->mimeType) {
166
            case self::TYPE_JPEG:
167
                header('Content-Type: image/jpeg');
168
                imagejpeg($this->resource);
169
                break;
170
            case self::TYPE_PNG:
171
                header('Content-Type: image/png');
172
                imagepng($this->resource);
173
                break;
174
            case self::TYPE_GIF:
175
                header('Content-Type: image/gif');
176
                imagegif($this->resource);
177
                break;
178
            case self::TYPE_BMP:
179
                header('Content-Type: image/x-windows-bmp');
180
                imagewbmp($this->resource);
181
                break;
182
            case self::TYPE_TIFF:
183
                /** TODO */
184
                header('Content-Type: image/x-windows-bmp');
185
                imagewbmp($this->resource);
186
                break;
187
            case self::TYPE_UNKNOWN:
188
                /** TODO */
189
                header('Content-Type: image/png');
190
                imagepng($this->resource);
191
                break;
192
        }
193
    }
194
195
    /**
196
     *
197
     * @return string Binary string containing image data
198
     */
199 3
    public function outputRaw()
200
    {
201 3
        ob_start();
202
203 3
        switch ($this->mimeType) {
204 3
            case self::TYPE_JPEG:
205 3
                imagejpeg($this->resource);
206 3
                break;
207
            case self::TYPE_PNG:
208
                imagepng($this->resource);
209
                break;
210
            case self::TYPE_GIF:
211
                imagegif($this->resource);
212
                break;
213
            case self::TYPE_BMP:
214
                imagewbmp($this->resource);
215
                break;
216
            case self::TYPE_TIFF:
217
                /** TODO */
218
                imagewbmp($this->resource);
219
                break;
220
            case self::TYPE_UNKNOWN:
221
                /** TODO */
222
                imagepng($this->resource);
223
                break;
224 3
        }
225
226 3
        return ob_get_clean();
227
    }
228
229
    /**
230
     *
231
     * @return ImageFactory
232
     */
233 1
    public function getFactory()
234
    {
235 1
        return $this->factory;
236
    }
237
238
    /**
239
     *
240
     * @param ImageFactory $factory
241
     */
242 1
    public function setFactory(ImageFactory $factory)
243
    {
244 1
        $this->factory = $factory;
245 1
    }
246
247
    /**
248
     *
249
     */
250 1
    public function __clone()
251
    {
252 1
        if (null !== $this->resource) {
253 1
            $this->createFromString($this->outputRaw());
254 1
        }
255 1
    }
256
}
257