GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Images::route()   F
last analyzed

Complexity

Conditions 20
Paths 3888

Size

Total Lines 93
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 20
eloc 64
nc 3888
nop 0
dl 0
loc 93
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * This file is part of the O2System Framework package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @author         Steeve Andrian Salim
9
 * @copyright      Copyright (c) Steeve Andrian Salim
10
 */
11
12
// ------------------------------------------------------------------------
13
14
namespace O2System\Framework\Http\Controllers;
15
16
// ------------------------------------------------------------------------
17
18
use O2System\Cache\Item;
19
use O2System\Framework\Http\Controller;
20
use O2System\Image\Manipulation;
21
22
/**
23
 * Class Images
24
 *
25
 * @package O2System\Framework\Http\Controllers
26
 */
27
class Images extends Controller
28
{
29
    /**
30
     * Images::$inherited
31
     *
32
     * Controller inherited flag.
33
     *
34
     * @var bool
35
     */
36
    static public $inherited = true;
37
38
    /**
39
     * Images::$storagePath
40
     *
41
     * @var string
42
     */
43
    public $storagePath;
44
45
    /**
46
     * Images::$imageNotFoundFilename
47
     *
48
     * @var string
49
     */
50
    public $imageNotFoundFilename = 'not-found.jpg';
51
52
    /**
53
     * Images::$imageFilePath
54
     *
55
     * @var string
56
     */
57
    public $imageFilePath = null;
58
59
    /**
60
     * Images::$imageFileMime
61
     *
62
     * @var string
63
     */
64
    public $imageFileMime = null;
65
66
    /**
67
     * Images::$imageSize
68
     *
69
     * @var array
70
     */
71
    public $imageSize = [
72
        'width'  => null,
73
        'height' => null,
74
    ];
75
76
    /**
77
     * Images::$imageScale
78
     *
79
     * @var string
80
     */
81
    public $imageScale = null;
82
83
    /**
84
     * Images::$imageQuality
85
     *
86
     * @var int
87
     */
88
    public $imageQuality = null;
89
90
    /**
91
     * Images::$imageCrop
92
     *
93
     * @var bool
94
     */
95
    public $imageCrop = false;
96
97
    // ------------------------------------------------------------------------
98
99
    /**
100
     * Images::__construct
101
     */
102
    public function __construct()
103
    {
104
        $this->storagePath = PATH_STORAGE . 'images' . DIRECTORY_SEPARATOR;
105
        $this->imageNotFoundFilename = $this->storagePath . 'not-found.jpg';
106
    }
107
108
    // ------------------------------------------------------------------------
109
110
    /**
111
     * Images::route
112
     */
113
    public function route()
114
    {
115
        $segments = server_request()->getUri()->segments->getArrayCopy();
116
117
        if (false !== ($key = array_search('images', $segments))) {
118
            $segments = array_slice($segments, $key);
0 ignored issues
show
Bug introduced by
It seems like $key can also be of type string; however, parameter $offset of array_slice() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
            $segments = array_slice($segments, /** @scrutinizer ignore-type */ $key);
Loading history...
119
        } else {
120
            array_shift($segments);
121
        }
122
123
        $this->imageFilePath = $this->imageNotFoundFilename;
124
125
        $this->imageSize[ 'width' ] = $this->input->get('width');
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist on O2System\Framework\Http\Controllers\Images. Since you implemented __get, consider adding a @property annotation.
Loading history...
126
        $this->imageSize[ 'height' ] = $this->input->get('height');
127
        $this->imageScale = $this->input->get('scale');
128
        $this->imageQuality = $this->input->get('quality');
129
        $this->imageCrop = $this->input->get('crop');
130
131
        if (false !== ($key = array_search('crop', $segments))) {
132
            $this->imageCrop = true;
133
            unset($segments[ $key ]);
134
            $segments = array_values($segments);
135
        }
136
137
        if (count($segments) == 1) {
138
            $this->imageFilePath = $this->storagePath . end($segments);
139
        } elseif (count($segments) >= 2) {
140
            if (preg_match("/(\d+)(x)(\d+)/", $segments[ count($segments) - 2 ], $matches)) {
141
                $this->imageSize[ 'width' ] = $matches[ 1 ];
142
                $this->imageSize[ 'height' ] = $matches[ 3 ];
143
144
                if (count($segments) == 2) {
145
                    $this->imageFilePath = $this->storagePath . end($segments);
146
                } else {
147
                    $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR,
148
                            array_slice($segments, 0,
149
                                count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments);
150
                }
151
            } elseif (preg_match("/(\d+)(p)/", $segments[ count($segments) - 2 ],
152
                    $matches) or is_numeric($segments[ count($segments) - 2 ])
153
            ) {
154
                $this->imageScale = isset($matches[ 1 ]) ? $matches[ 1 ] : $segments[ count($segments) - 2 ];
155
                if (count($segments) == 2) {
156
                    $this->imageFilePath = $this->storagePath . end($segments);
157
                } else {
158
                    $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR,
159
                            array_slice($segments, 0,
160
                                count($segments) - 2)) . DIRECTORY_SEPARATOR . end($segments);
161
                }
162
            } else {
163
                $this->imageFilePath = $this->storagePath . implode(DIRECTORY_SEPARATOR, $segments);
164
            }
165
        }
166
167
        $imageFilePath = $this->imageFilePath;
168
        $extensions[ 0 ] = pathinfo($imageFilePath, PATHINFO_EXTENSION);
0 ignored issues
show
Comprehensibility Best Practice introduced by
$extensions was never initialized. Although not strictly required by PHP, it is generally a good practice to add $extensions = array(); before regardless.
Loading history...
169
170
        for ($i = 0; $i < 2; $i++) {
171
            $extension = pathinfo($imageFilePath, PATHINFO_EXTENSION);
172
173
            if ($extension !== '') {
174
                $extensions[ $i ] = $extension;
175
                $imageFilePath = str_replace('.' . $extensions[ $i ], '', $imageFilePath);
176
            }
177
        }
178
179
        $mimes = [
180
            'gif'  => 'image/gif',
181
            'jpg'  => 'image/jpeg',
182
            'png'  => 'image/png',
183
            'webp' => 'image/webp',
184
        ];
185
186
        if (count($extensions) == 2) {
187
            $this->imageFilePath = $imageFilePath . '.' . $extensions[ 1 ];
188
        }
189
190
        if (array_key_exists($extension = reset($extensions), $mimes)) {
191
            $this->imageFileMime = $mimes[ $extension ];
192
        } elseif (array_key_exists($extension = pathinfo($this->imageFilePath, PATHINFO_EXTENSION), $mimes)) {
193
            $this->imageFileMime = $mimes[ $extension ];
194
        }
195
196
        if ( ! is_file($this->imageFilePath)) {
197
            $this->imageFilePath = $this->imageNotFoundFilename;
198
        }
199
200
        if ( ! empty($this->imageScale)) {
201
            $this->scale();
202
        } elseif ( ! empty($this->imageSize[ 'width' ]) || ! empty($this->imageSize[ 'height' ])) {
203
            $this->resize();
204
        } else {
205
            $this->original();
206
        }
207
    }
