Passed
Push — master ( ba0de4...64562a )
by Dan Michael O.
03:13
created

UsersController::getShow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
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
        $users = 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
        $users = /** @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($users)) {
145
            return back()->with('error', 'Fant ikke låne-ID-en ' . $user->barcode . ' i Alma 😭 ');
146
        }
147
148
        $barcode = $users[0]->getBarcode();
149
        $other = User::where('barcode', '=', $barcode)->first();
150
        if (!is_null($other) && $other->id != $user->id) {
151
            return back()->with('error', 'Låne-ID-en er allerede koblet til en annen Bibrex-bruker ' .
152
                '(' . $other->name . '). Du kan slå dem sammen fra brukeroversikten.');
153
        }
154
155
        $user->mergeFromAlmaResponse($users[0]);
156
        $user->save();
157
158
        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

158
        return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
159
            ->with('status', 'Bibrex-brukeren ble koblet med Alma-brukeren!');
160
    }
161
162
    /**
163
     * Find the first Alma user matching a query.
164
     */
165
    protected function findAlmaUser(AlmaClient $alma, array $queries)
166
    {
167
        foreach ($queries as $query) {
168
            foreach ($alma->users->search($query, ['limit' => 1]) as $user) {
169
                return new AlmaUser($user);
170
            }
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * Import user data from Alma.
178
     *
179
     * @param  int  $id
180
     * @return Response
181
     */
182
    public function sync(AlmaClient $alma, User $user)
183
    {
184
        if (!$user->barcode) {
185
            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

185
            return /** @scrutinizer ignore-call */ back()->with('error', 'Du må registrere låne-ID for brukeren før du kan importere.');
Loading history...
186
        }
187
188
        $queries = [
189
            'identifiers~' . $user->university_id,
190
            'identifiers~' . $user->barcode,
191
            'ALL~' . $user->university_id,
192
            'ALL~' . $user->barcode,
193
        ];
194
195
        $almaUser = $this->findAlmaUser($alma, $queries);
196
        if (is_null($almaUser)) {
197
            $user->in_alma = false;
0 ignored issues
show
Bug Best Practice introduced by
The property in_alma does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
198
            $user->save();
199
            return back()->with('error', 'Fant ikke brukeren i Alma 😭');
200
        }
201
202
        $user->mergeFromAlmaResponse($almaUser);
203
        $user->save();
204
205
        return back()->with('status', 'Brukeropplysninger ble oppdatert fra Alma.');
206
    }
207
208
    /**
209
     * Show the form for editing the specified resource.
210
     *
211
     * @param User $user
212
     * @param Request $request
213
     * @return Response
214
     */
215
    public function getEdit(User $user, Request $request)
216
    {
217
        if (!$user->id) {
218
            $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...
219
            $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...
220
            $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...
221
            $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...
222
            $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...
223
            $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...
224
        }
225
226
        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

226
        return /** @scrutinizer ignore-call */ response()->view('users.edit', array(
Loading history...
227
            'user' => $user
228
        ));
229
    }
230
231
    /**
232
     * Update the specified resource in storage.
233
     *
234
     * @param User $user
235
     * @param  Request $request
236
     * @return Response
237
     */
238
    public function upsert(User $user, Request $request)
239
    {
240
        \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...
241
            'barcode' => 'nullable|regex:/^[0-9a-zA-Z]{10}$/|unique:users,barcode' . ($user->id ? ',' . $user->id : ''),
242
            'university_id' => 'nullable|regex:/@/|unique:users,university_id' . ($user->id ? ',' . $user->id : ''),
243
            'lastname' => 'required',
244
            'firstname' => 'required',
245
            'email' => 'requiredWithout:phone',
246
            'lang' => 'required',
247
        ], $this->messages)->validate();
248
249
        $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...
250
        $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...
251
        $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...
252
        $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...
253
        $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...
254
        $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...
255
        $user->note = $request->input('note');
256
        $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...
257
        $user->last_loan_at = Carbon::now();
258
        $newUser = !$user->exists;
259
        if (!$user->save()) {
260
            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

260
            /** @scrutinizer ignore-call */ 
261
            dd('Oi');
Loading history...
261
        }
262
263
        if ($newUser) {
264
            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

264
            return /** @scrutinizer ignore-call */ redirect()->action('LoansController@getIndex')
Loading history...
265
                ->with('status', 'Brukeren ble opprettet.')
266
                ->with('user', [
267
                    'type' => 'local',
268
                    'id' => $user->id,
269
                    'name' => $user->lastname . ', ' . $user->firstname,
270
                ]);
271
        }
272
273
        return redirect()->action('UsersController@getShow', $user->id)
274
            ->with('status', 'Brukeren ble lagret.');
275
    }
