Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
profileController
0.00% covered (danger)
0.00%
0 / 37
0.00% covered (danger)
0.00%
0 / 4
56
0.00% covered (danger)
0.00%
0 / 1
 show
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 update
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 changePassword
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 changeImage
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2
3namespace App\Http\Controllers\Api\user\authentication;
4
5use App\Http\Controllers\Controller;
6use App\Http\Requests\Api\user\changeImageRequest;
7use App\Http\Requests\Api\user\changePasswordRequest;
8use App\Http\Requests\Api\user\updateRequest;
9use App\Http\Resources\userResource;
10use App\Models\Image;
11use App\Traits\response;
12use App\Traits\Upload;
13use Illuminate\Http\Request;
14use Illuminate\Support\Facades\DB;
15use Illuminate\Support\Facades\Hash;
16
17class profileController extends Controller
18{
19    use Upload;
20    public function show(){
21        $user = auth('user_api')->user();
22
23        return $this->success(
24            trans('auth.success'),
25            200,
26            'data',
27            new userResource($user)
28        );
29    }
30
31    public function update(updateRequest $request){
32        $user = auth('user_api')->user();
33
34        $filterRequest = $request->only(
35            'username','name'
36        );
37
38        $user->update($filterRequest);
39
40        return $this->success(trans('auth.success'),
41                                200,
42                                'data',
43                                new userResource($user)
44                            );
45    }
46
47    public function changePassword(changePasswordRequest $request){
48        $user = auth('user_api')->user();
49
50        //update user pass
51        if(Hash::check($request->old_password, $user->password)){
52            $user->password  = Hash::make($request->password);
53            $user->save();
54        } else {
55            return $this->failed(trans('api.old password is wrong'), 400);
56        }
57
58        return $this->success(trans('auth.success'), 200);
59    }
60
61    public function changeImage(changeImageRequest $request){
62        $user = auth('user_api')->user();
63
64        $path = $this->uploadImage($request->file('image'), 'uploads/users', 300);
65
66        if($user->Image == null){ //if not has image
67            Image::create([
68                'imageable_id'   => $user->id,
69                'imageable_type' => 'App\Models\User',
70                'src'            => $path,
71            ]);
72        } else { //if has image
73            $oldImage = $user->Image->src;
74            if(file_exists(base_path('public/uploads/users/') . $oldImage)){
75                unlink(base_path('public/uploads/users/') . $oldImage);
76            }
77
78            $user->Image->src = $path;
79            $user->Image->save();
80        }
81
82        return $this->success(trans('auth.success'), 200);
83    }
84}