I have a laravel application and in a certain store method, there are 128 lines of code. I need to refactor things that are repeating. I have a vague idea on how to do this but I'm not sure. This is the whole code (Do have in mind, Its really large).
public function store(Request $request)
{
$selectedPlugin = null;
$selectedPlugins = array();
$price = null;
foreach($request->input('plugin') as $key => $value) {
if ($value === 'selected') {
$selectedPlugin = $key;
$plugin = Product::find($selectedPlugin);
if($plugin == null)
{
break;
} elseif ($plugin != null) {
$price += $plugin->price;
echo "ID: " . $plugin->id . "<br>";
$selectedPlugins[$plugin->id] = $plugin->toArray();
$request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id, $plugin->toArray());
$request->session()->put('chosen_plugins.' . 'PluginID' . $plugin->id .'.composer_package', $plugin->productable->composer_package);
}
}
}
if(session()->exists('chosen_plugins') == true) {
$products = Session::get('chosen_plugins');
if(session()->exists('chosen_theme') == true)
{
$products['theme'] = Session::get('chosen_theme');
$themePrice = Session::get('chosen_theme')['price'];
$subtotal = $price + $themePrice;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
} elseif (session()->exists('chosen_theme') == false) {
$subtotal = $price;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
}
$order = new Order;
$order->user_id = Auth()->id();
$latestOrder = Order::orderBy('id', 'DESC')->first();
$order->order_nr = '#'.str_pad($latestOrder->id + 1, 8, "0", STR_PAD_LEFT);
$order->sub_total = $subtotal;
$order->vat = $vat;
$order->price_sum = $priceSum;
$order->save();
foreach($products as $product) {
$orderItems = new Orderitems;
$orderItems->order_id = $order->id;
$orderItems->product_id = $product['id'];
$orderItems->price = $product['price'];
$orderItems->save();
}
return redirect()->to('/ordersummary/'. $order->id);
} elseif (session()->exists('chosen_plugins') == false ) {
if(session()->exists('chosen_theme') == true)
{
$theme = Session::get('chosen_theme');
$subtotal = $theme['price'];
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
} elseif (session()->exists('chosen_theme') == false) {
$subtotal = 35;
$vat = 21/100 * $subtotal;
$priceSum = $subtotal + $vat;
}
$order = new Order;
$order->user_id = Auth()->id();
$latestOrder = Order::orderBy('id', 'DESC')->first();
$order->order_nr = '#'.str_pad($latestOrder->id + 1, 8, "0", STR_PAD_LEFT);
$order->sub_total = $subtotal;
$order->vat = $vat;
$order->price_sum = $priceSum;
$order->save();
if (session()->exists('chosen_theme') == true) {
$orderItems = new Orderitems;
$orderItems->order_id = $order->id;
$orderItems->product_id = $theme['id'];
$orderItems->price = $theme['price'];
$orderItems->save();
return redirect()->to('/ordersummary/'. $order->id);
} elseif (session()->exists('chosen_theme') == false) {
return redirect()->to('/ordersummary/'. $order->id);
}
}
}
As you can see, A lot of the code is repeating itself. Things like creating the order and defining the prices. These can be a lot cleaner. Besides that, the code is working perfectly fine. Also, the whole method is a big IF-statement. How can I refactor this method so it looks clean and the repeating pieces of code are no longer present?
Thanks in advance!
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire