ItemsController::deleteForm()   A
last analyzed

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\Item;
6
use App\Support\DbHelper;
7
use App\Thing;
8
use Illuminate\Http\Request;
9
use Illuminate\Http\Response;
10
use Rule;
0 ignored issues
show
Bug introduced by
The type Rule 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...
11
use function Stringy\create as s;
12
13
class ItemsController extends Controller
14
{
15
16
    /**
17
     * Validation error messages.
18
     *
19
     * @static array
20
     */
21
    protected $messages = [
22
        'barcode.required' => 'Strekkode må legges inn.',
23
        'barcode.unique' => 'Strekkoden er allerede i bruk. Du må legge inn en unik strekkode.',
24
    ];
25
26
    /**
27
     * Display a listing of the resource.
28
     *
29
     * @return Response
30
     */
31
    public function index()
32
    {
33
        $items = Item::with('loans', 'thing')
34
            ->whereNotNull('barcode')
35
            ->where('library_id', '=', \Auth::user()->id)
36
            ->get();
37
38
        return response()->view('items.index', [
39
            'items' => $items,
40
        ]);
41
    }
42
43
    /**
44
     * Search for resources.
45
     *
46
     * @param  Request $request
47
     * @return Response
48
     */
49
    public function search(Request $request)
50
    {
51
        $op = DbHelper::isPostgres() ? 'ILIKE' : 'LIKE';
52
        $q = $request->input('query') . '%';
53
        $items = Item::where('barcode', $op, $q)
54
            ->where('library_id', '=', \Auth::user()->id)
55
            ->limit(10)
56
            ->get()->map(function ($item) {
57
                return [
58
                    'id' => $item->id,
59
                    'type' => 'item',
60
                    'name' => $item->barcode,
61
                    'group' => $item->thing->name(),
62
                ];
63
            });
64
65
        return response()->json($items);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json($items) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
66
    }
67
68
    /**
69
     * Display the specified resource.
70
     *
71
     * @param Item $item
72
     * @return Response
73
     */
74
    public function show(Item $item)
75
    {
76
        return response()->view('items.show', array(
77
            'item' => $item,
78
        ));
79
    }
80
81
    /**
82
     * Show the form for editing the specified resource.
83
     *
84
     * @param Item $item
85
     * @param Request $request
86
     * @return Response
87
     */
88
    public function editForm(Item $item, Request $request)
89
    {
90
        if ($request->input('thing')) {
91
            $item->thing = Thing::find($request->input('thing'));
92
        }
93
        return response()->view('items.edit', array(
94
            'item' => $item,
95
            'things' => Thing::orderBy('properties->name->nob')->get(),
96
        ));
97
    }
98
99
    /**
100
     * Update or create the specified resource in storage.
101
     *
102
     * @param Item $item
103
     * @param Request $request
104
     * @return Response
105
     */
106
    public function upsert(Item $item, Request $request)
107
    {
108
        \Validator::make($request->all(), [
109
            'barcode' => 'required|unique:items,barcode' . ($item->barcode ? ',' . $item->id : ''),
110
            'thing' => 'exists:things,id',
111
        ], $this->messages)->validate();
112
113
        $isNew = !$item->exists;
114
115
        $item->barcode = $request->input('barcode');
116
        $item->note = $request->input('note');
117
        $item->thing_id = intval($request->input('thing'));
118
        $item->save();
119
120
        if ($isNew) {
121
            \Log::info(sprintf(
122
                'Registrerte %s <a href="%s">%s</a>.',
123
                s($item->thing->properties->get('name_definite.nob'))->lowerCaseFirst(),
124
                action('ItemsController@show', $item->id),
125
                $item->barcode
126
            ), ['library' => \Auth::user()->name]);
127
128
            return redirect()->action('ThingsController@show', $item->thing->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...rcode . ' ble lagret!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
129
                ->with('status', 'Eksemplaret ' . $item->barcode . ' ble lagret!');
130
        } else {
131
            return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...rcode . ' ble lagret!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
132
                ->with('status', 'Eksemplaret ' . $item->barcode . ' ble lagret!');
133
        }
134
    }
135
136
    /**
137
     * Show the form for deleting the specified resource.
138
     *
139
     * @param Item $item
140
     * @param Request $request
141
     * @return Response
142
     */
143
    public function deleteForm(Item $item, Request $request)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

143
    public function deleteForm(Item $item, /** @scrutinizer ignore-unused */ Request $request)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
144
    {
145
        if ($item->loans()->count()) {
146
            return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...te utlånt eksemplar.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
147
                ->with('error', 'Kan ikke slette utlånt eksemplar.');
148
        }
149
150
        return response()->view('items.delete', array(
151
            'item' => $item,
152
        ));
153
    }
154
155
    /**
156
     * Delete the specified resource from storage.
157
     *
158
     * @param Item $item
159
     * @return Response
160
     */
161
    public function delete(Item $item)
162
    {
163
        if ($item->loans()->count()) {
164
            return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...te utlånt eksemplar.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
165
                ->with('error', 'Kan ikke slette utlånt eksemplar.');
166
        }
167
168
        \Log::info(sprintf(
169
            'Slettet %s <a href="%s">%s</a>.',
170
            $item->thing->properties->get('name_definite.nob'),
171
            action('ItemsController@show', $item->id),
172
            $item->barcode
173
        ), ['library' => \Auth::user()->name]);
174
        $item->delete();
175
176
        return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...emplaret ble slettet!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
177
            ->with('status', 'Eksemplaret ble slettet!');
178
    }
179
180
    /**
181
     * Mark specified resource as lost.
182
     *
183
     * @param Item $item
184
     * @return Response
185
     */
186
    public function lost(Item $item)
187
    {
188
        \Log::info(sprintf(
189
            'Markerte %s <a href="%s">%s</a> som tapt.',
190
            $item->thing->properties->get('name_definite.nob'),
191
            action('ItemsController@show', $item->id),
192
            $item->barcode
193
        ), ['library' => \Auth::user()->name]);
194
195
        if ($item->loans()->count()) {
196
            $item->loans->first()->lost();
197
        } else {
198
            $item->lost();
199
        }
200
201
        return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio... ble merket som tapt!') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
202
            ->with('status', 'Eksemplaret ble merket som tapt!');
203
    }
204
205
    /**
206
     * Restore the specified resource.
207
     *
208
     * @param Item $item
209
     * @return Response
210
     */
211
    public function restore(Item $item)
212
    {
213
        $item->found();
214
215
        \Log::info(sprintf(
216
            'Gjenopprettet %s <a href="%s">%s</a>.',
217
            $item->thing->properties->get('name_definite.nob'),
218
            action('ItemsController@show', $item->id),
219
            $item->barcode
220
        ), ['library' => \Auth::user()->name]);
221
222
        return redirect()->action('ItemsController@show', $item->id)
0 ignored issues
show
Bug Best Practice introduced by
The expression return redirect()->actio...et ble gjenopprettet.') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
223
            ->with('status', 'Eksemplaret ble gjenopprettet.');
224
    }
225
}
226