Completed
Push — master ( ec2395...0dfd17 )
by Sherif
02:09
created

Media   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 5
lcom 0
cbo 0
dl 0
loc 61
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadImage() 0 5 1
A uploadImageBas64() 0 11 2
A deleteImage() 0 4 1
A saveImage() 0 8 1
1
<?php namespace App\Modules\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
        $image = \Image::make($image);
15
        return $this->saveImage($image, $dir);
16
    }
17
18
    /**
19
     * Upload the given image.
20
     *
21
     * @param  object  $image
22
     * @param  string  $dir
23
     * @return string
24
     */
25
    public function uploadImageBas64($image, $dir)
26
    {
27
        if (! strlen($image)) {
28
            return null;
29
        }
30
31
        $base  = base64_decode(preg_replace('#^data:image/\w+;base64,#i', '', $image));
32
        $image = \Image::make($base);
33
34
        return $this->saveImage($image, $dir);
35
    }
36
37
    /**
38
     * Delete the given image.
39
     *
40
     * @param  object $path
41
     * @return void
42
     */
43
    public function deleteImage($path)
44
    {
45
        \Storage::delete($path);
0 ignored issues
show
Bug introduced by
The method delete() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
46
    }
47
48
    /**
49
     * Save the given image.
50
     *
51
     * @param  object  $image
52
     * @param  string  $dir
53
     * @return string
54
     */
55
    protected function saveImage($image, $dir)
56
    {
57
        $imageName = 'image'.uniqid().time().'.jpg';
58
        $path      = 'public'.DIRECTORY_SEPARATOR.'uploads'.DIRECTORY_SEPARATOR.$dir.DIRECTORY_SEPARATOR.$imageName;
59
        \Storage::put($path, $image->stream());
0 ignored issues
show
Bug introduced by
The method put() does not seem to exist on object<Illuminate\Contracts\Filesystem\Factory>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
60
61
        return $path;
62
    }
63
}
64