| Conditions | 6 |
| Paths | 8 |
| Total Lines | 57 |
| Code Lines | 37 |
| 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 |
||
| 42 | public function store(Request $request) |
||
| 43 | { |
||
| 44 | $request->validate([ |
||
| 45 | 'name' => 'required|string|max:60', |
||
| 46 | 'city' => 'required|string|max:35', |
||
| 47 | 'country' => 'required|exists:countries,id', |
||
| 48 | 'address' => 'required|string|nullable|max:255', |
||
| 49 | 'phone-number' => 'string|nullable|max:20', |
||
| 50 | 'email' => 'email|nullable', |
||
| 51 | 'website' => 'url|nullable', |
||
| 52 | 'image' => 'image|nullable|max:2000', |
||
| 53 | ]); |
||
| 54 | |||
| 55 | $image = $request->file('image'); |
||
| 56 | $imagePath = null; |
||
| 57 | |||
| 58 | if ($image) { |
||
| 59 | $imagePath = $image->hashName('spots'); |
||
| 60 | |||
| 61 | $img = Image::make($image); |
||
| 62 | $img->fit(675, 450); |
||
| 63 | |||
| 64 | Storage::put($imagePath, (string) $img->encode()); |
||
| 65 | } |
||
| 66 | |||
| 67 | $name = $request->get('name'); |
||
| 68 | |||
| 69 | $slug = str_slug($name); |
||
| 70 | $slugIndex = 0; |
||
| 71 | |||
| 72 | // If slug already in use, add an index to the slug |
||
| 73 | while (Spot::where('slug', $slug)->count()) { |
||
| 74 | $slugIndex++; |
||
| 75 | $slug = "$slug-$slugIndex"; |
||
| 76 | } |
||
| 77 | |||
| 78 | $spot = Spot::create([ |
||
| 79 | 'name' => $name, |
||
| 80 | 'slug' => $slug, |
||
| 81 | 'country_id' => $request->get('country'), |
||
| 82 | 'city' => $request->get('city'), |
||
| 83 | 'address' => $request->get('address'), |
||
| 84 | 'email' => $request->get('email'), |
||
| 85 | 'phone_number' => $request->get('phone-number'), |
||
| 86 | 'website' => $request->get('website'), |
||
| 87 | 'image' => $imagePath, |
||
| 88 | ]); |
||
| 89 | |||
| 90 | $recipientAddress = config('mail.to')['address']; |
||
| 91 | |||
| 92 | if ($recipientAddress && (config('mail.status') || app()->environment(['production']))) { |
||
|
|
|||
| 93 | // Send e-mail to admin alerting about new spot submission |
||
| 94 | Mail::to($recipientAddress)->send(new SpotSubmitted($spot)); |
||
| 95 | } |
||
| 96 | |||
| 97 | return redirect()->route('home')->with('success', |
||
| 98 | "<p>Spot <strong>$name</strong> submetido com sucesso!</p><p>Será publicado em breve, após revisão por |
||
| 99 | parte da nossa equipa. Obrigado!</p>"); |
||
| 118 |