Completed
Push — master ( a86d17...8d920c )
by Davide
08:03 queued 03:47
created

EventSearchController::index()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 59
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 59
rs 9.248
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;
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...
7
use App\Teacher;
8
use App\Continent;
0 ignored issues
show
Bug introduced by
The type App\Continent 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 App\EventVenue;
10
use App\EventCategory;
11
use App\BackgroundImage;
0 ignored issues
show
Bug introduced by
The type App\BackgroundImage 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...
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\Cache;
14
use App\Http\Resources\Continent as ContinentResource;
0 ignored issues
show
Bug introduced by
The type App\Http\Resources\Continent 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...
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'))
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. ( Ignorable by Annotation )

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

93
    public function show(/** @scrutinizer ignore-unused */ Event $event, $id)

This check looks for 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'))
124
                ->with('country', $country)
125
                ->with('eventCategories', $eventCategories);
126
    }
127
128
    /***************************************************************************/
129
}
130