Passed
Push — master ( 3eebb8...9b97c2 )
by Davide
28:04
created

OrganizerController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace DavideCasiraghi\LaravelEventsCalendar\Http\Controllers;
4
5
use DavideCasiraghi\LaravelEventsCalendar\Models\Organizer;
6
use Illuminate\Foundation\Auth\User;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Str;
11
use Validator;
12
13
class OrganizerController extends Controller
14
{
15
    /* Restrict the access to this resource just to logged in users except show view */
16 12
    public function __construct()
17
    {
18 12
        $this->middleware('auth', ['except' => ['show', 'organizerBySlug']]);
19 12
    }
20
21
    /***************************************************************************/
22
23
    /**
24
     * Display a listing of the resource.
25
     *
26
     * @return \Illuminate\Http\Response
27
     */
28 2
    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 2
        $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 2
        $searchKeywords = $request->input('keywords');
34
35 2
        if ($searchKeywords) {
36 1
            $organizers = DB::table('organizers')
37
                ->when($authorUserId, function ($query, $authorUserId) {
38
                    return $query->where('created_by', $authorUserId);
39 1
                })
40
                ->when($searchKeywords, function ($query, $searchKeywords) {
41 1
                    return $query->where('name', $searchKeywords)->orWhere('name', 'like', '%'.$searchKeywords.'%');
42 1
                })
43 1
                ->orderBy('name')
44 1
                ->paginate(20);
45
        } else {
46 1
            $organizers = DB::table('organizers')
47
            ->when($authorUserId, function ($query, $authorUserId) {
48
                return $query->where('created_by', $authorUserId);
49 1
            })
50 1
            ->orderBy('name')
51 1
            ->paginate(20);
52
        }
53
54 2
        return view('laravel-events-calendar::organizers.index', compact('organizers'))
55 2
            ->with('i', (request()->input('page', 1) - 1) * 20)
56 2
            ->with('searchKeywords', $searchKeywords);
57
    }
58
59
    /***************************************************************************/
60
61
    /**
62
     * Show the form for creating a new resource.
63
     *
64
     * @return \Illuminate\Http\Response
65
     */
66 1
    public function create()
67
    {
68 1
        $users = User::pluck('name', 'id');
69 1
        $authorUserId = $this->getLoggedAuthorId();
70
71 1
        return view('laravel-events-calendar::organizers.create')
72 1
            ->with('users', $users)
73 1
            ->with('authorUserId', $authorUserId);
74
    }
75
76
    /***************************************************************************/
77
78
    /**
79
     * Store a newly created resource in storage.
80
     *
81
     * @param  \Illuminate\Http\Request  $request
82
     * @return \Illuminate\Http\Response
83
     */
84 2
    public function store(Request $request)
85
    {
86
        // Validate form datas
87 2
        $validator = $this->organizersValidator($request);
88 2
        if ($validator->fails()) {
89 1
            return back()->withErrors($validator)->withInput();
90
        }
91
92 1
        $organizer = new Organizer();
93 1
        $this->saveOnDb($request, $organizer);
94
95 1
        return redirect()->route('organizers.index')
96 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_added_successfully'));
97
    }
98
99
    /***************************************************************************/
100
101
    /**
102
     * Display the specified resource.
103
     *
104
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
105
     * @return \Illuminate\Http\Response
106
     */
107 1
    public function show(Organizer $organizer)
108
    {
109 1
        return view('laravel-events-calendar::organizers.show', compact('organizer'));
110
    }
111
112
    /***************************************************************************/
113
114
    /**
115
     * Show the form for editing the specified resource.
116
     *
117
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
118
     * @return \Illuminate\Http\Response
119
     */
120 1
    public function edit(Organizer $organizer)
121
    {
122 1
        $authorUserId = $this->getLoggedAuthorId();
123 1
        $users = User::pluck('name', 'id');
124
125 1
        return view('laravel-events-calendar::organizers.edit', compact('organizer'))
126 1
            ->with('users', $users)
127 1
            ->with('authorUserId', $authorUserId);
128
    }
129
130
    /***************************************************************************/
131
132
    /**
133
     * Update the specified resource in storage.
134
     *
135
     * @param  \Illuminate\Http\Request  $request
136
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
137
     * @return \Illuminate\Http\Response
138
     */
139 2
    public function update(Request $request, Organizer $organizer)
140
    {
141
        // Validate form datas
142 2
        $validator = $this->organizersValidator($request);
143 2
        if ($validator->fails()) {
144 1
            return back()->withErrors($validator)->withInput();
145
        }
146
147 1
        $this->saveOnDb($request, $organizer);
148
149 1
        return redirect()->route('organizers.index')
150 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_updated_successfully'));
151
    }
