首页 > 解决方案 > 重用 Laravel 模型创建代码的代码

问题描述

我使用这个自定义类代码来处理 Laravel 中不同类型的订单。此问题包括有关订单类型 A 的代码。

我想知道如何在创建对象时重用代码。因为我看到代码中有很多重复。

我有不同的订单类型,功能相同但行为不同。这就是我使用抽象类来扩展所有订单类型的原因。

abstract class BaseOrder
{
    abstract public function getOrder(Order $order);

    abstract public function storeOrder(Request $request);

    ...
}

class OrderTypeA extends BaseOrder
{
    private $type = 'A';
   
    public function storeOrder($request)
    {
        $returnedValues = DB::transaction(function () use ($request) {

            $order = new Order;
            $order->type = $this->type;
            $order->from = $request->json('from');
            $order->amount = $request->json('amount');
            $order->status = 2;
            $order->save();

            foreach ($request->json('items') as $item) {
                $order->items()->create([
                    'product_id' => $item['product_id'],
                    'quantity' => $item['quantity'],
                    'total' => $item['total'],
                ]);
            }
            return compact('order');
        });

        return $returnedValues['order']->load('items');
    }

    public function updateOrder($request)
    {
        $returnedValues = DB::transaction(function () use ($request) {

            if ($request->id) {
                $order = Order::findOrFail($request->id);
                $order->amount = $request->json('amount');
                $order->status = 1;
                $order->save();

                $order->items()->delete();

                foreach ($request->json('items') as $item) {
                    $order->items()->create([
                        'product_id' => $item['product_id'],
                        'quantity' => $item['quantity'],
                        'total' => $item['total'],
                    ]);
                }
            } else {
                $order = new Order;
                $order->type = $this->type;
                $order->from = $request->json('from');
                $order->to = null;
                $order->amount = $request->json('amount');
                $order->status = 1;
                $order->save();

                foreach ($request->json('items') as $item) {
                    $order->items()->create([
                        'product_id' => $item['product_id'],
                        'quantity' => $item['quantity'],
                        'total' => $item['total'],
                    ]);
                }
            }
            return compact('order');
        });

        return $returnedValues['order']->load('items');
    }

    public function generateOrder($customer)
    {
        $order = new stdClass();
        $order->id = null;
        $order->type = $this->type;
        $order->from = $customer->id;
        $order->to = null;
        $order->status = 1;

        $totalAmount = 0;
        $items = [];
        $stocks = Stock::where('type', 1)
            ->where('customer_id', $customer->id)
            ->whereRaw('stock <= reorder_level')
            ->get();

        if (count($stocks) > 0) {
            foreach ($stocks as $key => $stock) {
                if ($stock->minimum_order_quantity != 0) {
                    $priceListItem = PriceList::where('product_id', $stock->product_id)->latest()->first();
                    $item = new stdClass;
                    $item->id = $key;
                    $item->product_id = $stock->product_id;
                    $item->product = Product::findOrFail($stock->product_id);
                    $item->order_id = null;
                    $item->quantity = (int) $stock->minimum_order_quantity;
                    $item->total = ((int) $stock->minimum_order_quantity * ((float) $priceListItem->price));
                    $item->is_checked = true;
                    $item->created_at = null;
                    $item->updated_at = null;

                    $totalAmount += $item->total;
                    $items[] = $item;
                }
            }
        }
        $order->amount = $totalAmount;
        $order->items = $items;
        return $order;
    }
}

标签: phplaraveleloquentrefactoring

解决方案


推荐阅读