| Conditions | 10 |
| Paths | 3 |
| Total Lines | 97 |
| Code Lines | 61 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php namespace App\Http\Controllers\Backend; |
||
| 23 | public function index(Request $request, $productId) |
||
| 24 | { |
||
| 25 | $product = ProductService::find($productId); |
||
| 26 | |||
| 27 | if($product) { |
||
| 28 | if ($request->wantsJson()) { |
||
| 29 | |||
| 30 | $query = ProductCombinationService::getModel()->where('product_id', '=', $productId); |
||
| 31 | |||
| 32 | $datatables = \DataTables::of($query)->addColumn('action', function ($query) use ($productId) { |
||
| 33 | $deleteLink = \Form::deleteajax(url()->route('product-combination.destroy', array('productId' => $productId, 'id' => $query->id)), 'Delete', '', array('class'=>'btn btn-default btn-sm btn-danger')); |
||
| 34 | $links = '<a href="'.url()->route('product-combination.edit', array('productId' => $productId, 'id' => $query->id)).'" class="btn btn-default btn-sm btn-success"><i class="entypo-pencil"></i>Edit</a> '.$deleteLink; |
||
| 35 | |||
| 36 | return $links; |
||
| 37 | }) |
||
| 38 | |||
| 39 | ->addColumn('amount', function ($query) { |
||
| 40 | return '<input type="text" class="change-amount-product-attribute" value="'.$query->amount.'" data-url="/admin/product/'.$query->product_id.'/product-combination/change-amount-attribute/'.$query->id.'">'; |
||
| 41 | }) |
||
| 42 | |||
| 43 | ->addColumn('price', function ($query) { |
||
| 44 | $result = 0; |
||
| 45 | if ($query->price) { |
||
| 46 | |||
| 47 | $taxRate = 0; |
||
| 48 | $priceInc = 0; |
||
| 49 | $taxValue = 0; |
||
| 50 | |||
| 51 | if (isset($query->taxRate->rate)) { |
||
| 52 | $taxRate = $query->taxRate->rate; |
||
| 53 | $priceInc = (($query->taxRate->rate / 100) * $query->price) + $query->price; |
||
| 54 | $taxValue = $priceInc - $query->price; |
||
| 55 | } |
||
| 56 | |||
| 57 | $discountPriceInc = false; |
||
| 58 | $discountPriceEx = false; |
||
| 59 | $discountTaxRate = 0; |
||
| 60 | if ($query->discount_value) { |
||
| 61 | if ($query->discount_type == 'amount') { |
||
| 62 | $discountPriceInc = $priceInc - $query->discount_value; |
||
| 63 | $discountPriceEx = $discountPriceInc / 1.21; |
||
| 64 | } elseif ($query->discount_type == 'percent') { |
||
| 65 | $tax = ($query->discount_value / 100) * $priceInc; |
||
| 66 | $discountPriceInc = $priceInc - $tax; |
||
| 67 | $discountPriceEx = $discountPriceInc / 1.21; |
||
| 68 | } |
||
| 69 | $discountTaxRate = $discountPriceInc - $discountPriceEx; |
||
| 70 | $discountPriceInc = $discountPriceInc; |
||
| 71 | $discountPriceEx = $discountPriceEx; |
||
| 72 | } |
||
| 73 | |||
| 74 | |||
| 75 | $output = array( |
||
| 76 | 'orginal_price_ex_tax' => $query->price, |
||
| 77 | 'orginal_price_ex_tax_number_format' => number_format($query->price, 2, '.', ''), |
||
| 78 | 'orginal_price_inc_tax' => $priceInc, |
||
| 79 | 'orginal_price_inc_tax_number_format' => number_format($priceInc, 2, '.', ''), |
||
| 80 | 'tax_rate' => $taxRate, |
||
| 81 | 'tax_value' => $taxValue, |
||
| 82 | 'currency' => 'EU', |
||
| 83 | 'discount_price_inc' => $discountPriceInc, |
||
| 84 | 'discount_price_inc_number_format' => number_format($discountPriceInc, 2, '.', ''), |
||
|
|
|||
| 85 | 'discount_price_ex' => $discountPriceEx, |
||
| 86 | 'discount_price_ex_number_format' => number_format($discountPriceEx, 2, '.', ''), |
||
| 87 | 'discount_tax_value' => $discountTaxRate, |
||
| 88 | 'discount_value' => $query->discount_value, |
||
| 89 | 'amount' => $query->amount |
||
| 90 | ); |
||
| 91 | |||
| 92 | $result = '€ '.$output['orginal_price_ex_tax_number_format'].' / € '.$output['orginal_price_inc_tax_number_format']; |
||
| 93 | |||
| 94 | |||
| 95 | if ($query->discount_value) { |
||
| 96 | $result .= '<br/> discount: yes'; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | return $result; |
||
| 101 | }) |
||
| 102 | |||
| 103 | ->addColumn('combinations', function ($query) use ($productId) { |
||
| 104 | $items = array(); |
||
| 105 | foreach ($query->combinations as $row) { |
||
| 106 | $items[] = $row->attribute->attributeGroup->title.': '.$row->attribute->value; |
||
| 107 | } |
||
| 108 | |||
| 109 | return implode(', ', $items); |
||
| 110 | }); |
||
| 111 | |||
| 112 | return $datatables->rawColumns(['amount', 'action'])->make(true); |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | return view('backend.product-combination.index')->with(array('product' => $product, 'attributeGroups' => AttributeService::selectAllGroups()->pluck('title', 'id'))); |
||
| 117 | } |
||
| 118 | |||
| 119 | return redirect()->route('product.index'); |
||
| 120 | } |
||
| 198 |