Passed
Push — master ( 5961e5...28e6d5 )
by Dan Michael O.
02:43
created

ThingsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 354
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 186
dl 0
loc 354
rs 10
c 0
b 0
f 0
wmc 15

9 Methods

Rating   Name   Duplication   Size   Complexity  
A json() 0 29 3
A restore() 0 12 1
A miscErrorResponse() 0 5 1
A show() 0 35 1
A upsert() 0 45 3
A updateSettings() 0 21 1
A index() 0 35 1
A updateImage() 0 52 1
A delete() 0 20 3
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Item;
6
use App\Thing;
7
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...
8
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...
9
use Imagine\Imagick\Imagine;
0 ignored issues
show
Bug introduced by
The type Imagine\Imagick\Imagine 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
11
class ThingsController extends Controller
12
{
13
14
    protected $thing;
15
16
    /**
17
     * Validation error messages.
18
     *
19
     * @static array
20
     */
21
    protected $messages = [
22
        'name.required' => 'Internt navn må fylles ut.',
23
        'name.unique' => 'Typen finnes allerede.',
24
25
        'loan_time.required' => 'Lånetid må fylles ut.',
26
27
        'properties.name_indefinite.nob.required' => 'Ubestemt form på bokmål må fylles ut.',
28
        'properties.name_definite.nob.required' => 'Bestemt form på bokmål må fylles ut.',
29
30
        'properties.name_indefinite.nno.required' => 'Ubestemt form på nynorsk må fylles ut.',
31
        'properties.name_definite.nno.required' => 'Bestemt form på nynorsk må fylles ut.',
32
33
        'properties.name_indefinite.eng.required' => 'Ubestemt form på engelsk må fylles ut.',
34
        'properties.name_definite.eng.required' => 'Bestemt form på engelsk må fylles ut.',
35
    ];
36
37
    /**
38
     * Display a listing of the resource.
39
     *
40
     * @param Request $request
41
     * @return Response
42
     */
43
    public function index(Request $request)
44
    {
45
        $libraryId = \Auth::user()->id;
0 ignored issues
show
Bug introduced by
The type Auth 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...
46
47
        $things = Thing::with('items', 'settings', 'items.loans')
48
            ->orderBy('properties->name->nob')
49
            ->get();
50
51
        $things = $things->map(function ($thing) use ($libraryId) {
52
            $all = $thing->items->whereNotIn('barcode', [null]);
53
            $avail = $all->filter(function (Item $item) {
54
                return is_null($item->activeLoan);
55
            });
56
            $mine = $all->where('library_id', $libraryId);
57
            $avail_mine = $avail->where('library_id', $libraryId);
58
59
            return [
60
                'type' => 'thing',
61
                'id' => $thing->id,
62
                'name' => $thing->name(),
63
                'library_settings' => $thing->library_settings,
64
                'properties' => $thing->properties,
65
                'image' => $thing->image,
66
                'loan_time' => $thing->loan_time,
67
68
                'items_total' => $all->count(),
69
                'items_mine' => $mine->count(),
70
71
                'avail_total' => $avail->count(),
72
                'avail_mine' => $avail_mine->count(),
73
            ];
74
        });
75
76
        return response()->view('things.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

76
        return /** @scrutinizer ignore-call */ response()->view('things.index', [
Loading history...
77
            'things' => $things,
78
        ]);
79
    }
80
81
    /**
82
     * Display a listing of the resource.
83
     *
84
     * @param Request $request
85
     * @return Response
86
     */
87
    public function json(Request $request)
88
    {
89
        $libraryId = \Auth::user()->id;
0 ignored issues
show
Unused Code introduced by
The assignment to $libraryId is dead and can be removed.
Loading history...
90
91
        $things = Thing::with('settings');
92
93
        if ($request->input('withLoans')) {
94
            $things->with('items.loans');
95
        }
96
97
        $things = $things->orderBy('properties->name->nob')->get();
98
99
        if ($request->input('withoutBarcode')) {
100
            $things = $things->filter(function ($thing) {
101
                return $thing->library_settings->loans_without_barcode;
102
            })->values();
103
        }
104
105
        $things = $things->map(function ($thing) {
106
            return [
107
                'id' => $thing->id,
108
                'type' => 'thing',
109
                'name' => $thing->name(),
110
                'properties' => $thing->properties,
111
                'library_settings' => $thing->library_settings,
112
            ];
113
        });
114
115
        return response()->json($things);
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

115
        return /** @scrutinizer ignore-call */ response()->json($things);
Loading history...
116
    }
117
118
    /**
119
     * Display the specified resource.
120
     *
121
     * @param Thing $thing
122
     * @return Response
123
     */
124
    public function show(Thing $thing)
125
    {
126
        $items = Item::with('thing', 'library')
127
            ->whereNotNull('barcode')
128
            ->where('thing_id', '=', $thing->id)
129
            ->orderBy('library_id')
130
            ->orderBy('barcode')
131
            ->get();
132
133
        $stats = [
134
            'loans' => [
135
                'total' => \DB::select(
0 ignored issues
show
Bug introduced by
The type DB 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...
136
                    "select count(*) as val from loans
137
                      where loans.item_id IN (select id from items where thing_id=:thing_id)",
138
                    ['thing_id' => $thing->id]
139
                )[0]->val,
140
                'this_year' => \DB::select(
141
                    "select count(*) as val from loans
142
                      where loans.item_id in (select id from items where thing_id=:thing_id)
143
                      and date_part('year', loans.created_at) = :year",
144
                    ['thing_id' => $thing->id, 'year' => date('Y')]
145
                )[0]->val,
146
                'last_year' => \DB::select(
147
                    "select count(*) as val from loans
148
                      where loans.item_id in (select id from items where thing_id=:thing_id)
149
                      and date_part('year', loans.created_at) = :year",
150
                    ['thing_id' => $thing->id, 'year' => date('Y') - 1]
151
                )[0]->val,
152
            ]
153
        ];
154
155
        return response()->view('things.show', 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

155
        return /** @scrutinizer ignore-call */ response()->view('things.show', array(
Loading history...
156
            'thing' => $thing,
157
            'items' => $items,
158
            'stats' => $stats,
159
        ));
160
    }
161
162
    /**
163
     * Update or create the specified resource in storage.
164
     *
165
     * @param Thing $thing
166
     * @param Request $request
167
     * @return Response
168
     */
169
    public function upsert(Thing $thing, Request $request)
170
    {
171
        \Validator::make($request->all(), [
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...
172
            //'name' => 'required|unique:things,name' . ($thing->id ? ',' . $thing->id : ''),
173
174
            'properties.name.nob' => 'required',
175
            'properties.name.nno' => 'required',
176
            'properties.name.eng' => 'required',
177
178
            'properties.name_indefinite.nob' => 'required',
179
            'properties.name_indefinite.nno' => 'required',
180
            'properties.name_indefinite.eng' => 'required',
181
            'properties.name_definite.nob' => 'required',
182
            'properties.name_definite.nno' => 'required',
183
            'properties.name_definite.eng' => 'required',
184
            'properties.loan_time' => 'required|numeric|min:1',
185
        ], $this->messages)->validate();
186
187
        $isNew = !$thing->exists;
188
189
        if ($isNew) {
190
            // The frontend will redirect to update the url, so flash a status message to the new page.
191
            \Session::flash('status', 'Tingen ble lagret.');
192
        }
193
194
        $thing->properties = $request->input('properties');
195
        $thing->save();
196
197
        if ($isNew) {
198
            \Log::info(sprintf(
199
                'Opprettet tingen <a href="%s">%s</a>.',
200
                action('ThingsController@show', $thing->id),
0 ignored issues
show
Bug introduced by
The function action 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

200
                /** @scrutinizer ignore-call */ 
201
                action('ThingsController@show', $thing->id),
Loading history...
201
                $thing->name()
202
            ));
203
        } else {
204
            \Log::info(sprintf(
205
                'Oppdaterte tingen <a href="%s">%s</a>.',
206
                action('ThingsController@show', $thing->id),
207
                $thing->name()
208
            ));
209
        }
210
211
        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

211
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
212
            'status' => 'Tingen «' . $thing->name() . '» ble lagret.',
213
            'thing' => $thing,
214
        ]);
215
    }
216
217
    /**
218
     * Update thing settings for my library.
219
     *
220
     * @param Thing $thing
221
     * @return Response
222
     */
223
    public function updateSettings(Thing $thing, Request $request)
224
    {
225
        \Validator::make($request->all(), [
226
            'loans_without_barcode' => ['required', 'boolean'],
227
            'reminders' => ['required', 'boolean'],
228
        ])->validate();
229
230
        $settings = $thing->library_settings;
231
        $settings->loans_without_barcode = (boolean) $request->input('loans_without_barcode');
232
        $settings->reminders = (boolean) $request->input('reminders');
233
        $settings->save();
234
235
        \Log::info(sprintf(
236
            'Oppdaterte innstillinger for tingen <a href="%s">%s</a>.',
237
            action('ThingsController@show', $thing->id),
0 ignored issues
show
Bug introduced by
The function action 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

237
            /** @scrutinizer ignore-call */ 
238
            action('ThingsController@show', $thing->id),
Loading history...
238
            $thing->name()
239
        ));
240
241
        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

241
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
242
            'status' => 'ok',
243
            'library_settings' => $settings,
244
        ]);