276
277
    /**
278
     * Display form to merge two users.
279
     *
280
     * @param User $user1
281
     * @param User $user2
282
     * @return Response
283
     */
284
    public function getMerge(User $user1, User $user2)
285
    {
286
        $merged = $user1->getMergeData($user2);
287
288
        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

288
        return /** @scrutinizer ignore-call */ response()->view('users.merge', array(
Loading history...
289
            'user1' => $user1,
290
            'user2' => $user2,
291
            'merged' => $merged
292
        ));
293
    }
294
295
    /**
296
     * Merge $user2 into $user1
297
     *
298
     * @param Request $request
299
     * @param User $user1
300
     * @param User $user2
301
     * @return Response
302
     */
303
    public function postMerge(Request $request, User $user1, User $user2)
304
    {
305
        $mergedAttributes = array();
306
        foreach (User::$editableAttributes as $attr) {
307
            $mergedAttributes[$attr] = $request->input($attr);
308
        }
309
310
        $errors = $user1->merge($user2, $mergedAttributes);
311
312
        if (!is_null($errors)) {
313
            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

313
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getMerge', array($user1->id, $user2->id))
Loading history...
314
                ->withErrors($errors);
315
        }
316
317
        return redirect()->action('UsersController@getShow', $user1->id)
318
            ->with('status', 'Brukerne ble flettet.');
319
    }
320
321
    /**
322
     * Show the form for creating the specified resource.
323
     *
324
     * @param Request $request
325
     * @return Response
326
     */
327
    public function createForm(Request $request)
328
    {
329
        $user = User::make();
330
331
        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

331
        return /** @scrutinizer ignore-call */ response()->view('users.create', [
Loading history...
332
            'user' => $user,
333
        ]);
334
    }
335
336
    /**
337
     * Show the form for deleting the specified resource.
338
     *
339
     * @param User $user
340
     * @param Request $request
341
     * @return Response
342
     */
343
    public function deleteForm(User $user, Request $request)
344
    {
345
        if ($user->loans()->count()) {
346
            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

346
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
347
                ->with('error', 'Kan ikke slette en bruker med aktive lån.');
348
        }
349
350
        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

350
        return /** @scrutinizer ignore-call */ response()->view('users.delete', [
Loading history...
351
            'user' => $user,
352
        ]);
353
    }
354
355
    /**
356
     * Delte the specified resource from storage.
357
     *
358
     * @param User $user
359
     * @param Request $request
360
     * @return Response
361
     */
362
    public function delete(User $user, Request $request)
363
    {
364
        if ($user->loans()->count()) {
365
            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

365
            return /** @scrutinizer ignore-call */ redirect()->action('UsersController@getShow', $user->id)
Loading history...
366
                ->with('error', 'Kan ikke slette en bruker med aktive lån.');
367
        }
368
369
        $user_id = $user->id;
370
        $name = $user->name;
371
372
        $user->delete();
373
        \Log::info(sprintf('Slettet brukeren med ID %d', $user_id));
374
375
        return redirect()->action('UsersController@getIndex')
376
            ->with('status', "Brukeren $name ble slettet (men slapp av, du har ikke drept noen).");
377
    }
378
}
379