ShopSummaryController::page()   A
last analyzed

Complexity

Conditions 5
Paths 3

Size

Total Lines 43
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 43
rs 9.0648
cc 5
nc 3
nop 0
1
<?php
2
namespace Mongi\Mongicommerce\Http\Controllers\shop;
3
4
use Illuminate\Support\Facades\Auth;
5
use Mongi\Mongicommerce\Http\Controllers\Controller;
6
use Mongi\Mongicommerce\Models\Cart;
7
use Mongi\Mongicommerce\Models\ProductItem;
8
9
10
class ShopSummaryController extends Controller
11
{
12
    public function page(){
13
        $products = [];
14
        $note = session('checkout.note_delivery');
15
        $get_in_shop_checkbox = session('checkout.get_in_shop_checkbox');
16
        $total = 0;
17
        if (!Auth::check()) {
18
            $ids = [];
19
            $productsCart = session('cart');
20
            if(isset($productsCart)){
21
                foreach ($productsCart as $id => $count) {
22
                    $product = ProductItem::where('id', $id)->first();
23
                    $ids[] = $id;
24
                    $products[] = [
25
                        'detail' => $product,
26
                        'single_price' => $product->price,
27
                        'count' => $count,
28
                        'single_weight' => $product->weight,
0 ignored issues
show
Bug introduced by
The property weight does not seem to exist on Mongi\Mongicommerce\Models\ProductItem. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
29
                        'sum_weight' => $product->weight * $count,
30
                        'total' => $product->price * $count
31
                    ];
32
                    $total += $product->price * $count;
33
                }
34
            }
35
        } else {
36
            $productsCart = Cart::where('user_id', Auth::user()->id)->get();
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
37
            foreach ($productsCart as $element) {
38
                $product = ProductItem::where('id', $element->product_item_id)->first();
39
40
                $products[] = [
41
                    'detail' => $product,
42
                    'single_price' => floatval($product->price),
43
                    'count' => $element->quantity,
44
                    'single_weight' => $product->weight,
45
                    'sum_weight' => $element->quantity * $product->weight,
46
                    'total' => $product->price * $element->quantity
47
                ];
48
49
                $total += $product->price * $element->quantity;
50
            }
51
        }
52
            $shipping_price = 0;
53
            session()->put('checkout.total', $total + $shipping_price);
54
            return view('mongicommerce.pages.summary',compact('products','total','shipping_price','note','get_in_shop_checkbox'));
55
    }
56
}
57