| Total Complexity | 47 |
| Total Lines | 402 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like ProductController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ProductController, and based on these observations, apply Extract Interface, too.
| 1 | <?php namespace App\Http\Controllers\Backend; |
||
| 26 | class ProductController extends Controller |
||
| 27 | { |
||
| 28 | public function index(Request $request) |
||
| 29 | { |
||
| 30 | if ($request->wantsJson()) { |
||
| 31 | |||
| 32 | $product = ProductService::getModel()->select( |
||
| 33 | ['product.*', |
||
| 34 | 'brand.title as brandtitle', |
||
| 35 | 'product_category.title as categorytitle'] |
||
| 36 | )->with(array('productCategory', 'brand', 'subcategories', 'attributes', 'productImages','taxRate')) |
||
| 37 | |||
| 38 | ->leftJoin('product_category as product_category', 'product_category.id', '=', 'product.product_category_id') |
||
| 39 | |||
| 40 | ->leftJoin('brand as brand', 'brand.id', '=', 'product.brand_id') |
||
| 41 | |||
| 42 | ->where('product.shop_id', '=', auth('hideyobackend')->user()->selected_shop_id); |
||
| 43 | |||
| 44 | $datatables = \Datatables::of($product) |
||
| 45 | ->filterColumn('reference_code', function ($query, $keyword) { |
||
| 46 | $query->whereRaw("product.reference_code like ?", ["%{$keyword}%"]); |
||
| 47 | }) |
||
| 48 | ->filterColumn('active', function ($query, $keyword) { |
||
| 49 | $query->whereRaw("product.active like ?", ["%{$keyword}%"]); |
||
| 50 | ; |
||
| 51 | }) |
||
| 52 | |||
| 53 | ->addColumn('rank', function ($product) { |
||
| 54 | return '<input type="text" class="change-rank" value="'.$product->rank.'" style="width:50px;" data-url="/admin/product/change-rank/'.$product->id.'">'; |
||
| 55 | |||
| 56 | }) |
||
| 57 | |||
| 58 | ->filterColumn('title', function ($query, $keyword) { |
||
| 59 | |||
| 60 | $query->where( |
||
| 61 | function ($query) use ($keyword) { |
||
| 62 | $query->whereRaw("product.title like ?", ["%{$keyword}%"]); |
||
| 63 | $query->orWhereRaw("product.reference_code like ?", ["%{$keyword}%"]); |
||
| 64 | $query->orWhereRaw("brand.title like ?", ["%{$keyword}%"]); |
||
| 65 | ; |
||
| 66 | } |
||
| 67 | ); |
||
| 68 | }) |
||
| 69 | |||
| 70 | ->filterColumn('categorytitle', function ($query, $keyword) { |
||
| 71 | $query->whereRaw("product_category.title like ?", ["%{$keyword}%"]); |
||
| 72 | }) |
||
| 73 | |||
| 74 | ->addColumn('active', function ($product) { |
||
| 75 | if ($product->active) { |
||
| 76 | return '<a href="#" class="change-active" data-url="'.url()->route('product.change-active', array('productId' => $product->id)).'"><span class="glyphicon glyphicon-ok icon-green"></span></a>'; |
||
| 77 | } |
||
| 78 | |||
| 79 | return '<a href="#" class="change-active" data-url="'.url()->route('product.change-active', array('productId' => $product->id)).'"><span class="glyphicon glyphicon-remove icon-red"></span></a>'; |
||
| 80 | }) |
||
| 81 | |||
| 82 | ->addColumn('title', function ($product) { |
||
| 83 | if ($product->brand) { |
||
| 84 | return $product->brand->title.' | '.$product->title; |
||
| 85 | } |
||
| 86 | |||
| 87 | return $product->title; |
||
| 88 | }) |
||
| 89 | |||
| 90 | |||
| 91 | ->addColumn('amount', function ($product) { |
||
| 92 | if ($product->attributes->count()) { |
||
| 93 | return '<a href="/admin/product/'.$product->id.'/product-combination">combinations</a>'; |
||
| 94 | } |
||
| 95 | |||
| 96 | return '<input type="text" class="change-amount" value="'.$product->amount.'" style="width:50px;" data-url="'.url()->route('product.change-amount', array('productId' => $product->id)).'">'; |
||
| 97 | }) |
||
| 98 | |||
| 99 | ->addColumn('image', function ($product) { |
||
| 100 | if ($product->productImages->count()) { |
||
| 101 | return '<img src="/files/product/100x100/'.$product->id.'/'.$product->productImages->first()->file.'" />'; |
||
| 102 | } |
||
| 103 | }) |
||
| 104 | ->addColumn('price', function ($product) { |
||
| 105 | |||
| 106 | $result = ""; |
||
| 107 | if ($product->price) { |
||
| 108 | |||
| 109 | $taxRate = 0; |
||
| 110 | $priceInc = 0; |
||
| 111 | $taxValue = 0; |
||
| 112 | |||
| 113 | if (isset($product->taxRate->rate)) { |
||
| 114 | $taxRate = $product->taxRate->rate; |
||
| 115 | $priceInc = (($product->taxRate->rate / 100) * $product->price) + $product->price; |
||
| 116 | $taxValue = $priceInc - $product->price; |
||
| 117 | } |
||
| 118 | |||
| 119 | $discountPriceInc = false; |
||
| 120 | $discountPriceEx = false; |
||
| 121 | $discountTaxRate = 0; |
||
| 122 | if ($product->discount_value) { |
||
| 123 | if ($product->discount_type == 'amount') { |
||
| 124 | $discountPriceInc = $priceInc - $product->discount_value; |
||
| 125 | $discountPriceEx = $discountPriceInc / 1.21; |
||
| 126 | } elseif ($product->discount_type == 'percent') { |
||
| 127 | $tax = ($product->discount_value / 100) * $priceInc; |
||
| 128 | $discountPriceInc = $priceInc - $tax; |
||
| 129 | $discountPriceEx = $discountPriceInc / 1.21; |
||
| 130 | } |
||
| 131 | $discountTaxRate = $discountPriceInc - $discountPriceEx; |
||
| 132 | $discountPriceInc = $discountPriceInc; |
||
| 133 | $discountPriceEx = $discountPriceEx; |
||
| 134 | } |
||
| 135 | |||
| 136 | |||
| 137 | $output = array( |
||
| 138 | 'orginal_price_ex_tax' => $product->price, |
||
| 139 | 'orginal_price_ex_tax_number_format' => number_format($product->price, 2, '.', ''), |
||
| 140 | 'orginal_price_inc_tax' => $priceInc, |
||
| 141 | 'orginal_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''), |
||
| 142 | 'tax_rate' => $taxRate, |
||
| 143 | 'tax_value' => $taxValue, |
||
| 144 | 'currency' => 'EU', |
||
| 145 | 'discount_price_inc' => $discountPriceInc, |
||
| 146 | 'discount_price_inc_number_format' => number_format($discountPriceInc, 2, '.', ''), |
||
| 147 | 'discount_price_ex' => $discountPriceEx, |
||
| 148 | 'discount_price_ex_number_format' => number_format($discountPriceEx, 2, '.', ''), |
||
| 149 | 'discount_tax_value' => $discountTaxRate, |
||
| 150 | 'discount_value' => $product->discount_value, |
||
| 151 | 'amount' => $product->amount |
||
| 152 | ); |
||
| 153 | |||
| 154 | $result = '€ '.$output['orginal_price_ex_tax_number_format'].' / € '.$output['orginal_price_inc_tax_number_format']; |
||
| 155 | |||
| 156 | |||
| 157 | if ($product->discount_value) { |
||
| 158 | $result .= '<br/> discount: yes'; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | return $result; |
||
| 163 | }) |
||
| 164 | |||
| 165 | |||
| 166 | ->addColumn('categorytitle', function ($product) { |
||
| 167 | if ($product->subcategories()->count()) { |
||
| 168 | $subcategories = $product->subcategories()->pluck('title')->toArray(); |
||
| 169 | return $product->categorytitle.', <small> '.implode(', ', $subcategories).'</small>'; |
||
| 170 | } |
||
| 171 | |||
| 172 | return $product->categorytitle; |
||
| 173 | }) |
||
| 174 | |||
| 175 | ->addColumn('action', function ($product) { |
||
| 176 | $deleteLink = \Form::deleteajax(url()->route('product.destroy', $product->id), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger'), $product->title); |
||
| 177 | $copy = '<a href="'.url()->route('product.copy', $product->id).'" class="btn btn-default btn-sm btn-info"><i class="entypo-pencil"></i>Copy</a>'; |
||
| 178 | |||
| 179 | $links = '<a href="'.url()->route('product.edit', $product->id).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Edit</a> '.$copy.' '.$deleteLink; |
||
| 180 | |||
| 181 | return $links; |
||
| 182 | }); |
||
| 183 | |||
| 184 | return $datatables->make(true); |
||
| 185 | } |
||
| 186 | |||
| 187 | return view('backend.product.index')->with('product', ProductService::selectAll()); |
||
| 188 | } |
||
| 189 | |||
| 190 | public function getRank(Request $request) |
||
| 191 | { |
||
| 192 | if ($request->wantsJson()) { |
||
| 193 | |||
| 194 | $product = ProductService::getModel()->select( |
||
| 195 | ['product.*', |
||
| 196 | 'brand.title as brandtitle', |
||
| 197 | 'product_category.title as categorytitle'] |
||
| 198 | )->with(array('productCategory', 'brand', 'subcategories', 'attributes', 'productImages','taxRate')) |
||
| 199 | ->leftJoin('product_category as product_category', 'product_category.id', '=', 'product.product_category_id') |
||
| 200 | ->leftJoin('brand as brand', 'brand.id', '=', 'product.brand_id') |
||
| 201 | ->where('product.shop_id', '=', auth('hideyobackend')->user()->selected_shop_id); |
||
| 202 | |||
| 203 | $datatables = \Datatables::of($product) |
||
| 204 | ->addColumn('rank', function ($product) { |
||
| 205 | return '<input type="text" class="change-rank" value="'.$product->rank.'" style="width:50px;" data-url="'.url()->route('product.change-rank', array('productId' => $product->id)).'">'; |
||
| 206 | }) |
||
| 207 | ->filterColumn('categorytitle', function ($query, $keyword) { |
||
| 208 | $query->whereRaw("product_category.title like ?", ["%{$keyword}%"]); |
||
| 209 | }) |
||
| 210 | ->addColumn('title', function ($product) { |
||
| 211 | if ($product->brand) { |
||
| 212 | return $product->brand->title.' | '.$product->title; |
||
| 213 | } |
||
| 214 | |||
| 215 | return $product->title; |
||
| 216 | }) |
||
| 217 | ->addColumn('categorytitle', function ($product) { |
||
| 218 | if ($product->subcategories()->count()) { |
||
| 219 | $subcategories = $product->subcategories()->pluck('title')->toArray(); |
||
| 220 | return $product->categorytitle.', <small> '.implode(', ', $subcategories).'</small>'; |
||
| 221 | } |
||
| 222 | |||
| 223 | return $product->categorytitle; |
||
| 224 | }); |
||
| 225 | |||
| 226 | return $datatables->make(true); |
||
| 227 | |||
| 228 | } |
||
| 229 | |||
| 230 | return view('backend.product.rank')->with('product', ProductService::selectAll()); |
||
| 231 | } |
||
| 232 | |||
| 233 | public function refactorAllImages() |
||
| 237 | } |
||
| 238 | |||
| 239 | public function create() |
||
| 242 | } |
||
| 243 | |||
| 244 | public function store(Request $request) |
||
| 245 | { |
||
| 246 | $result = ProductService::create($request->all()); |
||
| 247 | return ProductService::notificationRedirect('product.index', $result, 'The product was inserted.'); |
||
| 248 | } |
||
| 249 | |||
| 250 | public function changeActive($productId) |
||
| 251 | { |
||
| 252 | $result = ProductService::changeActive($productId); |
||
| 253 | return response()->json($result); |
||
| 254 | } |
||
| 255 | |||
| 256 | public function changeAmount($productId, $amount) |
||
| 257 | { |
||
| 258 | $result = ProductService::changeAmount($productId, $amount); |
||
| 259 | return response()->json($result); |
||
| 260 | } |
||
| 261 | |||
| 262 | |||
| 263 | public function changeRank($productId, $rank = 0) |
||
| 264 | { |
||
| 265 | $result = ProductService::changeRank($productId, $rank); |
||
| 266 | return response()->json($result); |
||
| 267 | } |
||
| 268 | |||
| 269 | public function edit($productId) |
||
| 270 | { |
||
| 271 | $product = ProductService::find($productId); |
||
| 272 | |||
| 273 | return view('backend.product.edit')->with( |
||
| 274 | array( |
||
| 275 | 'product' => $product, |
||
| 276 | 'brands' => BrandService::selectAll()->pluck('title', 'id')->toArray(), |
||
| 277 | 'productCategories' => ProductCategoryService::selectAllProductPullDown()->pluck('title', 'id'), |
||
| 278 | 'taxRates' => TaxRateService::selectAll()->pluck('title', 'id') |
||
| 279 | ) |
||
| 280 | ); |
||
| 281 | } |
||
| 282 | |||
| 283 | public function getExport() |
||
| 286 | } |
||
| 287 | |||
| 288 | public function postExport() |
||
| 289 | { |
||
| 290 | |||
| 291 | $result = ProductService::selectAllExport(); |
||
| 292 | Excel::create('export', function ($excel) use ($result) { |
||
| 293 | |||
| 294 | $excel->sheet('Products', function ($sheet) use ($result) { |
||
| 295 | $newArray = array(); |
||
| 296 | foreach ($result as $row) { |
||
| 297 | $category = ""; |
||
| 298 | if ($row->productCategory) { |
||
| 299 | $category = $row->productCategory->title; |
||
| 300 | } |
||
| 301 | |||
| 302 | $priceDetails = $row->getPriceDetails(); |
||
| 303 | |||
| 304 | |||
| 305 | $newArray[$row->id] = array( |
||
| 306 | 'title' => $row->title, |
||
| 307 | 'category' => $category, |
||
| 308 | 'amount' => $row->amount, |
||
| 309 | 'reference_code' => $row->reference_code, |
||
| 310 | 'orginal_price_ex_tax_number_format' => $priceDetails['orginal_price_ex_tax_number_format'], |
||
| 311 | 'orginal_price_inc_tax_number_format' => $priceDetails['orginal_price_inc_tax_number_format'], |
||
| 312 | 'tax_rate' => $priceDetails['tax_rate'], |
||
| 313 | 'currency' => $priceDetails['currency'] |
||
| 314 | |||
| 315 | ); |
||
| 316 | |||
| 317 | |||
| 318 | $images = array(); |
||
| 319 | if ($row->productImages->count()) { |
||
| 320 | $i = 0; |
||
| 321 | foreach ($row->productImages as $image) { |
||
| 322 | $i++; |
||
| 323 | $newArray[$row->id]['image_'.$i] = url('/').'/files/product/800x800/'.$row->id.'/'.$image->file; |
||
| 324 | } |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | $sheet->fromArray($newArray); |
||
| 329 | }); |
||
| 330 | })->download('xls'); |
||
| 331 | |||
| 332 | |||
| 333 | Notification::success('The product export is completed.'); |
||
| 334 | return redirect()->route('product.index'); |
||
| 335 | } |
||
| 336 | |||
| 337 | public function copy($productId) |
||
| 338 | { |
||
| 339 | $product = ProductService::find($productId); |
||
| 340 | |||
| 341 | return view('backend.product.copy')->with( |
||
| 342 | array( |
||
| 343 | 'brands' => BrandService::selectAll()->pluck('title', 'id')->toArray(), |
||
| 344 | 'product' => $product, |
||
| 345 | 'productCategories' => ProductCategoryService::selectAll()->pluck('title', 'id'), |
||
| 346 | 'taxRates' => TaxRateService::selectAll()->pluck('title', 'id') |
||
| 347 | ) |
||
| 348 | ); |
||
| 349 | } |
||
| 350 | |||
| 351 | public function storeCopy(Request $request, $productId) |
||
| 352 | { |
||
| 353 | $product = ProductService::find($productId); |
||
| 354 | $result = ProductService::createCopy($request->all(), $productId); |
||
| 355 | |||
| 356 | if (isset($result->id)) { |
||
| 357 | if ($product->attributes) { |
||
| 358 | foreach ($product->attributes as $attribute) { |
||
| 359 | $inputAttribute = $attribute->toArray(); |
||
| 360 | |||
| 361 | foreach ($attribute->combinations as $row2) { |
||
| 362 | $inputAttribute['selected_attribute_ids'][] = $row2->attribute->id; |
||
| 363 | } |
||
| 364 | |||
| 365 | $this->productCombination->create($inputAttribute, $result->id); |
||
| 366 | } |
||
| 367 | } |
||
| 368 | |||
| 369 | Notification::success('The product copy is inserted.'); |
||
| 370 | return redirect()->route('product.index'); |
||
| 371 | } |
||
| 372 | |||
| 373 | foreach ($result->errors()->all() as $error) { |
||
| 374 | Notification::error($error); |
||
| 375 | } |
||
| 376 | |||
| 377 | return redirect()->back()->withInput(); |
||
| 378 | } |
||
| 379 | |||
| 380 | public function editSeo($id) |
||
| 383 | } |
||
| 384 | |||
| 385 | public function editPrice($id) |
||
| 386 | { |
||
| 387 | return view('backend.product.edit_price')->with(array('product' => ProductService::find($id), 'taxRates' => TaxRateService::selectAll()->pluck('title', 'id'))); |
||
| 388 | } |
||
| 389 | |||
| 390 | public function update(Request $request, $productId) |
||
| 391 | { |
||
| 392 | $input = $request->all(); |
||
| 393 | $result = ProductService::updateById($input, $productId); |
||
| 394 | |||
| 395 | $redirect = redirect()->route('product.index'); |
||
| 396 | |||
| 397 | if (isset($result->id)) { |
||
| 398 | if ($request->get('seo')) { |
||
| 399 | Notification::success('Product seo was updated.'); |
||
| 400 | $redirect = redirect()->route('product.edit_seo', $productId); |
||
| 401 | } elseif ($request->get('price')) { |
||
| 402 | Notification::success('Product price was updated.'); |
||
| 403 | $redirect = redirect()->route('product.edit_price', $productId); |
||
| 404 | } elseif ($request->get('product-combination')) { |
||
| 405 | Notification::success('Product combination leading attribute group was updated.'); |
||
| 406 | $redirect = redirect()->route('product-combination.index', $productId); |
||
| 407 | } else { |
||
| 408 | Notification::success('Product was updated.'); |
||
| 409 | } |
||
| 410 | |||
| 411 | return $redirect; |
||
| 412 | } |
||
| 413 | |||
| 414 | foreach ($result->errors()->all() as $error) { |
||
| 415 | Notification::error($error); |
||
| 416 | } |
||
| 417 | |||
| 418 | return redirect()->back()->withInput()->withErrors($result->errors()->all()); |
||
| 419 | } |
||
| 420 | |||
| 421 | public function destroy($id) |
||
| 428 | } |
||
| 429 | } |
||
| 430 | } |
||
| 431 |
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:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths