Passed
Push — master ( 1fd74f...a7048c )
by Dan Michael O.
03:15
created

UsersController::deleteForm()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\User;
6
use App\Alma\User as AlmaUser;
7
use Carbon\Carbon;
0 ignored issues
show
Bug introduced by
The type Carbon\Carbon 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\Http\Request;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Request 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 Illuminate\Http\Response;
0 ignored issues
show
Bug introduced by
The type Illuminate\Http\Response 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...
10
use LimitIterator;
11
use Scriptotek\Alma\Client as AlmaClient;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client 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
13
class UsersController extends Controller
14
{
15
16
    private $messages = [
17
        'barcode.regex' => 'Låne-ID er ikke på riktig format.',
18
        'university_id.regex' => 'Feide-ID er ikke på riktig format ([email protected]).',
19
        'barcode.unique' => 'Det finnes allerede en annen bruker med denne strekkoden.',
20
        'university_id.unique' => 'Det finnes allerede en annen bruker med denne Feide-IDen.',
21
        'lastname.required' => 'Etternavn må fylles inn.',
22
        'firstname.required' => 'Fornavn må fylles inn.',
23
        'email.required_without' => 'Enten e-post eller telefonnummer må fylles inn.',
24
        'lang.required' => 'Språk må fylles inn.'
25
    ];
26
27
    /**
28
     * Display a listing of the resource.
29
     *
30
     * @param  Request $request
31
     * @return Response
32
     */
33
    public function getIndex(Request $request)
34
    {
35
        $users = User::with('loans')
36
            ->where('lastname', '!=', '(anonymisert)')
37
            ->orderBy('lastname')
38
            ->get()
39
            ->map(function ($user) {
40
                return [
41
                    'id' => $user->id,
42
                    'primaryId' => $user->alma_primary_id,
43
                    'group' => $user->alma_user_group,
44
                    'name' => $user->lastname . ', ' . $user->firstname,
45
                    'barcode' => $user->barcode,
46
                    'in_alma' => $user->in_alma,
47
                    'created_at' => $user->created_at->toDateTimestring(),
48
                    'note' => $user->note,
49
                ];
50
            });
51
52
        return response()->view('users.index', [
0 ignored issues
show
Bug introduced by
The function response 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

52
        return /** @scrutinizer ignore-call */ response()->view('users.index', [
Loading history...
53
            'users' => $users,
54
        ]);
55
    }
56
57
    /**
58
     * Display a listing of the resource as json.
59
     *
60
     * @param  Request $request
61
     * @return Response
62
     */
63
    public function json(Request $request)
64
    {
65
        $users = [];
66
        foreach (User::get() as $user) {
67
            $users[] = [
68
                'id' => $user->id,
69
                'primaryId' => $user->alma_primary_id,
70
                'group' => $user->alma_user_group,
71
                'name' => $user->lastname . ', ' . $user->firstname,
72
                'barcode' => $user->barcode,
73
                'type' => 'local',
74
            ];
75
        }
76
77
        return response()->json($users);
0 ignored issues
show
Bug introduced by
The function response 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

77
        return /** @scrutinizer ignore-call */ response()->json($users);
Loading history...
78
    }
79
80
    /**
81
     * Display a listing of the resource.
82
     *
83
     * @param AlmaClient $alma
84
     * @param  Request $request
85
     * @return Response
86
     */
87
    public function searchAlma(AlmaClient $alma, Request $request)
88
    {
89
        if (is_null($alma->key)) {
90
            \Log::warning('Cannot search Alma users since no Alma API key is configured.');
91
            return response()->json([]);
0 ignored issues
show
Bug introduced by
The function response 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

91
            return /** @scrutinizer ignore-call */ response()->json([]);
Loading history...
92
        }
93
        $query = 'ALL~' . $request->input('query');
94
        $users = collect($alma->users->search($query, ['limit' => 5]))->map(function ($u) {
0 ignored issues
show
Bug introduced by
The function collect 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

94
        $users = /** @scrutinizer ignore-call */ collect($alma->users->search($query, ['limit' => 5]))->map(function ($u) {
Loading history...
95
            return new AlmaUser($u);
96
        });
97
98
        return response()->json($users);
99
    }
100
101
    /**
102
     * Display the specified resource.
103
     *
104
     * @param User $user
105
     * @return Response
106
     */
107
    public function getShow(User $user)
108
    {
109
        return response()->view('users.show', [
0 ignored issues
show
Bug introduced by
The function response 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

109
        return /** @scrutinizer ignore-call */ response()->view('users.show', [
Loading history...
110
            'user' => $user,
111
        ]);
112
    }
113
114
    /**
115
     * Display form for connecting local user to external user.
116
     *
117
     * @param User $user
118
     * @return Response
119
     */
120
    public function connectForm(User $user)
121
    {
122
        return response()->view('users.connect', [
0 ignored issues
show
Bug introduced by
The function response 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

122
        return /** @scrutinizer ignore-call */ response()->view('users.connect', [
Loading history...
123
            'user' => $user,
124
        ]);
125
    }
126
127
    /**
128
     * Connect local user to external user.
129
     *
130
     * @param User $user
131
     * @param Request $request
132
     * @param AlmaClient $alma
133
     * @return Response
134
     */
135
    public function connect(User $user, Request $request, AlmaClient $alma)
136
    {
137
        $barcode = $request->barcode;
138
        if (empty($barcode)) {
139
            return back()->with('error', 'Du må registrere låne-ID.');
0 ignored issues
show
Bug introduced by
The function back 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

139
            return /** @scrutinizer ignore-call */ back()->with('error', 'Du må registrere låne-ID.');
Loading history...
140
        }
141
        $almaUsers = collect($alma->users->search('identifiers~' . $barcode, ['limit' => 1]))->map(function ($u) {
0 ignored issues
show
Bug introduced by
The function collect 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

141
        $almaUsers = /** @scrutinizer ignore-call */ collect($alma->users->search('identifiers~' . $barcode, ['limit' => 1]))->map(function ($u) {
Loading history...
142
            return new AlmaUser($u);
143
        });
144
        if (!count($almaUsers)) {
145
            return back()->with('error', 'Fant ikke låne-ID-en ' . $user->barcode . ' i Alma 😭 ');
146
        }
147
148
        $almaUser = $almaUsers[0];
149
150
        $barcode = $almaUser->getBarcode();
151
        $other = User::where('barcode', '=', $barcode)->first();
152
        if (!is_null($other) && $other->id != $user->id) {
153
            return back()->with('error', 'Låne-ID-en er allerede koblet til en annen Bibrex-bruker ' .
154
                '(' . $other->name . '). Du kan slå dem sammen fra brukeroversikten.');
155
        }
156
157
        $user->mergeFromAlmaResponse($almaUser);
158
        $user->save();
159
160
        return redirect()->action('UsersController@getShow', $user->id)
0 ignored issues
show
Bug introduced by
The function redirect 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

160
        return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
161
            ->with('status', 'Bibrex-brukeren ble koblet med Alma-brukeren!');
162
    }
163
164
    /**
165
     * Import user data from Alma.
166
     *
167
     * @param  int  $id
168
     * @return Response
169
     */
170
    public function sync(AlmaClient $alma, User $user)
171
    {
172
        if (!$user->barcode) {
173
            return back()->with('error', 'Du må registrere låne-ID for brukeren før du kan importere.');
0 ignored issues
show
Bug introduced by
The function back 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

173
            return /** @scrutinizer ignore-call */ back()->with('error', 'Du må registrere låne-ID for brukeren før du kan importere.');
Loading history...
174
        }
175
176
        if (!$user->updateFromAlma($alma)) {
177
            $user->save();
178
179
            return back()->with('error', 'Fant ikke brukeren i Alma 😭');
180
        }
181
        $user->save();
182
183
        return back()->with('status', 'Brukeropplysninger ble oppdatert fra Alma.');
184
    }
185
186
    /**
187
     * Show the form for editing the specified resource.
188
     *
189
     * @param User $user
190
     * @param Request $request
191
     * @return Response
192
     */
193
    public function getEdit(User $user, Request $request)
194
    {
195
        if (!$user->id) {
196
            $user->barcode = $request->barcode;
0 ignored issues
show
Bug Best Practice introduced by
The property barcode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
197
            $user->university_id = $request->university_id;
0 ignored issues
show
Bug Best Practice introduced by
The property university_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
198
            $user->lastname = $request->lastname;
0 ignored issues
show
Bug Best Practice introduced by
The property lastname does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
199
            $user->firstname = $request->firstname;
0 ignored issues
show
Bug Best Practice introduced by
The property firstname does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
200
            $user->phone = $request->phone;
0 ignored issues
show
Bug Best Practice introduced by
The property phone does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
201
            $user->email = $request->email;
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
202
        }
203
204
        return response()->view('users.edit', array(
0 ignored issues
show
Bug introduced by
The function response 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

204
        return /** @scrutinizer ignore-call */ response()->view('users.edit', array(
Loading history...
205
            'user' => $user
206
        ));
207
    }
208
209
    /**
210
     * Update the specified resource in storage.
211
     *
212
     * @param User $user
213
     * @param  Request $request
214
     * @return Response
215
     */
216
    public function upsert(User $user, Request $request)
217
    {
218
        \Validator::make($request->input(), [
0 ignored issues
show
Bug introduced by
The type Validator 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...
219
            'barcode' => 'nullable|regex:/^[0-9a-zA-Z]{10}$/|unique:users,barcode' . ($user->id ? ',' . $user->id : ''),
220
            'university_id' => 'nullable|regex:/@/|unique:users,university_id' . ($user->id ? ',' . $user->id : ''),
221
            'lastname' => 'required',
222
            'firstname' => 'required',
223
            'email' => 'requiredWithout:phone',
224
            'lang' => 'required',
225
        ], $this->messages)->validate();
226
227
        $user->barcode = $request->input('barcode');
0 ignored issues
show
Bug Best Practice introduced by
The property barcode does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
228
        $user->university_id = $request->input('university_id');
0 ignored issues
show
Bug Best Practice introduced by
The property university_id does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
229
        $user->lastname = $request->input('lastname');
0 ignored issues
show
Bug Best Practice introduced by
The property lastname does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
230
        $user->firstname = $request->input('firstname');
0 ignored issues
show
Bug Best Practice introduced by
The property firstname does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
231
        $user->phone = $request->input('phone');
0 ignored issues
show
Bug Best Practice introduced by
The property phone does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
232
        $user->email = $request->input('email');
0 ignored issues
show
Bug Best Practice introduced by
The property email does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
233
        $user->note = $request->input('note');
234
        $user->lang = $request->input('lang');
0 ignored issues
show
Bug Best Practice introduced by
The property lang does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
235
        $user->last_loan_at = Carbon::now();
236
        $newUser = !$user->exists;
237
        if (!$user->save()) {
238
            dd('Oi');
0 ignored issues
show
Bug introduced by
The function dd 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

238
            /** @scrutinizer ignore-call */ 
239
            dd('Oi');
Loading history...
239
        }
240
241
        if ($newUser) {
242
            return redirect()->action('LoansController@getIndex')
0 ignored issues
show
Bug introduced by
The function redirect 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

242
            return /** @scrutinizer ignore-call */ redirect()->action('LoansController@getIndex')
Loading history...
243
                ->with('status', 'Brukeren ble opprettet.')
244
                ->with('user', [
245
                    'type' => 'local',
246
                    'id' => $user->id,
247
                    'name' => $user->lastname . ', ' . $user->firstname,
248
                ]);
249
        }
250
251
        return redirect()->action('UsersController@getShow', $user->id)
252
            ->with('status', 'Brukeren ble lagret.');
253
    }
254
255
    /**
256
     * Display form to merge two users.
257
     *
258
     * @param User $user1
259
     * @param User $user2
260
     * @return Response
261
     */
262
    public function getMerge(User $user1, User $user2)
263
    {
264
        $merged = $user1->getMergeData($user2);
265
266
        return response()->view('users.merge', array(
0 ignored issues
show
Bug introduced by
The function response 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

266
        return /** @scrutinizer ignore-call */ response()->view('users.merge', array(
Loading history...
267
            'user1' => $user1,
268
            'user2' => $user2,
269
            'merged' => $merged
270
        ));
271
    }
272
273
    /**
274
     * Merge $user2 into $user1
275
     *
276
     * @param Request $request
277
     * @param User $user1
278
     * @param User $user2
279
     * @return Response
280
     */
281
    public function postMerge(Request $request, User $user1, User $user2)
282
    {
283
        $mergedAttributes = array();
284
        foreach (User::$editableAttributes as $attr) {
285
            $mergedAttributes[$attr] = $request->input($attr);
286
        }
287
288
        $errors = $user1->merge($user2, $mergedAttributes);
289
290
        if (!is_null($errors)) {
291
            return redirect()->action('UsersController@getMerge', array($user1->id, $user2->id))
0 ignored issues
show
Bug introduced by
The function redirect 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

291
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getMerge', array($user1->id, $user2->id))
Loading history...
292
                ->withErrors($errors);
293
        }
294
295
        return redirect()->action('UsersController@getShow', $user1->id)
296
            ->with('status', 'Brukerne ble flettet.');
297
    }
298
299
    /**
300
     * Show the form for creating the specified resource.
301
     *
302
     * @param Request $request
303
     * @return Response
304
     */
305
    public function createForm(Request $request)
306
    {
307
        $user = User::make();
308
309
        return response()->view('users.create', [
0 ignored issues
show
Bug introduced by
The function response 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

309
        return /** @scrutinizer ignore-call */ response()->view('users.create', [
Loading history...
310
            'user' => $user,
311
        ]);
312
    }
313
314
    /**
315
     * Show the form for deleting the specified resource.
316
     *
317
     * @param User $user
318
     * @param Request $request
319
     * @return Response
320
     */
321
    public function deleteForm(User $user, Request $request)
322
    {
323
        if ($user->loans()->count()) {
324
            return redirect()->action('UsersController@getShow', $user->id)
0 ignored issues
show
Bug introduced by
The function redirect 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

324
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
325
                ->with('error', 'Kan ikke slette en bruker med aktive lån.');
326
        }
327
328
        return response()->view('users.delete', [
0 ignored issues
show
Bug introduced by
The function response 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

328
        return /** @scrutinizer ignore-call */ response()->view('users.delete', [
Loading history...
329
            'user' => $user,
330
        ]);
331
    }
332
333
    /**
334
     * Delte the specified resource from storage.
335
     *
336
     * @param User $user
337
     * @param Request $request
338
     * @return Response
339
     */
340
    public function delete(User $user, Request $request)
341
    {
342
        if ($user->loans()->count()) {
343
            return redirect()->action('UsersController@getShow', $user->id)
0 ignored issues
show
Bug introduced by
The function redirect 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

343
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
344
                ->with('error', 'Kan ikke slette en bruker med aktive lån.');
345
        }
346
347
        $user_id = $user->id;
348
        $name = $user->name;
349
350
        $user->delete();
351
        \Log::info(sprintf('Slettet brukeren med ID %d', $user_id));
352
353
        return redirect()->action('UsersController@getIndex')
354
            ->with('status', "Brukeren $name ble slettet (men slapp av, du har ikke drept noen).");
355
    }
356
}
357