Passed
Push — master ( c430e0...09e8fc )
by Davide
02:42 queued 11s
created

EventVenueController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 267
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 95
dl 0
loc 267
rs 10
c 0
b 0
f 0

12 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 15 2
A modal() 0 5 1
A edit() 0 13 4
A __construct() 0 3 1
A storeFromModal() 0 11 1
A saveOnDb() 0 18 2
A create() 0 10 1
A destroy() 0 6 1
A index() 0 41 4
A eventsVenueValidator() 0 13 1
A show() 0 8 1
A update() 0 13 2
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use App\User;
0 ignored issues
show
Bug introduced by
The type App\User was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Validator;
7
use App\Country;
0 ignored issues
show
Bug introduced by
The type App\Country was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use App\EventVenue;
0 ignored issues
show
Bug introduced by
The type App\EventVenue was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Illuminate\Support\Str;
10
use Illuminate\Http\Request;
11
use Illuminate\Support\Facades\DB;
12
use Illuminate\Support\Facades\Auth;
13
use Illuminate\Support\Facades\Cache;
14
15
class EventVenueController extends Controller
16
{
17
    /* Restrict the access to this resource just to logged in users except show view */
18
    public function __construct()
19
    {
20
        $this->middleware('auth', ['except' => ['show']]);
21
    }
22
23
    /***************************************************************************/
24
25
    /**
26
     * Display a listing of the resource.
27
     *
28
     * @return \Illuminate\Http\Response
29
     */
30
    public function index(Request $request)
31
    {
32
        $cacheExpireTime = 900; // Set the duration time of the cache (15 min - 900sec)
33
        $countries = Cache::remember('countries', $cacheExpireTime, function () {
34
            return Country::orderBy('name')->pluck('name', 'id');
35
        });
36
37
        $searchKeywords = $request->input('keywords');
38
        $searchCountry = $request->input('country_id');
39
40
        // To show just the veues created by the the user - If admin or super admin is set to null show all the venues
41
        $authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null;
42
43
        if ($searchKeywords || $searchCountry) {
44
            $eventVenues = DB::table('event_venues')
45
                ->when($authorUserId, function ($query, $authorUserId) {
46
                    return $query->where('created_by', $authorUserId);
47
                })
48
                ->when($searchKeywords, function ($query, $searchKeywords) {
49
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
50
                })
51
                ->when($searchCountry, function ($query, $searchCountry) {
52
                    return $query->where('country_id', '=', $searchCountry);
53
                })
54
                ->orderBy('name')
55
                ->paginate(20);
56
        } else {
57
            $eventVenues = EventVenue::
58
                when($authorUserId, function ($query, $authorUserId) {
59
                    return $query->where('created_by', $authorUserId);
60
                })
61
                ->orderBy('name')
62
                ->paginate(20);
63
        }
64
        //dd(DB::getQueryLog());
65
66
        return view('eventVenues.index', compact('eventVenues'))
67
                ->with('i', (request()->input('page', 1) - 1) * 20)
68
                ->with('countries', $countries)
69
                ->with('searchKeywords', $searchKeywords)
70
                ->with('searchCountry', $searchCountry);
71
    }
72
73
    /***************************************************************************/
74
75
    /**
76
     * Show the form for creating a new resource.
77
     *
78
     * @return \Illuminate\Http\Response
79
     */
80
    public function create()
81
    {
82
        $authorUserId = $this->getLoggedUser();
83
        $users = User::pluck('name', 'id');
84
        $countries = Country::getCountries();
85
86
        return view('eventVenues.create')
87
                ->with('countries', $countries)
88
                ->with('users', $users)
89
                ->with('authorUserId', $authorUserId);
90
    }
91
92
    /***************************************************************************/
93
94
    /**
95
     * Store a newly created resource in storage.
96
     *
97
     * @param  \Illuminate\Http\Request  $request
98
     * @return \Illuminate\Http\Response
99
     */
100
    public function store(Request $request)
101
    {
102
103
        // Validate form datas
104
        $validator = $this->eventsVenueValidator($request);
105
        if ($validator->fails()) {
106
            return back()->withErrors($validator)->withInput();
107
        }
108
109
        $eventVenue = new EventVenue();
110
111
        $this->saveOnDb($request, $eventVenue);
112
113
        return redirect()->route('eventVenues.index')
114
                        ->with('success', __('messages.venue_added_successfully'));
115
    }
116
117
    /***************************************************************************/
118
119
    /**
120
     * Display the specified resource.
121
     *
122
     * @param  \App\EventVenue  $eventVenue
123
     * @return \Illuminate\Http\Response
124
     */
125
    public function show(EventVenue $eventVenue)
126
    {
127
        $country = DB::table('countries')
128
                ->select('id', 'name', 'continent_id')
129
                ->where('id', $eventVenue->country_id)
130
                ->first();
131
132
        return view('eventVenues.show', compact('eventVenue'))->with('country', $country);
133
    }
134
135
    /***************************************************************************/
136
137
    /**
138
     * Show the form for editing the specified resource.
139
     *
140
     * @param  \App\EventVenue  $eventVenue
141
     * @return \Illuminate\Http\Response
142
     */
143
    public function edit(EventVenue $eventVenue)
144
    {
145
        if (Auth::user()->id == $eventVenue->created_by || Auth::user()->isSuperAdmin() || Auth::user()->isAdmin()) {
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
Bug introduced by
The method isAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        if (Auth::user()->id == $eventVenue->created_by || Auth::user()->isSuperAdmin() || Auth::user()->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
Bug introduced by
The method isSuperAdmin() does not exist on Illuminate\Contracts\Auth\Authenticatable. It seems like you code against a sub-type of Illuminate\Contracts\Auth\Authenticatable such as Illuminate\Foundation\Auth\User. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

145
        if (Auth::user()->id == $eventVenue->created_by || Auth::user()->/** @scrutinizer ignore-call */ isSuperAdmin() || Auth::user()->isAdmin()) {
Loading history...
146
            $authorUserId = $this->getLoggedAuthorId();
147
            $users = User::pluck('name', 'id');
148
            $countries = Country::getCountries();
149
150
            return view('eventVenues.edit', compact('eventVenue'))
151
                ->with('countries', $countries)
152
                ->with('users', $users)
153
                ->with('authorUserId', $authorUserId);
154
        } else {
155
            return redirect()->route('home')->with('message', __('auth.not_allowed_to_access'));
156
        }
157
    }
158
159
    /***************************************************************************/
160
161
    /**
162
     * Update the specified resource in storage.
163
     *
164
     * @param  \Illuminate\Http\Request  $request
165
     * @param  \App\EventVenue  $eventVenue
166
     * @return \Illuminate\Http\Response
167
     */
168
    public function update(Request $request, EventVenue $eventVenue)
169
    {
170
        // Validate form datas
171
        $validator = $this->eventsVenueValidator($request);
172
        if ($validator->fails()) {
173
            return back()->withErrors($validator)->withInput();
174
        }
175
176
        //$eventVenue->update($request->all());
177
        $this->saveOnDb($request, $eventVenue);
178
179
        return redirect()->route('eventVenues.index')
180
                        ->with('success', __('messages.venue_updated_successfully'));
181
    }
182
183
    /***************************************************************************/
184
185
    /**
186
     * Remove the specified resource from storage.
187
     *
188
     * @param  \App\EventVenue  $eventVenue
189
     * @return \Illuminate\Http\Response
190
     */
191
    public function destroy(EventVenue $eventVenue)
192
    {
193
        $eventVenue->delete();
194
195
        return redirect()->route('eventVenues.index')
196
                        ->with('success', __('messages.venue_deleted_successfully'));
197
    }
198
199
    /***************************************************************************/
200
201
    /**
202
     * Save the record on DB.
203
     *
204
     * @param  \App\EventVenue  $eventVenue
205
     * @return \Illuminate\Http\Response
206
     */
207
    public function saveOnDb($request, $eventVenue)
208
    {
209
        $eventVenue->name = $request->get('name');
210
        //$eventVenue->description = $request->get('description');
211
        $eventVenue->description = clean($request->get('description'));
0 ignored issues
show
Bug introduced by
The function clean was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

211
        $eventVenue->description = /** @scrutinizer ignore-call */ clean($request->get('description'));
Loading history...
212
        $eventVenue->continent_id = Country::where('id', $request->get('country_id'))->pluck('continent_id')->first();
213
        $eventVenue->country_id = $request->get('country_id');
214
        $eventVenue->city = $request->get('city');
215
        $eventVenue->state_province = $request->get('state_province');
216
        $eventVenue->address = $request->get('address');
217
        $eventVenue->zip_code = $request->get('zip_code');
218
        $eventVenue->website = $request->get('website');
219
220
        if (! $eventVenue->slug) {
221
            $eventVenue->slug = Str::slug($eventVenue->name, '-').rand(10000, 100000);
222
        }
223
        $eventVenue->created_by = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
224
        $eventVenue->save();
225
    }
226
227
    /***************************************************************************/
228
229
    /**
230
     * Open a modal in the event view when create teachers is clicked.
231
     *
232
     * @return \Illuminate\Http\Response
233
     */
234
    public function modal()
235
    {
236
        $countries = Country::getCountries();
237
238
        return view('eventVenues.modal')->with('countries', $countries);
239
    }
240
241
    /***************************************************************************/
242
243
    /**
244
     * Store a newly created teacher from the create event view modal in storage.
245
     *
246
     * @param  \Illuminate\Http\Request  $request
247
     * @return \Illuminate\Http\Response
248
     */
249
    public function storeFromModal(Request $request)
250
    {
251
        $eventVenue = new EventVenue();
252
253
        request()->validate([
254
            'name' => 'required',
255
        ]);
256
257
        $this->saveOnDb($request, $eventVenue);
258
259
        return redirect()->back()->with('message', __('messages.venue_added_successfully'));
260
    }
261
262
    /***************************************************************************/
263
264
    /**
265
     * Return the Venue validator with all the defined constraint.
266
     *
267
     * @return \Illuminate\Validation\Validator
268
     */
269
    public function eventsVenueValidator($request)
270
    {
271
        $rules = [
272
            'name' => 'required',
273
            'city' => 'required',
274
            'country_id' => 'required',
275
            'website' => 'nullable|url',
276
        ];
277
        $messages = [];
278
279
        $validator = Validator::make($request->all(), $rules, $messages);
280
281
        return $validator;
282
    }
283
}
284