SpotsController::update()   B
last analyzed

Complexity

Conditions 7
Paths 24

Size

Total Lines 59
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 39
c 1
b 0
f 0
nc 24
nop 2
dl 0
loc 59
rs 8.3626

How to fix   Long Method   

Long Method

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:

1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Spot;
6
use App\User;
7
use Illuminate\Http\Request;
8
use Yajra\Datatables\Datatables;
9
use Illuminate\Support\Facades\DB;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Support\Facades\Storage;
12
use Intervention\Image\Facades\Image;
13
14
class SpotsController extends Controller
15
{
16
    /**
17
     * List of all Spots
18
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
19
     */
20
    public function index()
21
    {
22
        return view('admin.spots.index');
23
    }
24
25
    public function indexData()
26
    {
27
        $spots = Spot::with('country')
28
            ->selectRaw('spots.id,
29
                spots.name as spot_name, 
30
                spots.is_approved, 
31
                spots.is_featured, 
32
                spots.city,
33
                spots.created_at, 
34
                country_id'
35
            );
36
37
        $table = Datatables::of($spots);
38
39
        $table->editColumn('is_approved', function (Spot $spot) {
40
            return ($spot->is_approved ? '<i class="fa fa-check"></i>' : '<i class="fa fa-close"></i>');
41
        });
42
43
        $table->editColumn('is_featured', function (Spot $spot) {
44
           return ($spot->is_featured ? '<i class="fa fa-star"></i>' : '');
45
        });
46
47
        $table->addColumn('actions', function (Spot $spot) {
48
            return
49
                '<a href="' . route('admin.spots.edit', $spot->id) . '">
50
                    <button class="btn btn-block btn-default btn-xs">
51
                        <i class="fa fa-edit"></i> Editar
52
                    </button>
53
                </a>';
54
        });
55
56
        $table->rawColumns([
57
            'is_approved',
58
            'is_featured',
59
            'actions',
60
        ]);
61
62
        return $table->make(true);
63
    }
64
65
    /**
66
     * Show form to complete a Spot submission data
67
     * @param $spotId
68
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
69
     */
70
    public function edit(String $spotId)
71
    {
72
        $spot = Spot::findOrFail($spotId);
73
74
        $countriesList = DB::table('countries')
75
            ->select('id', 'name_pt')
76
            ->where('continent_id', 2)
77
            ->orWhere('continent_id', 3)
78
            ->orWhere('continent_id', 6)
79
            ->orWhere('continent_id', 7)
80
            ->orderBy('name_pt', 'asc')
81
            ->get();
82
83
        return view('admin.spots.edit', compact('spot', 'countriesList'));
84
    }
85
86
    /**
87
     * Submit a new form to update a Spot
88
     * @param Request $request
89
     * @param $spotId
90
     * @return \Illuminate\Http\RedirectResponse
91
     */
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
    }
152
153
    /**
154
     * Delete an existing spot
155
     * @param $spotId
156
     * @return \Illuminate\Http\RedirectResponse
157
     */
158
    public function destroy($spotId)
159
    {
160
        $spot = Spot::findOrFail($spotId);
161
        $spotName = $spot->name;
162
163
        $spot->delete();
164
165
        return redirect()->route('admin.spots.index')->with('success', "Spot $spotName apagado com sucesso.");
166
    }
167
}
168