Completed
Push — master ( 0f2a67...6bf02f )
by Mohamed
01:36
created

MediaLibraryController::upload()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 17
rs 9.7
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
13
class MediaLibraryController extends Controller
14
{
15
    /**
16
     * @param StoreFormRequest $request
17
     * @param Filesystem $files
18
     * @return JsonResponse
19
     */
20
    public function upload(StoreFormRequest $request, Filesystem $files)
21
    {
22
        $path = storage_path('tmp');
23
24
        if (!$files->isDirectory($path)) {
25
            $files->makeDirectory($path, 0777, true);
26
        }
27
28
        $file = $request->file('file');
29
30
        $file->move($path, $name = uniqid() . '_' . trim($file->getClientOriginalName()));
31
32
        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...
33
            'name' => $name,
34
            'original_name' => $file->getClientOriginalName(),
35
        ], 201);
36
    }
37
38
    /**
39
     * @param StoreFormRequest $request
40
     * @return JsonResponse
41
     */
42
    public function store(StoreFormRequest $request)
43
    {
44
        $file = $request->file('file');
45
        $name = uniqid() . '_' . trim($file->getClientOriginalName());
46
        $image = Image::make($file)->stream('jpg', 80);
47
48
        Storage::disk(config('media-library.disk_name'))->put("editor/{$name}", $image);
49
50
        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...
51
            'url' => asset("storage/editor/{$name}"),
52
            'name' => $name
53
        ], 201);
54
    }
55
56
    /**
57
     * @param Request $request
58
     * @param Filesystem $files
59
     * @return Response
60
     */
61
    public function delete(Request $request, Filesystem $files)
62
    {
63
        if ($request->has('name')) {
64
            $path = '';
65
66
            if ($files->exists($tmp = storage_path("tmp/{$request->input('name')}"))) {
67
                $path = $tmp;
68
            }
69
            elseif ($files->exists($editor = storage_path("app/public/editor/{$request->input('name')}"))) {
70
                $path = $editor;
71
            }
72
73
            $files->delete($path);
74
            return response('resource deleted successfully', 201);
75
        }
76
77
        return response('Not exists', 500);
78
    }
79
}
80