245
    }
246
247
    /**
248
     * Update image
249
     *
250
     * @param Thing $thing
251
     * @param Request $request
252
     * @param Imagine $imagine
253
     * @return Response
254
     * @throws \Illuminate\Validation\ValidationException
255
     */
256
    public function updateImage(Thing $thing, Request $request, Imagine $imagine)
257
    {
258
        $this->validate($request, [
259
            'thingimage' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:10000',
260
        ]);
261
262
        $file = $request->file('thingimage');
263
        $filename = $file->storePublicly('public');
264
        $filename = substr($filename, strpos($filename, '/') + 1);
265
        $filename_t = $filename . '.thumb.jpg';
266
267
        $fullpath  = storage_path("app/public/$filename");
0 ignored issues
show
Bug introduced by
The function storage_path 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

267
        $fullpath  = /** @scrutinizer ignore-call */ storage_path("app/public/$filename");
Loading history...
268
        $fullpath_t = storage_path("app/public/$filename_t");
269
270
        $imagine->open($fullpath)
271
            ->thumbnail(
272
                new \Imagine\Image\Box(
0 ignored issues
show
Bug introduced by
The type Imagine\Image\Box 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...
273
                    config('bibrex.thumbnail_dimensions.width'),
0 ignored issues
show
Bug introduced by
The function config 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

273
                    /** @scrutinizer ignore-call */ 
274
                    config('bibrex.thumbnail_dimensions.width'),
Loading history...
274
                    config('bibrex.thumbnail_dimensions.height')
275
                ),
276
                \Imagine\Image\ImageInterface::THUMBNAIL_INSET
0 ignored issues
show
Bug introduced by
The type Imagine\Image\ImageInterface 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...
277
            )
278
            ->save($fullpath_t);
279
280
        list($width, $height) = getimagesize($fullpath);
281
        list($width_t, $height_t) = getimagesize($fullpath_t);
282
283
        $thing->image = [
284
            'full' => [
285
                'name' => $filename,
286
                'size' => filesize($fullpath),
287
                'width' => $width,
288
                'height' => $height,
289
            ],
290
            'thumb' => [
291
                'name' => $filename_t,
292
                'size' => filesize($fullpath_t),
293
                'width' => $width_t,
294
                'height' => $height_t,
295
            ],
296
        ];
297
        $thing->save();
298
299
        \Log::info(sprintf(
300
            'Oppdaterte bilde for tingen <a href="%s">%s</a>.',
301
            action('ThingsController@show', $thing->id),
0 ignored issues
show
Bug introduced by
The function action 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

301
            /** @scrutinizer ignore-call */ 
302
            action('ThingsController@show', $thing->id),
Loading history...
302
            $thing->name()
303
        ));
304
305
        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

305
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
306
            'status' => 'Bildet ble lagret.',
307
            'thing' => $thing,
308
        ]);
