Completed
Push — master ( 5dd211...333169 )
by Mohamed
01:21
created

MediaLibraryController::store()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 13
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
namespace Microboard\Http\Controllers;
4
5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Http\JsonResponse;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Illuminate\Support\Facades\Storage;
10
use Intervention\Image\Facades\Image;
11
use Microboard\Http\Requests\Media\StoreFormRequest;
12
use Spatie\MediaLibrary\MediaCollections\Models\Media;
13
14
class MediaLibraryController extends Controller
15
{
16
    /**
17
     * @param StoreFormRequest $request
18
     * @param Filesystem $files
19
     * @return JsonResponse
20
     */
21
    public function upload(StoreFormRequest $request, Filesystem $files)
22
    {
23
        $path = storage_path('tmp');
24
25
        if (!$files->isDirectory($path)) {
26
            $files->makeDirectory($path, 0777, true);
27
        }
28
29
        $file = $request->file('file');
30
31
        $file->move($path, $name = uniqid() . '_' . trim($file->getClientOriginalName()));
32
33
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
34
            'name' => $name,
35
            'original_name' => $file->getClientOriginalName(),
36
        ], 201);
37
    }
38
39
    /**
40
     * @param StoreFormRequest $request
41
     * @return JsonResponse
42
     */
43
    public function store(StoreFormRequest $request)
44
    {
45
        $file = $request->file('file');
46
        $name = uniqid() . '_' . trim($file->getClientOriginalName());
47
        $image = Image::make($file)->stream('jpg', 80);
48
49
        Storage::disk(config('media-library.disk_name'))->put("editor/{$name}", $image);
50
51
        return response()->json([
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
52
            'url' => asset("storage/editor/{$name}"),
53
            'name' => $name
54
        ], 201);
55
    }
56
57
    /**
58
     * @param Request $request
59
     * @param Filesystem $files
60
     * @return Response
61
     */
62
    public function delete(Request $request, Filesystem $files)
63
    {
64
        if ($request->has('name')) {
65
            $path = '';
66
67
            if ($files->exists($tmp = storage_path("tmp/{$request->input('name')}"))) {
68
                $path = $tmp;
69
            }
70
71
            elseif ($files->exists($editor = storage_path("app/public/editor/{$request->input('name')}"))) {
72
                $path = $editor;
73
            }
74
75
            elseif ($media = Media::where('file_name', $request->input('name'))->first()) {
76
                $path = $media->getPath();
77
                $media->delete();
78
            }
79
80
            if ($path) {
81
                $files->delete($path);
82
            }
83
84
            return response('resource deleted successfully', 201);
85
        }
86
87
        return response('Not exists', 500);
88
    }
89
}
90