1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Http\Controllers; |
4
|
|
|
|
5
|
|
|
use App\Http\Requests\UserUpdateRequest; |
6
|
|
|
use App\Repositories\UserRepository as User; |
7
|
|
|
use App\Transformers\UserTransformer; |
8
|
|
|
use Chrisbjr\ApiGuard\Http\Controllers\ApiGuardController; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class UserController. |
13
|
|
|
*/ |
14
|
|
|
class UserController extends ApiGuardController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var UserTransformer |
18
|
|
|
*/ |
19
|
|
|
protected $userTransformer; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var array |
23
|
|
|
*/ |
24
|
|
|
protected $apiMethods = [ |
25
|
|
|
'getAllUsers' => [ |
26
|
|
|
'keyAuthentication' => false, |
27
|
|
|
], |
28
|
|
|
'show' => [ |
29
|
|
|
'keyAuthentication' => false, |
30
|
|
|
], |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* UserController constructor. |
35
|
|
|
* |
36
|
|
|
* @param User $user |
37
|
|
|
* @param UserTransformer $userTransformer |
38
|
|
|
*/ |
39
|
5 |
|
public function __construct(User $user, UserTransformer $userTransformer) |
40
|
|
|
{ |
41
|
5 |
|
parent::__construct(); |
42
|
|
|
|
43
|
5 |
|
$this->userTransformer = $userTransformer; |
44
|
5 |
|
$this->user = $user; |
45
|
5 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get all users. |
49
|
|
|
* |
50
|
|
|
* @return mixed |
51
|
|
|
*/ |
52
|
4 |
|
public function getAllUsers() |
53
|
|
|
{ |
54
|
4 |
|
$user = $this->user->all(); |
55
|
|
|
|
56
|
4 |
|
return $this->response->withCollection($user, $this->userTransformer); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Get user find id. |
61
|
|
|
* |
62
|
|
|
* @param $id |
63
|
|
|
* |
64
|
|
|
* @return mixed |
65
|
|
|
*/ |
66
|
1 |
|
public function show($id) |
67
|
|
|
{ |
68
|
1 |
|
$user = $this->user->findOrFail($id); |
69
|
|
|
|
70
|
1 |
|
return $this->response->withItem($user, $this->userTransformer); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Update user. |
75
|
|
|
* |
76
|
|
|
* @param UserUpdateRequest $request |
77
|
|
|
* @param $id |
78
|
|
|
* |
79
|
|
|
* @return mixed |
80
|
|
|
*/ |
81
|
2 |
|
public function update(UserUpdateRequest $request, $id) |
82
|
|
|
{ |
83
|
2 |
|
$user = $this->user->findOrFail($id); |
84
|
|
|
|
85
|
2 |
|
if ($request->avatar != '') { |
86
|
1 |
|
$path = $this->storeImage($request->avatar, $user); |
87
|
|
|
} else { |
88
|
1 |
|
$path = $user->avatar; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$data = [ |
92
|
2 |
|
'name' => $request->input('name'), |
93
|
2 |
|
'email' => $request->input('email'), |
94
|
2 |
|
'password' => bcrypt($request->input('password')), |
|
|
|
|
95
|
2 |
|
'avatar' => $path, |
96
|
|
|
]; |
97
|
|
|
|
98
|
2 |
|
$this->user->update($data, $id); |
99
|
|
|
|
100
|
2 |
|
return $this->response->withItem($user, $this->userTransformer); |
|
|
|
|
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Delete user. |
105
|
|
|
* |
106
|
|
|
* @param $id |
107
|
|
|
*/ |
108
|
1 |
|
public function delete($id) |
109
|
|
|
{ |
110
|
1 |
|
$this->user->delete($id); |
111
|
1 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Store image avatar. |
115
|
|
|
* |
116
|
|
|
* @param $image |
117
|
|
|
* @param $user |
118
|
|
|
* |
119
|
|
|
* @return mixed |
120
|
|
|
*/ |
121
|
1 |
|
private function storeImage($image, $user) |
122
|
|
|
{ |
123
|
1 |
|
$path = Storage::url('images/avatars/'.$user->id.'.'.$image->getClientOriginalExtension()); |
124
|
|
|
|
125
|
1 |
|
Storage::disk('public')->put( |
126
|
1 |
|
'images/avatars/'.$user->id.'.'.$image->getClientOriginalExtension(), |
127
|
1 |
|
file_get_contents($image->getRealPath()) |
128
|
|
|
); |
129
|
|
|
|
130
|
1 |
|
return $path; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: