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

OrganizerController   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 255
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 21
eloc 76
dl 0
loc 255
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A show() 0 3 1
A update() 0 12 2
A index() 0 27 3
A saveOnDb() 0 14 2
A store() 0 13 2
A destroy() 0 6 1
A __construct() 0 3 1
A storeFromModal() 0 10 1
A modal() 0 3 1
A edit() 0 11 4
A organizersValidator() 0 14 1
A organizerBySlug() 0 7 1
A create() 0 8 1
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\Organizer;
0 ignored issues
show
Bug introduced by
The type App\Organizer 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 Illuminate\Support\Str;
9
use Illuminate\Http\Request;
10
use Illuminate\Support\Facades\DB;
11
use Illuminate\Support\Facades\Auth;
12
13
class OrganizerController extends Controller
14
{
15
    /* Restrict the access to this resource just to logged in users except show view */
16
    public function __construct()
17
    {
18
        $this->middleware('auth', ['except' => ['show', 'organizerBySlug']]);
19
    }
20
21
    /***************************************************************************/
22
23
    /**
24
     * Display a listing of the resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28
    public function index(Request $request)
29
    {
30
        // To show just the organizers created by the the user - If admin or super admin is set to null show all the organizers
31
        $authorUserId = ($this->getLoggedAuthorId()) ? $this->getLoggedAuthorId() : null; // if is 0 (super admin or admin) it's setted to null to avoid include it in the query
32
33
        $searchKeywords = $request->input('keywords');
34
35
        if ($searchKeywords) {
36
            $organizers = DB::table('organizers')
37
                ->when($authorUserId, function ($query, $authorUserId) {
38
                    return $query->where('created_by', $authorUserId);
39
                })
40
                ->when($searchKeywords, function ($query, $searchKeywords) {
41
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
42
                })
43
                ->paginate(20);
44
        } else {
45
            $organizers = DB::table('organizers')
46
            ->when($authorUserId, function ($query, $authorUserId) {
47
                return $query->where('created_by', $authorUserId);
48
            })
49
            ->paginate(20);
50
        }
51
52
        return view('organizers.index', compact('organizers'))
53
            ->with('i', (request()->input('page', 1) - 1) * 20)
54
            ->with('searchKeywords', $searchKeywords);
55
    }
56
57
    /***************************************************************************/
58
59
    /**
60
     * Show the form for creating a new resource.
61
     *
62
     * @return \Illuminate\Http\Response
63
     */
64
    public function create()
65
    {
66
        $users = User::pluck('name', 'id');
67
        $authorUserId = $this->getLoggedUser();
68
69
        return view('organizers.create')
70
            ->with('users', $users)
71
            ->with('authorUserId', $authorUserId);
72
    }
73
74
    /***************************************************************************/
75
76
    /**
77
     * Store a newly created resource in storage.
78
     *
79
     * @param  \Illuminate\Http\Request  $request
80
     * @return \Illuminate\Http\Response
81
     */
82
    public function store(Request $request)
83
    {
84
        // Validate form datas
85
        $validator = $this->organizersValidator($request);
86
        if ($validator->fails()) {
87
            return back()->withErrors($validator)->withInput();
88
        }
89
90
        $organizer = new Organizer();
91
        $this->saveOnDb($request, $organizer);
92
93
        return redirect()->route('organizers.index')
94
                        ->with('success', __('messages.organizer_added_successfully'));
95
    }
96
97
    /***************************************************************************/
98
99
    /**
100
     * Display the specified resource.
101
     *
102
     * @param  \App\Organizer  $organizer
103
     * @return \Illuminate\Http\Response
104
     */
105
    public function show(Organizer $organizer)
106
    {
107
        return view('organizers.show', compact('organizer'));
108
    }
109
110
    /***************************************************************************/
111
112
    /**
113
     * Show the form for editing the specified resource.
114
     *
115
     * @param  \App\Organizer  $organizer
116
     * @return \Illuminate\Http\Response
117
     */
118
    public function edit(Organizer $organizer)
119
    {
120
        if (Auth::user()->id == $organizer->created_by || Auth::user()->isSuperAdmin() || Auth::user()->isAdmin()) {
0 ignored issues
show
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

120
        if (Auth::user()->id == $organizer->created_by || Auth::user()->isSuperAdmin() || Auth::user()->/** @scrutinizer ignore-call */ isAdmin()) {
Loading history...
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 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

120
        if (Auth::user()->id == $organizer->created_by || Auth::user()->/** @scrutinizer ignore-call */ isSuperAdmin() || Auth::user()->isAdmin()) {
Loading history...
121
            $authorUserId = $this->getLoggedAuthorId();
122
            $users = User::pluck('name', 'id');
123
124
            return view('organizers.edit', compact('organizer'))
125
                ->with('users', $users)
126
                ->with('authorUserId', $authorUserId);
127
        } else {
128
            return redirect()->route('home')->with('message', __('auth.not_allowed_to_access'));
129
        }
130
    }
131
132
    /***************************************************************************/
133
134
    /**
135
     * Update the specified resource in storage.
136
     *
137
     * @param  \Illuminate\Http\Request  $request
138
     * @param  \App\Organizer  $organizer
139
     * @return \Illuminate\Http\Response
140
     */
141
    public function update(Request $request, Organizer $organizer)
142
    {
143
        // Validate form datas
144
        $validator = $this->organizersValidator($request);
145
        if ($validator->fails()) {
146
            return back()->withErrors($validator)->withInput();
147
        }
148
149
        $this->saveOnDb($request, $organizer);
150
151
        return redirect()->route('organizers.index')
152
                        ->with('success', __('messages.organizer_updated_successfully'));
153
    }
154
155
    /***************************************************************************/
156
157
    /**
158
     * Remove the specified resource from storage.
159
     *
160
     * @param  \App\Organizer  $organizer
161
     * @return \Illuminate\Http\Response
162
     */
163
    public function destroy(Organizer $organizer)
164
    {
165
        $organizer->delete();
166
167
        return redirect()->route('organizers.index')
168
                        ->with('success', __('messages.organizer_deleted_successfully'));
169
    }
170
171
    /***************************************************************************/
172
173
    /**
174
     * Save the record on DB.
175
     * @param  \Illuminate\Http\Request  $request
176
     * @param  \App\Organizer  $organizer
177
     * @return void
178
     */
179
    public function saveOnDb($request, $organizer)
180
    {
181
        $organizer->name = $request->get('name');
182
        $organizer->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

182
        $organizer->description = /** @scrutinizer ignore-call */ clean($request->get('description'));
Loading history...
183
        $organizer->website = $request->get('website');
184
        $organizer->email = $request->get('email');
185
        $organizer->phone = $request->get('phone');
186
187
        $organizer->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...
188
        if (! $organizer->slug) {
189
            $organizer->slug = Str::slug($organizer->name, '-').'-'.rand(10000, 100000);
190
        }
191
192
        $organizer->save();
193
    }
194
195
    /***************************************************************************/
196
197
    /**
198
     * Open a modal in the event view when create organizer is clicked.
199
     *
200
     * @return \Illuminate\Http\Response
201
     */
202
    public function modal()
203
    {
204
        return view('organizers.modal');
205
    }
206
207
    /***************************************************************************/
208
209
    /**
210
     * Store a newly created organizer from the create event view modal in storage.
211
     *
212
     * @param  \Illuminate\Http\Request  $request
213
     * @return \Illuminate\Http\Response
214
     */
215
    public function storeFromModal(Request $request)
216
    {
217
        $organizer = new Organizer();
218
        request()->validate([
219
            'name' => 'required',
220
        ]);
221
222
        $this->saveOnDb($request, $organizer);
223
224
        return redirect()->back()->with('message', 'Organizer created');
225
        //return redirect()->back()->with('message', __('auth.successfully_registered'));
226
        //return true;
227
    }
228
229
    /***************************************************************************/
230
231
    /**
232
     * Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx).
233
     *
234
     * @param  string $slug
235
     * @return \Illuminate\Http\Response
236
     */
237
    public function organizerBySlug($slug)
238
    {
239
        $organizer = Organizer::
240
                where('slug', $slug)
241
                ->first();
242
243
        return $this->show($organizer);
244
    }
245
246
    /***************************************************************************/
247
248
    /**
249
     * Return the validator with all the defined constraint.
250
     *
251
     * @param  \Illuminate\Http\Request  $request
252
     * @return \Illuminate\Validation\Validator
253
     */
254
    public function organizersValidator($request)
255
    {
256
        $rules = [
257
            'name' => 'required',
258
            'email' => 'required|email',
259
            'website' => 'nullable|url',
260
        ];
261
        $messages = [
262
            'website.url' => 'The website link is invalid. It should start with https://',
263
        ];
264
265
        $validator = Validator::make($request->all(), $rules, $messages);
266
267
        return $validator;
268
    }
269
}
270