Completed
Push — master ( ba992d...dc755b )
by Davide
07:03
created

EventVenueController::saveOnDb()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use Validator;
7
use App\Country;
8
use App\EventVenue;
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 View Code Duplication
    public function index(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
    public function create()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
    {
82
        $authorUserId = $this->getLoggedUser();
83
        $users = User::pluck('name', 'id');
84
        $countries = Country::getCountries();
85
86
        return view('eventVenues.create')
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
    public function store(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
    public function edit(EventVenue $eventVenue)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
Bug introduced by
The method isSuperAdmin() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method isAdmin() does not seem to exist on object<Illuminate\Contracts\Auth\Authenticatable>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
146
            $authorUserId = $this->getLoggedAuthorId();
147
            $users = User::pluck('name', 'id');
148
            $countries = Country::getCountries();
149
150
            return view('eventVenues.edit', compact('eventVenue'))
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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\Teacher  $teacher
0 ignored issues
show
Bug introduced by
There is no parameter named $teacher. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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'));
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;
224
        $eventVenue->save();
225
    }
226
227
    /***************************************************************************/
228
229
    /**
230
     * Open a modal in the event view when create teachers is clicked.
231
     *
232
     * @return view
233
     */
234
    public function modal()
235
    {
236
        $countries = Country::getCountries();
237
238
        return view('eventVenues.modal')->with('countries', $countries);
0 ignored issues
show
Bug introduced by
The method with does only exist in Illuminate\View\View, but not in Illuminate\Contracts\View\Factory.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
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 View Code Duplication
    public function storeFromModal(Request $request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function eventsVenueValidator($request)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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