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

EventSearchController::index()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 60

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 60
rs 8.8727
c 0
b 0
f 0
cc 1
nc 1
nop 1

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;
4
5
use App\Event;
6
use App\Country;
7
use App\Teacher;
8
use App\Continent;
9
use App\EventVenue;
10
use App\EventCategory;
11
use App\BackgroundImage;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Cache;
14
use App\Http\Resources\Continent as ContinentResource;
15
16
class EventSearchController extends Controller
17
{
18
    // **********************************************************************
19
20
    /**
21
     * Display the event search results in Global Calendar Homepage.
22
     * @param  \Illuminate\Http\Request  $request
23
     * @return \Illuminate\Http\Response
24
     */
25
    public function index(Request $request)
26
    {
27
        $cacheExpireTime = 900; // expressed in seconds (15 min)
28
29
        $backgroundImages = BackgroundImage::all();
30
31
        $eventCategories = Cache::remember('categories', $cacheExpireTime, function () {
32
            return EventCategory::orderBy('name')->pluck('name', 'id');
33
        });
34
35
        // Get the countries with active events
36
        $activeEvents = Event::getActiveEvents();
37
        $countries = $activeEvents->unique('country_name')->sortBy('country_name')->pluck('country_name', 'country_id');
38
        //$cities = $activeEvents->unique('city')->toArray();
39
        $activeContinentsCountries = ContinentResource::collection(Continent::all());
40
41
        $continents = Cache::rememberForever('continents', function () {
42
            return Continent::orderBy('name')->pluck('name', 'id');
43
        });
44
45
        $venues = Cache::remember('venues', $cacheExpireTime, function () {
46
            return EventVenue::pluck('name', 'id');
47
        });
48
49
        $teachers = Cache::remember('teachers', $cacheExpireTime, function () {
50
            return Teacher::orderBy('name')->pluck('name', 'id');
51
        });
52
53
        $filters = [];
54
        $filters['keywords'] = $request->input('keywords');
55
        $filters['category'] = $request->input('category_id');
56
        $filters['country'] = $request->input('country_id');
57
        $filters['city'] = $request->input('city_name');
58
        $filters['continent'] = $request->input('continent_id');
59
        $filters['teacher'] = $request->input('teacher_id');
60
        $filters['venue'] = $request->input('venue_name');
61
        $filters['startDate'] = Event::prepareStartDate($request->input('startDate'));
62
        $filters['endDate'] = Event::prepareEndDate($request->input('endDate'));
63
64
        $events = Event::getEvents($filters, 20);
65
66
        return view('eventSearch.index', compact('events'))
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('eventCategories', $eventCategories)
69
            ->with('continents', $continents)
70
            ->with('countries', $countries)
71
            ->with('venues', $venues)
72
            ->with('teachers', $teachers)
73
            ->with('searchKeywords', $filters['keywords'])
74
            ->with('searchCategory', $filters['category'])
75
            ->with('searchCountry', $filters['country'])
76
            ->with('searchContinent', $filters['continent'])
77
            ->with('searchCity', $filters['city'])
78
            ->with('searchTeacher', $filters['teacher'])
79
            ->with('searchVenue', $filters['venue'])
80
            ->with('searchStartDate', $request->input('startDate'))
81
            ->with('searchEndDate', $request->input('endDate'))
82
            ->with('backgroundImages', $backgroundImages)
83
            ->with('activeContinentsCountries', $activeContinentsCountries);
84
    }
85
86
    /**
87
     * Display the specified resource.
88
     *
89
     * @param  \App\Event  $event
90
     * @param  int  $id
91
     * @return \Illuminate\Http\Response
92
     */
93
    public function show(Event $event, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
94
    {
95
        $event = Event::where('id', $id)->first();
96
97
        return view('eventSearch.show', compact('event'));
98
    }
99
100
    /***************************************************************************/
101
102
    /**
103
     * Return and HTML with all the events of a specific country by country CODE. (eg. http://websitename.com/eventSearch/country/SI)
104
     * this should be included in the IFRAME for the regional websites.
105
     *
106
     * @param  string $code
107
     * @return \Illuminate\Http\Response
108
     */
109
    public function EventsListByCountry($code)
110
    {
111
        $country = Country::where('code', $code)->first();
112
113
        $filters = [];
114
        $filters['keywords'] = $filters['category'] = $filters['city'] = $filters['continent'] = $filters['teacher'] = $filters['venue'] = $filters['startDate'] = $filters['endDate'] = null;
115
        $filters['country'] = $country->id;
116
        $events = Event::getEvents($filters, null);
117
118
        $cacheExpireTime = 900; // Set the duration time of the cache (15 min - 900sec)
119
        $eventCategories = Cache::remember('categories', $cacheExpireTime, function () {
120
            return EventCategory::orderBy('name')->pluck('name', 'id');
121
        });
122
123
        return view('eventSearch.index-iframe', compact('events'))
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...
124
                ->with('country', $country)
125
                ->with('eventCategories', $eventCategories);
126
    }
127
128
    /***************************************************************************/
129
}
130