Completed
Push — master ( c844fc...ec9d92 )
by Jan
21:21
created

MediaAddParameters::handle()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 0 Features 1
Metric Value
c 7
b 0
f 1
dl 0
loc 38
rs 8.8571
cc 3
eloc 17
nc 3
nop 2
1
<?php
2
/**
3
 * Media add parameters
4
 * 
5
 * Adds variable parameters to media files
6
 * 
7
 * @category Middleware
8
 * @subpackage General
9
 * @package Olapus
10
 * @author Jan Drda <[email protected]>
11
 * @copyright Jan Drda
12
 * @license https://opensource.org/licenses/MIT MIT
13
 */
14
15
namespace App\Http\Middleware;
16
17
use Closure;
18
19
/**
20
 * Adding media parameters
21
 */
22
class MediaAddParameters {
23
24
    /**
25
     * Main function
26
     *
27
     * @param \Illuminate\Http\Request $request
28
     * @param Closure $next
29
     * @return mixed
30
     */
31
    public function handle($request, Closure $next) {
32
        foreach ($request->file() as $name => $value) {
0 ignored issues
show
Bug introduced by
The expression $request->file() of type object<Illuminate\Http\UploadedFile>|array|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
33
34
            /**
35
             * All media
36
             */
37
            $request->merge(
38
                    [
39
                        $name . '_mime_type' => $request->file($name)->getMimeType(),
40
                        $name . '_original_extension' => $request->file($name)->getClientOriginalExtension(),
41
                        $name . '_original_name' => $request->file($name)->getClientOriginalName(),
42
                        $name . '_extension' => pathinfo($request->file($name)->getClientOriginalName(), PATHINFO_EXTENSION),
43
                        $name . '_size' => filesize($request->file($name)->getRealPath()),
44
                        $name . '_etag' => sha1_file($request->file($name)->getRealPath()),
45
            ]);
46
47
            /**
48
             * Images
49
             */
50
            if (str_contains($name . '_mime_type', 'image')) {
51
                list($width, $height) = (getimagesize($request->file($name)->getRealPath()));
52
                $request->merge(
53
                        [
54
                            $name . '_width' => $width,
55
                            $name . '_height' => $height,
56
                ]);
57
58
59
                $request->merge(
60
                        [
61
                            $name => $request->file($name)->getRealPath()
62
                ]);
63
                /* } */
64
            }
65
        }
66
67
        return $next($request);
68
    }
69
}
70