| Conditions | 8 |
| Paths | 15 |
| Total Lines | 62 |
| Code Lines | 34 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 36 | public function profileUpdate(Request $request) |
||
| 37 | { |
||
| 38 | $user = User::where('id', $request->user()->id)->first(); |
||
| 39 | |||
| 40 | if (!$user) { |
||
| 41 | return response()->json(['status' => 'error', 'message' => 'Opss. Usuário não foi encontrado, favor verifique se esta logado.']); |
||
| 42 | } |
||
| 43 | |||
| 44 | $rules = [ |
||
| 45 | 'name' => 'required', |
||
| 46 | 'email' => 'required|email|unique:users,email,' . $request->user()->id, |
||
| 47 | 'password' => 'required|string|min:6|confirmed', |
||
| 48 | ]; |
||
| 49 | |||
| 50 | $validator = \Validator::make($request->all(), $rules); |
||
| 51 | |||
| 52 | if ($validator->fails()) { |
||
| 53 | return response()->json(['status' => 'error', 'errors' => $validator->errors()]); |
||
| 54 | } |
||
| 55 | |||
| 56 | /* |
||
| 57 | if (!Hash::check($request->input('password_current'), $user->password)) { |
||
| 58 | return response()->json(['status' => 'error', 'message' => 'Opss. A senha atual informada está incorreta, favor verificar.']); |
||
| 59 | } |
||
| 60 | */ |
||
| 61 | |||
| 62 | $data = [ |
||
| 63 | 'name' => $request->input('name'), |
||
| 64 | 'email' => $request->input('email'), |
||
| 65 | 'password' => bcrypt($request->input('password')), |
||
| 66 | ]; |
||
| 67 | |||
| 68 | if ($request->image) { |
||
| 69 | $time = time(); |
||
| 70 | $directoryPai = 'profiles'; |
||
| 71 | $directoryImage = $directoryPai . DIRECTORY_SEPARATOR . 'profile_id_' . $user->id; |
||
| 72 | $ext = substr($request->image, 11, strpos($request->image, ';') - 11); |
||
| 73 | $urlImage = $directoryImage . DIRECTORY_SEPARATOR . $time . '.' . $ext; |
||
| 74 | $file = str_replace('data:image/' . $ext . ';base64,', '', $request->image); |
||
| 75 | $file = base64_decode($file); |
||
| 76 | |||
| 77 | if (!file_exists($directoryPai)) { |
||
| 78 | mkdir($directoryPai, 0777); |
||
| 79 | } |
||
| 80 | if ($user->image) { |
||
| 81 | $imgUser = str_replace(asset('/'), '', $user->image); |
||
| 82 | if (file_exists($imgUser)) { |
||
| 83 | unlink($imgUser); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | if (!file_exists($directoryImage)) { |
||
| 87 | mkdir($directoryImage, 0777); |
||
| 88 | } |
||
| 89 | |||
| 90 | file_put_contents($urlImage, $file); |
||
| 91 | $data['image'] = $urlImage; |
||
| 92 | } |
||
| 93 | |||
| 94 | $user->update($data); |
||
| 95 | //$user->image = asset($user->image); |
||
| 96 | |||
| 97 | return response()->json(['status' => 'success', 'data' => $user]); |
||
| 98 | } |
||
| 99 | } |