Test Setup Failed
Push — master ( 460730...7fbd38 )
by Manu
02:52
created

InventoryController::doSearchingQuery()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 0
cts 13
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 1
crap 12
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Brand;
6
use App\Brand_Model;
7
use App\Inventory;
8
use App\Location;
9
use App\Material_Type;
10
use App\MoneySource;
11
use App\Provider;
12
use Illuminate\Http\Request;
13
use Illuminate\Support\Facades\DB;
14
use Response;
15
16
class InventoryController extends Controller
17
{
18
    /**
19
     * Create a new controller instance.
20
     *
21
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
22
     */
23
    public function __construct()
24
    {
25
        $this->middleware('auth');
26
    }
27
28
    /**
29
     * Display a listing of the resource.
30
     *
31
     * @return \Illuminate\Http\Response
32
     */
33
    public function index()
34
    {
35
        $inventories = DB::table('inventories')
36
        ->leftJoin('brand', 'inventories.brand_id', '=', 'brand.id')
37
        ->leftJoin('material_type', 'inventories.material_type_id', '=', 'material_type.id')
38
        ->leftJoin('brand_model', 'inventories.model_id', '=', 'brand_model.id')
39
        ->leftJoin('moneySource', 'inventories.moneySourceId', '=', 'moneySource.id')
40
        ->leftJoin('location', 'inventories.location_id', '=', 'location.id')
41
        ->leftJoin('provider', 'inventories.provider_id', '=', 'provider.id')
42
        ->select('inventories.*', 'material_type.name as material_type_name', 'material_type.id as material_type_id', 'brand.name as brand_name', 'brand.id as brand_id', 'brand_model.name as brand_model_name', 'brand_model.id as model_id', 'location.name as location_name', 'location.id as location_id', 'moneySource.name as moneySource_name', 'moneySource.id as moneySourceId',
43
        'provider.name as provider_name', 'provider.id as provider_id')
44
        ->paginate(5);
45
46
        return view('inventory/index', ['inventories' => $inventories]);
47
    }
48
49
    /**
50
     * Show the form for creating a new resource.
51
     *
52
     * @return \Illuminate\Http\Response
53
     */
54
    public function create()
55
    {
56
        $material_types = Material_Type::all();
57
        $brands = Brand::all();
58
        $brand_models = Brand_Model::all();
59
        $moneySources = MoneySource::all();
60
        $locations = Location::all();
61
        $providers = Provider::all();
62
63
        return view('inventory/create', ['material_types' => $material_types, 'brands' => $brands, 'brand_models' => $brand_models,
64
      'moneySources'                                      => $moneySources, 'locations' => $locations, 'providers' => $providers, ]);
65
    }
66
67
    /**
68
     * Store a newly created resource in storage.
69
     *
70
     * @param \Illuminate\Http\Request $request
71
     *
72
     * @return \Illuminate\Http\Response
73
     */
74
    public function store(Request $request)
75
    {
76
        $this->validateInput($request);
77
        // Upload image
78
        $path = $request->file('picture')->store('avatars');
79
        $keys = ['name', 'description', 'material_type_id', 'brand_id', 'model_id', 'location_id', 'quantity', 'price',
80
        'moneysourceId', 'provider_id', 'date_entrance', 'last_update', ];
81
        $input = $this->createQueryInput($keys, $request);
82
        $input['picture'] = $path;
83
84
        Inventory::create($input);
85
86
        return redirect()->intended('/inventory-mnt');
87
    }
88
89
    /**
90
     * Display the specified resource.
91
     *
92
     * @param int $id
93
     *
94
     * @return \Illuminate\Http\Response
95
     */
96
    public function show($id)
97
    {
98
        //
99
    }
100
101
    /**
102
     * Show the form for editing the specified resource.
103
     *
104
     * @param int $id
105
     *
106
     * @return \Illuminate\Http\Response
107
     */
108
    public function edit($id)
109
    {
110
        $inventory = Inventory::find($id);
111
        // Redirect to state list if updating state wasn't existed
112
        if ($inventory == null || count($inventory) == 0) {
113
            return redirect()->intended('/inventory-mnt');
114
        }
115
116
        $material_types = Material_type::all();
117
        $brands = Brand::all();
118
        $brand_models = Brand_model::all();
119
        $moneySources = MoneySource::all();
120
        $locations = Location::all();
121
        $providers = Provider::all();
122
123
        return view('inventory/edit', ['inventory' => $inventory, 'material_types' => $material_types, 'brands' => $brands,  'brand_models' => $brand_models, 'moneySources' => $moneySources,
124
        'locations'                                => $locations, 'providers' => $providers, ]);
125
    }
126
127
    /**
128
     * Update the specified resource in storage.
129
     *
130
     * @param \Illuminate\Http\Request $request
131
     * @param int                      $id
132
     *
133
     * @return \Illuminate\Http\Response
134
     */
135
    public function update(Request $request, $id)
136
    {
137
        $inventory = Inventory::findOrFail($id);
0 ignored issues
show
Unused Code introduced by
$inventory is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
138
        $this->validateInput($request);
139
        // Upload image
140
        $keys = ['name', 'description', 'material_type_id', 'brand_id', 'model_id', 'location_id', 'quantity',
141
        'price', 'moneysourceId', 'provider_id', 'date_entrance', 'last_update', ];
142
        $input = $this->createQueryInput($keys, $request);
143
        if ($request->file('picture')) {
144
            $path = $request->file('picture')->store('public');
145
            $input['picture'] = $path;
146
        }
147
        Inventory::where('id', $id)
148
            ->update($input);
149
150
        return redirect()->intended('/inventory-mnt');
151
    }
152
153
    /**
154
     * Remove the specified resource from storage.
155
     *
156
     * @param int $id
157
     *
158
     * @return \Illuminate\Http\Response
159
     */
160
    public function destroy($id)
161
    {
162
        Inventory::where('id', $id)->delete();
163
164
        return redirect()->intended('/inventory-mnt');
165
    }
166
167
    /**
168
     * Search state from database base on some specific constraints.
169
     *
170
     * @param \Illuminate\Http\Request $request
171
     *
172
     *  @return \Illuminate\Http\Response
173
     */
174
    public function search(Request $request)
175
    {
176
        $constraints = [
177
            'name' => $request['name'],
178
            ];
179
        $inventories = $this->doSearchingQuery($constraints);
180
181
        return view('inventory/index', ['inventories' => $inventories, 'searchingVals' => $constraints]);
182
    }
183
184
    private function doSearchingQuery($constraints)
185
    {
186
        $query = inventory::query();
187
        $fields = array_keys($constraints);
188
        $index = 0;
189
        foreach ($constraints as $constraint) {
190
            if ($constraint != null) {
191
                $query = $query->where($fields[$index], 'like', '%'.$constraint.'%');
192
            }
193
            $index++;
194
        }
195
196
        return $query->paginate(5);
197
    }
198
199
     /**
200
      * Load image resource.
201
      *
202
      * @param  string  $name
203
      *
204
      * @return \Illuminate\Http\Response
205
      */
206
     public function load($name)
207
     {
208
         $path = storage_path().'/app/public/'.$name;
209
         if (file_exists($path)) {
210
             return Response::download($path);
211
         }
212
     }
213
214
    private function validateInput($request)
215
    {
216
        $this->validate($request, [
217
            'name' => 'required|max:60',
218
        ]);
219
    }
220
221
    private function createQueryInput($keys, $request)
222
    {
223
        $queryInput = [];
224
        for ($i = 0; $i < count($keys); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
225
            $key = $keys[$i];
226
            $queryInput[$key] = $request[$key];
227
        }
228
229
        return $queryInput;
230
    }
231
}
232