| Conditions | 7 |
| Paths | 24 |
| Total Lines | 59 |
| Code Lines | 39 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 92 | public function update(Request $request, $spotId) |
||
| 93 | { |
||
| 94 | $request->validate([ |
||
| 95 | 'name' => 'required|string|max:60', |
||
| 96 | 'city' => 'required|string|max:35', |
||
| 97 | 'country' => 'required|exists:countries,id', |
||
| 98 | 'address' => 'required|string|nullable|max:255', |
||
| 99 | 'phone-number' => 'string|nullable|max:20', |
||
| 100 | 'email' => 'email|nullable', |
||
| 101 | 'website' => 'url|nullable', |
||
| 102 | 'latitude-longitude' => 'string|nullable', |
||
| 103 | 'image' => 'image|nullable|max:2000', |
||
| 104 | ]); |
||
| 105 | |||
| 106 | $latitude = null; |
||
| 107 | $longitude = null; |
||
| 108 | |||
| 109 | // If latitude/longitude were submitted, separate the values |
||
| 110 | if ($request->has('latitude-longitude') && $request->get('latitude-longitude')) { |
||
| 111 | $latitudeLongitude = explode(',', $request->get('latitude-longitude')); |
||
| 112 | |||
| 113 | if (isset($latitudeLongitude[0], $latitudeLongitude[1])) { |
||
| 114 | $latitude = $latitudeLongitude[0]; |
||
| 115 | $longitude = $latitudeLongitude[1]; |
||
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | $spot = Spot::findOrFail($spotId); |
||
| 120 | |||
| 121 | $dataToUpdate = [ |
||
| 122 | 'name' => $request->get('name'), |
||
| 123 | 'address' => $request->get('address'), |
||
| 124 | 'email' => $request->get('email'), |
||
| 125 | 'phone_number' => $request->get('phone-number'), |
||
| 126 | 'city' => $request->get('city'), |
||
| 127 | 'country_id' => $request->get('country'), |
||
| 128 | 'website' => $request->get('website'), |
||
| 129 | 'latitude' => $latitude, |
||
| 130 | 'longitude' => $longitude, |
||
| 131 | 'is_approved' => $request->has('approved') ? true : false, |
||
| 132 | 'is_featured' => $request->has('featured') ? true : false, |
||
| 133 | ]; |
||
| 134 | |||
| 135 | $image = $request->file('image'); |
||
| 136 | |||
| 137 | if ($image) { |
||
| 138 | $imagePath = $image->hashName('spots'); |
||
| 139 | |||
| 140 | $img = Image::make($image); |
||
| 141 | $img->fit(675, 450); |
||
| 142 | |||
| 143 | Storage::put($imagePath, (string) $img->encode()); |
||
| 144 | |||
| 145 | $dataToUpdate['image'] = $imagePath; |
||
| 146 | } |
||
| 147 | |||
| 148 | $spot->update($dataToUpdate); |
||
| 149 | |||
| 150 | return redirect()->route('admin.spots.edit', $spot->id)->with('success', "Spot $spot->name gravado com sucesso."); |
||
| 151 | } |
||
| 168 |