Passed
Pull Request — master (#67)
by
unknown
03:29
created

AccountController::updateAvatar()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 41
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 30
nc 4
nop 1
dl 0
loc 41
rs 9.1288
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Http\Controllers\Controller;
6
use Illuminate\Http\Request;
7
use Illuminate\Http\Response;
8
use Illuminate\Support\Facades\Validator;
9
use Illuminate\Support\Facades\Storage;
10
use Auth;
11
12
class AccountController extends Controller
13
{
14
    /**
15
     * The Ajax Update Avatar.
16
     *
17
     * @param Request $request web request
18
     *
19
     * @return Response
20
     */
21
    public function updateAvatar(Request $request)
22
    {
23
        $isValid = $request->file('avatar')->isValid();
24
        if($isValid){
25
            $extension = $request->file('avatar')->extension();
26
        }else{
27
            $output=[
28
                'ret' => '400',
29
                'desc' => 'Invalid file',
30
                'data' => null
31
            ];
32
            return response()->json($output);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($output) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
33
        }
34
        $allow_extension = ['jpg','png','jpeg'];
35
        if($isValid && in_array($extension,$allow_extension)){
36
            $path = $request->file('avatar')->store('/static/img/avatar','NOJPublic');
37
38
            $user = Auth::user();
39
            $old_path = $user->avatar;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on App\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
            if($old_path != '/static/img/avatar/default.png'){
41
                Storage::disk('NOJPublic')->delete($old_path);
42
            }
43
44
            $user->avatar = '/'.$path;
0 ignored issues
show
Bug Best Practice introduced by
The property avatar does not exist on App\User. Since you implemented __set, consider adding a @property annotation.
Loading history...
Bug introduced by
Are you sure $path of type false|string can be used in concatenation? ( Ignorable by Annotation )

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

44
            $user->avatar = '/'./** @scrutinizer ignore-type */ $path;
Loading history...
45
            $user->save();
46
47
            $output=[
48
                'ret' => '200',
49
                'desc' => 'success',
50
                'data' => [
51
                    'url' => '/'.$path
52
                ]
53
            ];
54
            return response()->json($output);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($output) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
55
        }else{
56
            $output=[
57
                'ret' => '400',
58
                'desc' => 'Invalid file',
59
                'data' => null
60
            ];
61
            return response()->json($output);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($output) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
62
        }
63
64
    }
65
}
66