Test Failed
Push — master ( a4b299...6cd9b3 )
by Gianluca
15:28
created

ShopCartController::page()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 2
rs 10
cc 1
nc 1
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
class ShopCartController extends Controller
11
{
12
    public function page(){
13
        return view('mongicommerce.pages.cart');
14
    }
15
    /**
16
     * @param ProductItem $product
17
     * @return bool|\Illuminate\Http\JsonResponse
18
     */
19
    private function checkIfProductAvaible(ProductItem $product)
20
    {
21
        $num_avaibile_product = $product->quantity;
22
23
        $checkElement = Cart::where('product_item_id', $product->id)->first();
24
        //if is null it means the cart is empty
25
        if (is_null($checkElement)) {
26
            if($num_avaibile_product > 0){
27
                return true;
28
            }else{
29
                return false;
30
            }
31
        }else{
32
            //if there is something in the cart check the avaibility
33
            if ($checkElement->quantity + 1 <= $num_avaibile_product) {
34
                return true;
35
            } else {
36
                return false;
37
            }
38
        }
39
    }
40
41
    public function addToCart(Request $r)
42
    {
43
        $product_item_id = $r->get('product_item_id');
44
        $product = ProductItem::find($product_item_id);
45
        $check = $this->checkIfProductAvaible($product);
0 ignored issues
show
Bug introduced by
It seems like $product can also be of type null; however, parameter $product of Mongi\Mongicommerce\Http...checkIfProductAvaible() does only seem to accept Mongi\Mongicommerce\Models\ProductItem, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $check = $this->checkIfProductAvaible(/** @scrutinizer ignore-type */ $product);
Loading history...
46
47
        if($check){
48
            //if i'am not authenticated
49
            if (!Auth::check()) {
50
                session()->push('products.ids', $product_item_id);
51
                session()->put('cart',self::getCountableCart(session('products.ids')));
52
                return response()->json(session('cart'));
53
            }else{
54
                //if number i want is less than number avaible product i can add into cart
55
                $mycart = Cart::where('product_item_id',$product->id)->where('user_id',Auth::user()->id)->first();
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...
56
                if(is_null($mycart)){
57
                    $cart = new Cart();
58
                    $cart->user_id = Auth::user()->id;
59
                    $cart->product_item_id = $product_item_id;
60
                    $cart->quantity = 1;
61
                    $cart->save();
62
                }else{
63
                    $mycart->quantity = $mycart->quantity + 1;
64
                    $mycart->save();
65
                }
66
                return true;
67
            }
68
        }else{
69
            return response()->json([
70
                'errors' => "Prodotto non disponibile o terminato",
71
            ], 422);
72
        }
73
74
    }
75
76
    public static function getCountableCart($arr)
77
    {
78
        if (!is_null($arr)) {
79
            return array_count_values($arr);
80
        } else {
81
            return [];
82
        }
83
    }
84
85
    public function getCartElements()
86
    {
87
        if (!Auth::check()) {
88
            if (!empty(session('products.ids'))) {
89
                return response()->json(count(session('cart')));
0 ignored issues
show
Bug introduced by
It seems like session('cart') can also be of type Illuminate\Session\SessionManager and Illuminate\Session\Store; however, parameter $value of count() does only seem to accept Countable|array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
                return response()->json(count(/** @scrutinizer ignore-type */ session('cart')));
Loading history...
90
            } else {
91
                return 0;
92
            }
93
94
        } else {
95
            $products_in_cart = Cart::where('user_id', Auth::user()->id)->count();
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...
96
            return response()->json($products_in_cart);
97
        }
98
    }
99
}
100