208
209
    // ------------------------------------------------------------------------
210
211
    /**
212
     * Images::scale
213
     *
214
     * @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException
215
     */
216
    protected function scale()
217
    {
218
        $config = config('image', true);
219
220
        if ( ! empty($this->imageQuality)) {
221
            $config->offsetSet('quality', intval($this->imageQuality));
222
        }
223
224
        if ($config->cached === true) {
0 ignored issues
show
Bug Best Practice introduced by
The property cached does not exist on O2System\Framework\Containers\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
225
            if ($this->imageFilePath !== $this->imageNotFoundFilename) {
226
                $cacheImageKey = 'image-' . $this->imageScale . '-' . str_replace($this->storagePath, '',
227
                        $this->imageFilePath);
228
229
                if (cache()->hasItemPool('images')) {
230
                    $cacheItemPool = cache()->getItemPool('images');
231
232
                    if ($cacheItemPool->hasItem($cacheImageKey)) {
233
                        $cacheImageString = $cacheItemPool->getItem($cacheImageKey)->get();
234
235
                        $manipulation = new Manipulation($config);
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type O2System\Framework\Containers\Config; however, parameter $config of O2System\Image\Manipulation::__construct() does only seem to accept O2System\Image\DataStructures\Config|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

235
                        $manipulation = new Manipulation(/** @scrutinizer ignore-type */ $config);
Loading history...
236
                        $manipulation->setImageFile($this->imageFilePath);
237
                        $manipulation->setImageString($cacheImageString);
238
                        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
239
                    } else {
240
                        $manipulation = new Manipulation($config);
241
                        $manipulation->setImageFile($this->imageFilePath);
242
                        $manipulation->scaleImage($this->imageScale);
0 ignored issues
show
Bug introduced by
$this->imageScale of type string is incompatible with the type integer expected by parameter $newScale of O2System\Image\Manipulation::scaleImage(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

242
                        $manipulation->scaleImage(/** @scrutinizer ignore-type */ $this->imageScale);
Loading history...
243
                        $cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), false));
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type DateInterval|integer expected by parameter $expiresAfter of O2System\Cache\Item::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

243
                        $cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), /** @scrutinizer ignore-type */ false));
