Completed
Push — master ( f8ccd7...a2ea8b )
by Sherif
13:39
created

Media::uploadImage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 2
1
<?php namespace App\Modules\V1\Core\Utl;
2
3
class Media
4
{
5
    /**
6
     * Upload the given image.
7
     * 
8
     * @param  object  $image
9
     * @param  string  $dir
10
     * @return string
11
     */
12
    public function uploadImage($image, $dir) 
13
    {
14
       $response        = [];
0 ignored issues
show
Unused Code introduced by
$response 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...
15
       $image           = $image;
0 ignored issues
show
Bug introduced by
Why assign $image to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
16
       $imageName       =  str_slug('image' . uniqid() . time() . '_' . $image->getClientOriginalName());
17
       $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
18
19
       ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false;
20
       $image->move($destinationPath, $imageName);
21
22
       return url($destinationPath . $imageName);
23
    }
24
25
    /**
26
     * Upload the given image.
27
     * 
28
     * @param  object  $image
29
     * @param  string  $dir
30
     * @return string
31
     */
32
    public function uploadImageBas64($image, $dir) 
33
    {
34
        $response        = [];
0 ignored issues
show
Unused Code introduced by
$response 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...
35
        $image           = $image;
0 ignored issues
show
Bug introduced by
Why assign $image to itself?

This checks looks for cases where a variable has been assigned to itself.

This assignement can be removed without consequences.

Loading history...
36
        $imageName       = 'image' . uniqid() . time() . '.jpg';
37
        $destinationPath = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR;
38
39
        ! file_exists($destinationPath) ? \File::makeDirectory($destinationPath) : false;
40
41
        $base  = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image));
42
        $image = \Image::make($base)->save($destinationPath . $imageName);
0 ignored issues
show
Unused Code introduced by
$image 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...
43
44
        return url($destinationPath . $imageName);
45
    }
46
47
    /**
48
     * Delete the given image.
49
     * 
50
     * @param  object  $path
51
     * @param  string  $dir
52
     * @return void
53
     */
54
    public function deleteImage($path, $dir) 
55
    {   
56
        $arr      = explode('/', $path);
57
        $path     = 'media' . DIRECTORY_SEPARATOR . $dir . DIRECTORY_SEPARATOR . end($arr);
58
        if (\File::exists($path)) 
59
        {
60
            \File::delete($path);
61
        }
62
    }
63
}
64