309
    }
310
311
    public function miscErrorResponse($msg)
312
    {
313
        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

313
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
314
            'errors' => ['misc' => [$msg]],
315
        ], 422);
316
    }
317
318
    /**
319
     * Remove the specified resource from storage.
320
     *
321
     * @param Thing $thing
322
     * @return Response
323
     */
324
    public function delete(Thing $thing)
325
    {
326
        if ($thing->items()->whereNotNull('barcode')->count() != 0) {
327
            return $this->miscErrorResponse('Kan ikke slette ting med eksemplarer.');
328
        }
329
330
        if (count($thing->activeLoans()) != 0) {
331
            return $this->miscErrorResponse('Kan ikke slette ting med aktive lån.');
332
        }
333
334
        \Log::info(sprintf(
335
            'Slettet tingen <a href="%s">%s</a>.',
336
            action('ThingsController@show', $thing->id),
0 ignored issues
show
Bug introduced by
The function action 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

336
            /** @scrutinizer ignore-call */ 
337
            action('ThingsController@show', $thing->id),
Loading history...
337
            $thing->name()
338
        ));
339
        $thing->delete();
340
341
        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

341
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
342
            'status' => 'Tingen «' . $thing->name() . '» ble sletta.',
343
            'thing' => $thing,
344
        ]);
345
    }
346
347
    /**
348
     * Restore the specified resource.
349
     *
350
     * @param Thing $thing
351
     * @return Response
352
     */
353
    public function restore(Thing $thing)
354
    {
355
        $thing->restore();
356
        \Log::info(sprintf(
357
            'Gjenopprettet tingen <a href="%s">%s</a>.',
358
            action('ThingsController@show', $thing->id),
0 ignored issues
show
Bug introduced by
The function action 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

358
            /** @scrutinizer ignore-call */ 
359
            action('ThingsController@show', $thing->id),
Loading history...
359
            $thing->name()
360
        ));
361
362
        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

362
        return /** @scrutinizer ignore-call */ response()->json([
Loading history...
363
            'status' => 'Tingen «' . $thing->name() . '» ble gjenopprettet.',
364
            'thing' => $thing,
365
        ]);
366
    }
367
}
368