| Total Complexity | 42 |
| Total Lines | 521 |
| 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 |
||
| 26 | class ProductController extends BaseProductController |
||
| 27 | { |
||
| 28 | public $product; |
||
| 29 | public $price; |
||
| 30 | public $type; |
||
| 31 | public $subscription; |
||
| 32 | public $currency; |
||
| 33 | public $group; |
||
| 34 | public $plan; |
||
| 35 | public $tax; |
||
| 36 | public $tax_relation; |
||
| 37 | public $tax_class; |
||
| 38 | public $product_upload; |
||
| 39 | |||
| 40 | public function __construct() |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Display a listing of the resource. |
||
| 84 | * |
||
| 85 | * @return \Response |
||
| 86 | */ |
||
| 87 | public function index() |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Display a listing of the resource. |
||
| 100 | * |
||
| 101 | * @return \Response |
||
| 102 | */ |
||
| 103 | public function getProducts() |
||
| 104 | { |
||
| 105 | try { |
||
| 106 | $new_product = Product::select('id', 'name', 'type', 'image','group','image')->get(); |
||
| 107 | |||
| 108 | return\ DataTables::of($new_product) |
||
| 109 | |||
| 110 | ->addColumn('checkbox', function ($model) { |
||
| 111 | return "<input type='checkbox' class='product_checkbox' |
||
| 112 | value=".$model->id.' name=select[] id=check>'; |
||
| 113 | }) |
||
| 114 | ->addColumn('name', function ($model) { |
||
| 115 | return ucfirst($model->name); |
||
| 116 | }) |
||
| 117 | ->addColumn('image', function ($model) { |
||
| 118 | // return $model->image; |
||
| 119 | return "<img src= '$model->image' + height=\"80\"/>"; |
||
| 120 | }) |
||
| 121 | ->addColumn('type', function ($model) { |
||
| 122 | if ($this->type->where('id', $model->type)->first()) { |
||
| 123 | return $this->type->where('id', $model->type)->first()->name; |
||
| 124 | } else { |
||
| 125 | return 'Not available'; |
||
| 126 | } |
||
| 127 | }) |
||
| 128 | |||
| 129 | ->addColumn('group', function ($model) { |
||
| 130 | if ($this->group->where('id', $model->group)->first()) { |
||
| 131 | return $this->group->where('id', $model->group)->first()->name; |
||
| 132 | } else { |
||
| 133 | return 'Not available'; |
||
| 134 | } |
||
| 135 | }) |
||
| 136 | |||
| 137 | ->addColumn('Action', function ($model) { |
||
| 138 | $url = ''; |
||
| 139 | if ($model->type == 2) { |
||
| 140 | $url = '<a href='.url('product/download/'.$model->id). |
||
| 141 | " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-download' |
||
| 142 | style='color:white;'> </i> Download</a>"; |
||
| 143 | } |
||
| 144 | |||
| 145 | return '<p><a href='.url('products/'.$model->id.'/edit'). |
||
| 146 | " class='btn btn-sm btn-primary btn-xs'><i class='fa fa-edit' |
||
| 147 | style='color:white;'> </i> Edit</a> $url</p>"; |
||
| 148 | }) |
||
| 149 | |||
| 150 | ->rawColumns(['checkbox', 'name', 'image','type', 'group','Action']) |
||
| 151 | ->make(true); |
||
| 152 | } catch (\Exception $e) { |
||
| 153 | Bugsnag::notifyException($e); |
||
| 154 | |||
| 155 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | // Save file Info in Modal popup |
||
| 160 | public function save(Request $request) |
||
| 161 | { |
||
| 162 | try { |
||
| 163 | $product_id = Product::where('name', '=', $request->input('product'))->select('id')->first(); |
||
| 164 | |||
| 165 | $this->product_upload->product_id = $product_id->id; |
||
| 166 | $this->product_upload->title = $request->input('title'); |
||
| 167 | $this->product_upload->description = $request->input('description'); |
||
| 168 | $this->product_upload->version = $request->input('version'); |
||
| 169 | |||
| 170 | if ($request->file) { |
||
| 171 | $file = $request->file('file')->getClientOriginalName(); |
||
| 172 | |||
| 173 | $destination = storage_path().'/products'; |
||
| 174 | $request->file('file')->move($destination, $file); |
||
| 175 | $this->product_upload->file = $file; |
||
| 176 | } |
||
| 177 | $this->product_upload->save(); |
||
| 178 | $this->product->where('id', $product_id->id)->update(['version'=>$request->input('version')]); |
||
| 179 | |||
| 180 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 181 | } catch (\Exception $e) { |
||
| 182 | Bugsnag::notifyException($e); |
||
| 183 | |||
| 184 | return redirect()->with('fails', $e->getMessage()); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Show the form for creating a new resource. |
||
| 190 | * |
||
| 191 | * @return \Response |
||
| 192 | */ |
||
| 193 | public function create() |
||
| 194 | { |
||
| 195 | try { |
||
| 196 | /* |
||
| 197 | * server url |
||
| 198 | */ |
||
| 199 | $url = $this->getMyUrl(); |
||
| 200 | $i = $this->product->orderBy('created_at', 'desc')->first()->id + 1; |
||
| 201 | $cartUrl = $url.'/pricing?id='.$i; |
||
| 202 | $type = $this->type->pluck('name', 'id')->toArray(); |
||
| 203 | $subscription = $this->plan->pluck('name', 'id')->toArray(); |
||
| 204 | $currency = $this->currency->where('status', 1)->pluck('name', 'code')->toArray(); |
||
| 205 | $group = $this->group->pluck('name', 'id')->toArray(); |
||
| 206 | $products = $this->product->pluck('name', 'id')->toArray(); |
||
| 207 | $periods = $this->period->pluck('name', 'days')->toArray(); |
||
| 208 | $taxes = $this->tax_class->pluck('name', 'id')->toArray(); |
||
| 209 | |||
| 210 | return view('themes.default1.product.product.create', |
||
| 211 | compact('subscription', 'type', 'periods', 'currency', |
||
| 212 | 'group', 'cartUrl', 'products', 'taxes')); |
||
| 213 | } catch (\Exception $e) { |
||
| 214 | Bugsnag::notifyException($e); |
||
| 215 | |||
| 216 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Store a newly created resource in storage. |
||
| 222 | * |
||
| 223 | * @return \Response |
||
| 224 | */ |
||
| 225 | public function store(Request $request) |
||
| 226 | { |
||
| 227 | $input = $request->all(); |
||
| 228 | $v = \Validator::make($input, [ |
||
| 229 | 'name' => 'required|unique:products,name', |
||
| 230 | 'type' => 'required', |
||
| 231 | 'group' => 'required', |
||
| 232 | 'description'=> 'required', |
||
| 233 | 'category' => 'required', |
||
| 234 | 'image' => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000', |
||
| 235 | // 'version' => 'required', |
||
| 236 | ]); |
||
| 237 | if ($v->fails()) { |
||
| 238 | // $currency = $input['currency']; |
||
| 239 | |||
| 240 | return redirect()->back() |
||
| 241 | ->withErrors($v) |
||
| 242 | ->withInput() |
||
| 243 | ->with('currency'); |
||
| 244 | } |
||
| 245 | |||
| 246 | try { |
||
| 247 | if ($request->hasFile('image')) { |
||
| 248 | $image = $request->file('image')->getClientOriginalName(); |
||
| 249 | $imagedestinationPath = 'dist/product/images'; |
||
| 250 | $request->file('image')->move($imagedestinationPath, $image); |
||
| 251 | $this->product->image = $image; |
||
| 252 | } |
||
| 253 | |||
| 254 | $product = $this->product; |
||
| 255 | $product->fill($request->except('image', 'file'))->save(); |
||
| 256 | // Product::where('id',$product->id)->update(['name'=>$request->names]); |
||
| 257 | |||
| 258 | $product_id = $product->id; |
||
| 259 | $subscription = $request->input('subscription'); |
||
| 260 | |||
| 261 | $price = $request->input('price'); |
||
| 262 | // $price= |
||
| 263 | |||
| 264 | $sales_price = $request->input('sales_price'); |
||
| 265 | $currencies = $request->input('currency'); |
||
| 266 | $taxes = $request->input('tax'); |
||
| 267 | if ($taxes) { |
||
| 268 | foreach ($taxes as $key=>$value) { |
||
| 269 | $newtax = new TaxProductRelation(); |
||
| 270 | $newtax->product_id = $product_id; |
||
| 271 | $newtax->tax_class_id = $value; |
||
| 272 | $newtax->save(); |
||
| 273 | } |
||
| 274 | } |
||
| 275 | |||
| 276 | return redirect()->back()->with('success', \Lang::get('message.saved-successfully')); |
||
| 277 | } catch (\Exception $e) { |
||
| 278 | Bugsnag::notifyException($e); |
||
| 279 | |||
| 280 | return redirect()->with('fails', $e->getMessage()); |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Show the form for editing the specified resource. |
||
| 286 | * |
||
| 287 | * @param int $id |
||
| 288 | * |
||
| 289 | * @return \Response |
||
| 290 | */ |
||
| 291 | public function edit($id) |
||
| 292 | { |
||
| 293 | try { |
||
| 294 | $type = $this->type->pluck('name', 'id')->toArray(); |
||
| 295 | |||
| 296 | $subscription = $this->plan->pluck('name', 'id')->toArray(); |
||
| 297 | $currency = $this->currency->pluck('name', 'code')->toArray(); |
||
| 298 | $group = $this->group->pluck('name', 'id')->toArray(); |
||
| 299 | $products = $this->product->pluck('name', 'id')->toArray(); |
||
| 300 | $periods = $this->period->pluck('name', 'days')->toArray(); |
||
| 301 | $url = $this->GetMyUrl(); |
||
| 302 | $cartUrl = $url.'/cart?id='.$id; |
||
| 303 | $product = $this->product->where('id', $id)->first(); |
||
| 304 | $selectedCategory = \App\Model\Product\ProductCategory:: |
||
|
|
|||
| 305 | where('category_name', $product->category)->pluck('category_name')->toArray(); |
||
| 306 | $taxes = $this->tax_class->pluck('name', 'id')->toArray(); |
||
| 307 | // dd($taxes); |
||
| 308 | $saved_taxes = $this->tax_relation->where('product_id', $id)->get(); |
||
| 309 | $savedTaxes = $this->tax_relation->where('product_id', $id)->pluck('tax_class_id')->toArray(); |
||
| 310 | |||
| 311 | return view('themes.default1.product.product.edit', |
||
| 312 | compact('product', 'periods', 'type', 'subscription', |
||
| 313 | 'currency', 'group', 'price', 'cartUrl', 'products', |
||
| 314 | 'regular', 'sales', 'taxes', 'saved_taxes', 'savedTaxes', 'selectedCategory')); |
||
| 315 | } catch (\Exception $e) { |
||
| 316 | Bugsnag::notifyException($e); |
||
| 317 | |||
| 318 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 319 | } |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Update the specified resource in storage. |
||
| 324 | * |
||
| 325 | * @param int $id |
||
| 326 | * |
||
| 327 | * @return \Response |
||
| 328 | */ |
||
| 329 | public function update($id, Request $request) |
||
| 330 | { |
||
| 331 | $input = $request->all(); |
||
| 332 | $v = \Validator::make($input, [ |
||
| 333 | 'name' => 'required', |
||
| 334 | 'type' => 'required', |
||
| 335 | 'group' => 'required', |
||
| 336 | 'description'=> 'required', |
||
| 337 | 'image' => 'sometimes | mimes:jpeg,jpg,png,gif | max:1000', |
||
| 338 | ]); |
||
| 339 | |||
| 340 | if ($v->fails()) { |
||
| 341 | return redirect()->back()->with('errors', $v->errors()); |
||
| 342 | } |
||
| 343 | |||
| 344 | try { |
||
| 345 | $product = $this->product->where('id', $id)->first(); |
||
| 346 | if ($request->hasFile('image')) { |
||
| 347 | $image = $request->file('image')->getClientOriginalName(); |
||
| 348 | $imagedestinationPath = 'dist/product/images'; |
||
| 349 | $request->file('image')->move($imagedestinationPath, $image); |
||
| 350 | $product->image = $image; |
||
| 351 | } |
||
| 352 | if ($request->hasFile('file')) { |
||
| 353 | $file = $request->file('file')->getClientOriginalName(); |
||
| 354 | $filedestinationPath = storage_path().'/products'; |
||
| 355 | $request->file('file')->move($filedestinationPath, $file); |
||
| 356 | $product->file = $file; |
||
| 357 | } |
||
| 358 | $product->fill($request->except('image', 'file'))->save(); |
||
| 359 | $this->updateVersionFromGithub($product->id); |
||
| 360 | |||
| 361 | $product_id = $product->id; |
||
| 362 | $subscription = $request->input('subscription'); |
||
| 363 | $cost = $request->input('price'); |
||
| 364 | $sales_price = $request->input('sales_price'); |
||
| 365 | $currencies = $request->input('currency'); |
||
| 366 | |||
| 367 | //add tax class to tax_product_relation table |
||
| 368 | $taxes = $request->input('tax'); |
||
| 369 | $newTax = $this->saveTax($taxes, $product_id); |
||
| 370 | |||
| 371 | return redirect()->back()->with('success', \Lang::get('message.updated-successfully')); |
||
| 372 | } catch (\Exception $e) { |
||
| 373 | Bugsnag::notifyException($e); |
||
| 374 | |||
| 375 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 376 | } |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Remove the specified resource from storage. |
||
| 381 | * |
||
| 382 | * @param int $id |
||
| 383 | * |
||
| 384 | * @return \Response |
||
| 385 | */ |
||
| 386 | public function destroy(Request $request) |
||
| 387 | { |
||
| 388 | try { |
||
| 389 | $ids = $request->input('select'); |
||
| 390 | if (!empty($ids)) { |
||
| 391 | foreach ($ids as $id) { |
||
| 392 | if ($id != 1) { |
||
| 393 | $product = $this->product->where('id', $id)->first(); |
||
| 394 | if ($product) { |
||
| 395 | $product->delete(); |
||
| 396 | } else { |
||
| 397 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 398 | <i class='fa fa-ban'></i> |
||
| 399 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 400 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 401 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 402 | './* @scrutinizer ignore-type */\Lang::get('message.no-record').' |
||
| 403 | </div>'; |
||
| 404 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 405 | } |
||
| 406 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 407 | <i class='fa fa-ban'></i> |
||
| 408 | <b>"./* @scrutinizer ignore-type */ |
||
| 409 | \Lang::get('message.alert').'!</b> './* @scrutinizer ignore-type */ \Lang::get('message.success').' |
||
| 410 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 411 | './* @scrutinizer ignore-type */\Lang::get('message.deleted-successfully').' |
||
| 412 | </div>'; |
||
| 413 | } else { |
||
| 414 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 415 | <i class='fa fa-ban'></i> |
||
| 416 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 417 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 418 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 419 | './* @scrutinizer ignore-type */ \Lang::get('message.can-not-delete-default').' |
||
| 420 | </div>'; |
||
| 421 | } |
||
| 422 | } |
||
| 423 | } else { |
||
| 424 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 425 | <i class='fa fa-ban'></i> |
||
| 426 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 427 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 428 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 429 | './* @scrutinizer ignore-type */\Lang::get('message.select-a-row').' |
||
| 430 | </div>'; |
||
| 431 | //echo \Lang::get('message.select-a-row'); |
||
| 432 | } |
||
| 433 | $lastActivity = Activity::all()->last(); |
||
| 434 | } catch (\Exception $e) { |
||
| 435 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 436 | <i class='fa fa-ban'></i> |
||
| 437 | <b>"./* @scrutinizer ignore-type */\Lang::get('message.alert').'!</b> '. |
||
| 438 | /* @scrutinizer ignore-type */\Lang::get('message.failed').' |
||
| 439 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 440 | '.$e->getMessage().' |
||
| 441 | </div>'; |
||
| 442 | } |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Remove the specified resource from storage. |
||
| 447 | * |
||
| 448 | * @param int $id |
||
| 449 | * |
||
| 450 | * @return \Response |
||
| 451 | */ |
||
| 452 | public function fileDestroy(Request $request) |
||
| 453 | { |
||
| 454 | try { |
||
| 455 | $ids = $request->input('select'); |
||
| 456 | if (!empty($ids)) { |
||
| 457 | foreach ($ids as $id) { |
||
| 458 | if ($id != 1) { |
||
| 459 | $product = $this->product_upload->where('id', $id)->first(); |
||
| 460 | if ($product) { |
||
| 461 | $product->delete(); |
||
| 462 | } else { |
||
| 463 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 464 | <i class='fa fa-ban'></i> |
||
| 465 | <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
||
| 466 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 467 | '.\Lang::get('message.no-record').' |
||
| 468 | </div>'; |
||
| 469 | //echo \Lang::get('message.no-record') . ' [id=>' . $id . ']'; |
||
| 470 | } |
||
| 471 | echo "<div class='alert alert-success alert-dismissable'> |
||
| 472 | <i class='fa fa-ban'></i> |
||
| 473 | <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.success').' |
||
| 474 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 475 | '.\Lang::get('message.deleted-successfully').' |
||
| 476 | </div>'; |
||
| 477 | } else { |
||
| 478 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 479 | <i class='fa fa-ban'></i> |
||
| 480 | <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
||
| 481 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 482 | '.\Lang::get('message.can-not-delete-default').' |
||
| 483 | </div>'; |
||
| 484 | } |
||
| 485 | } |
||
| 486 | } else { |
||
| 487 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 488 | <i class='fa fa-ban'></i> |
||
| 489 | <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
||
| 490 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 491 | '.\Lang::get('message.select-a-row').' |
||
| 492 | </div>'; |
||
| 493 | //echo \Lang::get('message.select-a-row'); |
||
| 494 | } |
||
| 495 | } catch (\Exception $e) { |
||
| 496 | echo "<div class='alert alert-danger alert-dismissable'> |
||
| 497 | <i class='fa fa-ban'></i> |
||
| 498 | <b>".\Lang::get('message.alert').'!</b> '.\Lang::get('message.failed').' |
||
| 499 | <button type=button class=close data-dismiss=alert aria-hidden=true>×</button> |
||
| 500 | '.$e->getMessage().' |
||
| 501 | </div>'; |
||
| 502 | } |
||
| 503 | } |
||
| 504 | |||
| 505 | /* |
||
| 506 | * Download Files from Filesystem/Github |
||
| 507 | */ |
||
| 508 | public function downloadProduct($uploadid, $id, $invoice_id, $version_id = '') |
||
| 509 | { |
||
| 510 | try { |
||
| 511 | $product = $this->product->findOrFail($uploadid); |
||
| 512 | $type = $product->type; |
||
| 513 | $owner = $product->github_owner; |
||
| 514 | $repository = $product->github_repository; |
||
| 515 | $file = $this->product_upload |
||
| 516 | ->where('product_id', '=', $uploadid) |
||
| 517 | ->where('id', $version_id)->select('file')->first(); |
||
| 518 | $order = Order::where('invoice_id', '=', $invoice_id)->first(); |
||
| 519 | $order_id = $order->id; |
||
| 520 | if ($type == 2) { |
||
| 521 | $relese = $this->getRelease($owner, $repository, $order_id, $file); |
||
| 522 | |||
| 523 | return $relese; |
||
| 524 | } |
||
| 525 | } catch (\Exception $e) { |
||
| 526 | Bugsnag::notifyException($e); |
||
| 527 | |||
| 528 | return redirect()->back()->with('fails', $e->getMessage()); |
||
| 529 | } |
||
| 530 | } |
||
| 531 | |||
| 532 | public function getSubscriptionCheckScript() |
||
| 547 | data: {'product': val, 'user': user,'plan':plan}, |
||
| 548 | //data: 'product=' + val+'user='+user, |
||
| 549 | success: function (data) { |
||
| 550 | var price = data['price']; |
||
| 551 | var field = data['field']; |
||
| 552 | $('#price').val(price); |
||
| 553 | $('#fields').append(field); |
||
| 554 | } |
||
| 555 | }); |
||
| 556 | } |
||
| 561 |
This check looks for references to static members where there are spaces between the name of the type and the actual member.
An example:
will actually work, but is not very readable.