152
153
    /***************************************************************************/
154
155
    /**
156
     * Remove the specified resource from storage.
157
     *
158
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
159
     * @return \Illuminate\Http\Response
160
     */
161 1
    public function destroy(Organizer $organizer)
162
    {
163 1
        $organizer->delete();
164
165 1
        return redirect()->route('organizers.index')
166 1
                        ->with('success', __('laravel-events-calendar::messages.organizer_deleted_successfully'));
167
    }
168
169
    /***************************************************************************/
170
171
    /**
172
     * Save the record on DB.
173
     * @param  \Illuminate\Http\Request  $request
174
     * @param  \DavideCasiraghi\LaravelEventsCalendar\Models\Organizer  $organizer
175
     * @return void
176
     */
177 3
    public function saveOnDb($request, $organizer)
178
    {
179 3
        $organizer->name = $request->get('name');
180 3
        $organizer->description = clean($request->get('description'));
181 3
        $organizer->website = $request->get('website');
182 3
        $organizer->email = $request->get('email');
183 3
        $organizer->phone = $request->get('phone');
184
        
185
        // Organizer profile picture upload
186 3
        if ($request->file('profile_picture')) {
187
            $imageFile = $request->file('profile_picture');
188
            $imageName = $imageFile->hashName();
189
            $imageSubdir = 'organizers_profile';
190
            $imageWidth = '968';
191
            $thumbWidth = '300';
192
193
            $this->uploadImageOnServer($imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
0 ignored issues
show
Bug introduced by
It seems like $imageFile can also be of type Illuminate\Http\UploadedFile; however, parameter $imageFile of DavideCasiraghi\LaravelE...::uploadImageOnServer() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

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

193
            $this->uploadImageOnServer(/** @scrutinizer ignore-type */ $imageFile, $imageName, $imageSubdir, $imageWidth, $thumbWidth);
Loading history...
194
            $organizer->profile_picture = $imageName;
195
        } else {
196 3
            $organizer->profile_picture = $request->profile_picture;
197
        }
198
199
        //$organizer->created_by = Auth::id();
200 3
        $organizer->created_by = $request->get('created_by');
201 3
        if (! $organizer->slug) {
202 2
            $organizer->slug = Str::slug($organizer->name, '-').'-'.rand(10000, 100000);
203
        }
204
205 3
        $organizer->save();
206
207 3
        return $organizer->id;
208
    }
209
210
    /***************************************************************************/
211
212
    /**
213
     * Open a modal in the event view when 'create new organizer' button is clicked.
214
     *
215
     * @return \Illuminate\Http\Response
216
     */
217 1
    public function modal()
218
    {
219 1
        $users = User::pluck('name', 'id');
220
221 1
        return view('laravel-events-calendar::organizers.modal')
222 1
                    ->with('users', $users);
223
    }
224
225
    /***************************************************************************/
226
227
    /**
228
     * Store a newly created organizer from the create event view modal in storage.
229
     *
230
     * @param  \Illuminate\Http\Request  $request
231
     * @return \Illuminate\Http\Response
232
     */
233 1
    public function storeFromModal(Request $request)
234
    {
235 1
        $organizer = new Organizer();
236
237 1
        $organizerId = $this->saveOnDb($request, $organizer);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $organizerId is correct as $this->saveOnDb($request, $organizer) targeting DavideCasiraghi\LaravelE...rController::saveOnDb() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
238 1
        $organizer = Organizer::find($organizerId);
239
240 1
        return response()->json([
241 1
            'organizerId' => $organizerId,
242 1
            'organizerName' => $organizer->name,
243
        ]);
244
    }
245
246
    /***************************************************************************/
247
248
    /**
249
     * Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx).
250
     *
251
     * @param  string $slug
252
     * @return \Illuminate\Http\Response
253
     */
254
    public function organizerBySlug($slug)
255
    {
256
        $organizer = Organizer::
257
                where('slug', $slug)
258
                ->first();
259
260
        return $this->show($organizer);
261
    }
262
263
    /***************************************************************************/
264
265
    /**
266
     * Return the validator with all the defined constraint.
267
     *
268
     * @param  \Illuminate\Http\Request  $request
269
     * @return \Illuminate\Validation\Validator
270
     */
271 4
    public function organizersValidator($request)
272
    {
273
        $rules = [
274 4
            'name' => 'required',
275
            'email' => 'required|email',
276
            'website' => 'nullable|url',
277
        ];
278
        $messages = [
279 4
            'website.url' => 'The website link is invalid. It should start with https://',
280
        ];
281
282 4
        $validator = Validator::make($request->all(), $rules, $messages);
283
284 4
        return $validator;
285
    }
286
}
287