Test Failed
Push — master ( 6cd9b3...9edbfe )
by Gianluca
08:33
created

ShopSummaryController::page()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 42
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 42
rs 9.0648
cc 5
nc 3
nop 0
1
<?php
2
namespace Mongi\Mongicommerce\Http\Controllers\shop;
3
4
use Illuminate\Http\Request;
5
use Illuminate\Support\Facades\Auth;
6
use Mongi\Mongicommerce\Http\Controllers\Controller;
7
use Mongi\Mongicommerce\Models\Cart;
8
use Mongi\Mongicommerce\Models\ProductItem;
9
10
11
class ShopSummaryController extends Controller
12
{
13
    public function page(){
14
        $products = [];
15
        $note = session('checkout.note_delivery');
16
        $get_in_shop_checkbox = session('checkout.get_in_shop_checkbox');
17
        $total = 0;
18
        if (!Auth::check()) {
19
            $ids = [];
20
            $productsCart = session('cart');
21
            if(isset($productsCart)){
22
                foreach ($productsCart as $id => $count) {
23
                    $product = ProductItem::where('id', $id)->first();
24
                    $ids[] = $id;
25
                    $products[] = [
26
                        'detail' => $product,
27
                        'single_price' => $product->price,
28
                        'count' => $count,
29
                        '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...
30
                        'sum_weight' => $product->weight * $count,
31
                        'total' => $product->price * $count
32
                    ];
33
                    $total += $product->price * $count;
34
                }
35
            }
36
        } else {
37
            $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...
38
            foreach ($productsCart as $element) {
39
                $product = ProductItem::where('id', $element->product_id)->first();
0 ignored issues
show
Bug introduced by
The property product_id does not exist on Mongi\Mongicommerce\Models\Cart. Did you mean product_item_id?
Loading history...
40
                $products[] = [
41
                    'detail' => $product,
42
                    'single_price' => floatval($product->price),
43
                    'count' => $element->quantity,
44
                    'single_weight' => $product->weight,
45
                    'sum_weight' => $element->count * $product->weight,
0 ignored issues
show
Bug introduced by
The property count does not seem to exist on Mongi\Mongicommerce\Models\Cart. 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...
46
                    'total' => $product->price * $element->count
47
                ];
48
49
                $total += $product->price * $element->count;
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