代码之家  ›  专栏  ›  技术社区  ›  Saleh Mehdiyev

我想重构我的Laravel代码以遵守SOLID原则

  •  -3
  • Saleh Mehdiyev  · 技术社区  · 1 年前
        $itemPrice = CartItem::where('order_code',$orderCode)->pluck('total_price');
        $totalPrices = $itemPrice->sum();
       
        $orderCode = session('order_code');
        $gateway = 'banka';
        $total = $totalPrices;
        $bin_number = rand(1000,10000);
        $order_id = rand(1,100);
        $status = 1;
        $error_message = null;
    
        try
        {
          $paymentSuccessfull = false;
          if(!$paymentSuccessfull)
          {
            throw new \Exception('basarisiz'); 
          }
         Transaction::create([
            'gateway'=>$gateway,
            'order_id'=>$order_id,
            'total'=>$total,
            'eror_message'=>$error_message,
            'bin_number'=>$bin_number,
            'status'=>$status,
          ]);
          session()->forget('order_code');
          return response()->json(['message' => 'siparis tamamlandi']);
        }
        catch(\Exception $e)
        {
            $error_message = $e->getMessage();
            $status = 0;
            Transaction::where('order_id', $order_id)->update([
              'eror_message' => $error_message,
              'status' => $status,
          ]);
    
            
            return response()->json(['error' => $error_message]);
        }
      
    

    我想让这段代码更可读,消除冗余,只执行一次创建操作,并有条件地更改状态值。“

    我试图让代码更干净,但在这样做的时候,我无法将数据保存到数据库中。“

    0 回复  |  直到 1 年前