mardi 3 avril 2018

Laravel - Refactor a giant if-statement

I have an app where the user is able to choose themes and plugins but they are also able to choose none of the two. So to check all the possible options I need a 4 clause if-statement to check every situation. In the meantime, this if-statement grew exponentially and now it's very large. I used Repository for some bits to refactor the if-statement a bit but its still way too large and uclear. This is the if-statement

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)
            {
                continue;

            } 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;

        }

        $data = [$subtotal, $vat, $priceSum];

        $order =  $this->_orderRepository->createOrderIfSessionExist($data, $products);

        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;

        }

        $data = [$subtotal, $vat, $priceSum];

        $order =  $this->_orderRepository->createOrderIfSessionDoesntExist($data);

        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);
        }

    }

}

I need some help to refactor this if-statement. Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire