Passed
Push — master ( 81620f...38b95c )
by Davide
54:10 queued 27:13
created

OrganizerController::destroy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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->created_by = Auth::id();
186 3
        $organizer->created_by = $request->get('created_by');
187 3
        if (! $organizer->slug) {
188 2
            $organizer->slug = Str::slug($organizer->name, '-').'-'.rand(10000, 100000);
189
        }
190
191 3
        $organizer->save();
192
193 3
        return $organizer->id;
194
    }
195
196
    /***************************************************************************/
197
198
    /**
199
     * Open a modal in the event view when 'create new organizer' button is clicked.
200
     *
201
     * @return \Illuminate\Http\Response
202
     */
203 1
    public function modal()
204
    {
205 1
        $users = User::pluck('name', 'id');
206
207 1
        return view('laravel-events-calendar::organizers.modal')
208 1
                    ->with('users', $users);
209
    }
210
211
    /***************************************************************************/
212
213
    /**
214
     * Store a newly created organizer from the create event view modal in storage.
215
     *
216
     * @param  \Illuminate\Http\Request  $request
217
     * @return \Illuminate\Http\Response
218
     */
219 1
    public function storeFromModal(Request $request)
220
    {
221 1
        $organizer = new Organizer();
222
223 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...
224 1
        $organizer = Organizer::find($organizerId);
225
226 1
        return response()->json([
227 1
            'organizerId' => $organizerId,
228 1
            'organizerName' => $organizer->name,
229
        ]);
230
    }
231
232
    /***************************************************************************/
233
234
    /**
235
     * Return the organizer by SLUG. (eg. http://websitename.com/organizer/xxxxx).
236
     *
237
     * @param  string $slug
238
     * @return \Illuminate\Http\Response
239
     */
240
    public function organizerBySlug($slug)
241
    {
242
        $organizer = Organizer::
243
                where('slug', $slug)
244
                ->first();
245
246
        return $this->show($organizer);
247
    }
248
249
    /***************************************************************************/
250
251
    /**
252
     * Return the validator with all the defined constraint.
253
     *
254
     * @param  \Illuminate\Http\Request  $request
255
     * @return \Illuminate\Validation\Validator
256
     */
257 4
    public function organizersValidator($request)
258
    {
259
        $rules = [
260 4
            'name' => 'required',
261
            'email' => 'required|email',
262
            'website' => 'nullable|url',
263
        ];
264
        $messages = [
265 4
            'website.url' => 'The website link is invalid. It should start with https://',
266
        ];
267
268 4
        $validator = Validator::make($request->all(), $rules, $messages);
269
270 4
        return $validator;
271
    }
272
}
273