Passed
Pull Request — master (#610)
by John
22:48
created

ImageHostingController::generate()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
nc 4
nop 1
dl 0
loc 30
rs 9.2568
c 1
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Tool\Ajax;
4
5
use App\Models\Eloquent\Tool\ImageHosting;
6
use App\Http\Controllers\Controller;
7
use Illuminate\Http\Request;
8
use Illuminate\Http\Response;
9
use Illuminate\Validation\Rule;
10
use App\Models\ResponseModel;
11
use Auth;
12
13
class ImageHostingController extends Controller
14
{
15
    /**
16
     * Generate a new pastebin.
17
     *
18
     * @return Response
19
     */
20
    public function generate(Request $request)
21
    {
22
        $user=Auth::user();
23
24
        if (!$user->hasPermission(26)) {
0 ignored issues
show
Bug introduced by
The method hasPermission() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of said class. However, the method does not exist in Illuminate\Auth\GenericUser. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
        if (!$user->/** @scrutinizer ignore-call */ hasPermission(26)) {
Loading history...
25
            return ResponseModel::err(2001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
26
        }
27
28
        $isValid=$request->file('image')->isValid();
29
        if ($isValid) {
30
            $extension=$request->file('image')->extension();
31
        } else {
32
            return ResponseModel::err(1005);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1005) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
33
        }
34
35
        $allow_extension=['jpg', 'png', 'jpeg', 'gif', 'bmp'];
36
        if ($isValid && in_array($extension, $allow_extension)) {
37
            $path=$request->file('image')->store('/static/img/upload', 'NOJPublic');
38
            $id=ImageHosting::create([
39
                'user_id' => $user->id,
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
40
                'relative_path' => "/$path"
41
            ])->id;
42
            return ResponseModel::success(200, null, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon..., array('id' => $id)))) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
43
                'relative_path' => "/$path",
44
                'path' => url($path),
0 ignored issues
show
Bug introduced by
It seems like $path can also be of type false; however, parameter $path of url() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
                'path' => url(/** @scrutinizer ignore-type */ $path),
Loading history...
45
                'id' => $id,
46
                'redirect_url' => route('tool.imagehosting.detail', ['id'=>$id]),
47
            ]);
48
        } else {
49
            return ResponseModel::err(1005);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(1005) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
50
        }
51
    }
52
}
53