Passed
Push — master ( 90b0c1...58a137 )
by Dan Michael O.
02:44
created

ItemsController::lost()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 17
rs 9.8666
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;
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 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;
0 ignored issues
show
introduced by
The function Stringy\create was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
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)
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...
36
            ->get();
37
38
        return response()->view('items.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

38
        return /** @scrutinizer ignore-call */ response()->view('items.index', [
Loading history...
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 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

65
        return /** @scrutinizer ignore-call */ response()->json($items);
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(
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('items.show', array(
Loading history...
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(
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

93
        return /** @scrutinizer ignore-call */ response()->view('items.edit', array(
Loading history...
94
            'item' => $item,
95
            'things' => Thing::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(), [
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...
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(),
0 ignored issues
show
Bug introduced by
The function create 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

123
                /** @scrutinizer ignore-call */ 
124
                s($item->thing->properties->get('name_definite.nob'))->lowerCaseFirst(),
Loading history...
124
                action('ItemsController@show', $item->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

124
                /** @scrutinizer ignore-call */ 
125
                action('ItemsController@show', $item->id),
Loading history...
125
                $item->barcode
126
            ), ['library' => \Auth::user()->name]);
127
128
            return redirect()->action('ThingsController@show', $item->thing->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

128
            return /** @scrutinizer ignore-call */ redirect()->action('ThingsController@show', $item->thing->id)
Loading history...
129
                ->with('status', 'Eksemplaret ' . $item->barcode . ' ble lagret!');
130
        } else {
131
            return redirect()->action('ItemsController@show', $item->id)
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)
144
    {
145
        if ($item->loans()->count()) {
146
            return redirect()->action('ItemsController@show', $item->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

146
            return /** @scrutinizer ignore-call */ redirect()->action('ItemsController@show', $item->id)
Loading history...
147
                ->with('error', 'Kan ikke slette utlånt eksemplar.');
148
        }
149
150
        return response()->view('items.delete', 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

150
        return /** @scrutinizer ignore-call */ response()->view('items.delete', array(
Loading history...
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 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

164
            return /** @scrutinizer ignore-call */ redirect()->action('ItemsController@show', $item->id)
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),
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

171
            /** @scrutinizer ignore-call */ 
172
            action('ItemsController@show', $item->id),
Loading history...
172
            $item->barcode
173
        ), ['library' => \Auth::user()->name]);
174
        $item->delete();
175
176
        return redirect()->action('ItemsController@show', $item->id)
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),
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

191
            /** @scrutinizer ignore-call */ 
192
            action('ItemsController@show', $item->id),
Loading history...
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 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

201
        return /** @scrutinizer ignore-call */ redirect()->action('ItemsController@show', $item->id)
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),
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

218
            /** @scrutinizer ignore-call */ 
219
            action('ItemsController@show', $item->id),
Loading history...
219
            $item->barcode
220
        ), ['library' => \Auth::user()->name]);
221
222
        return redirect()->action('ItemsController@show', $item->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

222
        return /** @scrutinizer ignore-call */ redirect()->action('ItemsController@show', $item->id)
Loading history...
223
            ->with('status', 'Eksemplaret ble gjenopprettet.');
224
    }
225
}
226