Loading history...
244
245
                        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
246
                        exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
247
                    }
248
                }
249
            }
250
        }
251
252
        $manipulation = new Manipulation($config);
253
        $manipulation->setImageFile($this->imageFilePath);
254
        $manipulation->scaleImage($this->imageScale);
255
        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
256
        exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
257
    }
258
259
    // ------------------------------------------------------------------------
260
261
    /**
262
     * Images::resize
263
     *
264
     * @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException
265
     */
266
    protected function resize()
267
    {
268
        $config = config('image', true);
269
270
        if ( ! empty($this->imageQuality)) {
271
            $config->offsetSet('quality', intval($this->imageQuality));
272
        }
273
274
        if ($config->cached === true) {
0 ignored issues
show
Bug Best Practice introduced by
The property cached does not exist on O2System\Framework\Containers\Config. Since you implemented __get, consider adding a @property annotation.
Loading history...
275
            if ($this->imageFilePath !== $this->imageNotFoundFilename) {
276
                $cacheImageKey = 'image-' . ($this->input->get('crop') ? 'crop-' : '') . implode('x',
0 ignored issues
show
Bug Best Practice introduced by
The property input does not exist on O2System\Framework\Http\Controllers\Images. Since you implemented __get, consider adding a @property annotation.
Loading history...
277
                        $this->imageSize) . '-' . str_replace($this->storagePath, '', $this->imageFilePath);
278
279
                if (cache()->hasItemPool('images')) {
280
                    $cacheItemPool = cache()->getItemPool('images');
281
282
                    if ($cacheItemPool->hasItem($cacheImageKey)) {
283
                        $cacheImageString = $cacheItemPool->getItem($cacheImageKey)->get();
284
285
                        $manipulation = new Manipulation($config);
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type O2System\Framework\Containers\Config; however, parameter $config of O2System\Image\Manipulation::__construct() does only seem to accept O2System\Image\DataStructures\Config|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

285
                        $manipulation = new Manipulation(/** @scrutinizer ignore-type */ $config);
Loading history...
286
                        $manipulation->setImageFile($this->imageFilePath);
287
                        $manipulation->setImageString($cacheImageString);
288
                        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
289
                    } else {
290
                        $manipulation = new Manipulation($config);
291
                        $manipulation->setImageFile($this->imageFilePath);
292
                        $manipulation->resizeImage($this->imageSize[ 'width' ], $this->imageSize[ 'height' ],
293
                            (bool)$this->imageCrop);
294
                        $cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), false));
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type DateInterval|integer expected by parameter $expiresAfter of O2System\Cache\Item::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

294
                        $cacheItemPool->save(new Item($cacheImageKey, $manipulation->getBlobImage(), /** @scrutinizer ignore-type */ false));
Loading history...
295
296
                        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
297
                        exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
298
                    }
299
                }
300
            }
301
        }
302
303
        $manipulation = new Manipulation($config);
304
        $manipulation->setImageFile($this->imageFilePath);
305
        $manipulation->resizeImage($this->imageSize[ 'width' ], $this->imageSize[ 'height' ], (bool)$this->imageCrop);
306
        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
307
        exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
308
    }
309
310
    // ------------------------------------------------------------------------
311
312
    /**
313
     * Images::original
314
     *
315
     * @throws \O2System\Spl\Exceptions\Runtime\FileNotFoundException
316
     */
317
    protected function original()
318
    {
319
        $config = config('image', true);
320
321
        if ( ! empty($this->imageQuality)) {
322
            $config->offsetSet('quality', intval($this->imageQuality));
323
        }
324
325
        $manipulation = new Manipulation($config);
0 ignored issues
show
Bug introduced by
It seems like $config can also be of type O2System\Framework\Containers\Config; however, parameter $config of O2System\Image\Manipulation::__construct() does only seem to accept O2System\Image\DataStructures\Config|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

325
        $manipulation = new Manipulation(/** @scrutinizer ignore-type */ $config);
Loading history...
326
        $manipulation->setImageFile($this->imageFilePath);
327
        $manipulation->displayImage(intval($this->imageQuality), $this->imageFileMime);
328
        exit(EXIT_SUCCESS);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
329
    }
330
}