Completed
Push — master ( a6516d...da5039 )
by Scott
224:29 queued 164:15
created

CartRepository::add()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 17
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
1
<?php namespace Bedard\Shop\Repositories;
2
3
use Bedard\Shop\Models\Cart;
4
use Bedard\Shop\Models\CartItem;
5
use Bedard\Shop\Models\CartSettings;
6
use Bedard\Shop\Models\Inventory;
7
use Cookie;
8
use Session;
9
10
class CartRepository
11
{
12
    /**
13
     * @var string  Cart persistence key.
14
     */
15
    const CART_KEY = 'bedard_shop_cart';
16
17
    /**
18
     * Add an item to the curent cart.
19
     *
20
     * @param  \Bedard\Shop\Models\Inventory
21
     * @param  int
22
     * @return \Bedard\Shop\Models\Cart
23
     */
24
    public function add(Inventory $inventory, $quantity)
25
    {
26
        $cart = $this->find();
27
        $item = CartItem::firstOrCreate([
28
            'cart_id' => $cart->id,
29
            'inventory_id' => $inventory->id,
30
        ]);
31
32
        $item->quantity += $quantity;
33
        if ($item->quantity > $inventory->quantity) {
34
            $item->quantity = $inventory->quantity;
35
        }
36
37
        $item->save();
38
39
        return $cart;
40
    }
41
42
    /**
43
     * Create a new cart.
44
     *
45
     * @return \Bedard\Shop\Models\Cart
46
     */
47
    public function create()
48
    {
49
        $cart = Cart::create();
50
51
        Session::put(self::CART_KEY, $cart->token);
52
        Cookie::queue(self::CART_KEY, $cart->token, CartSettings::getLifespan());
53
54
        return $cart;
55
    }
56
57
    /**
58
     * Delete an inventory from the cart.
59
     *
60
     * @param  Inventory $inventory
61
     * @return \Bedard\Shop\Models\Cart
62
     */
63
    public function delete(Inventory $inventory)
64
    {
65
        $cart = $this->find();
66
67
        $item = $cart->items()->whereInventoryId($inventory->id)->first();
68
69
        if ($item) {
70
            $item->delete();
71
        }
72
73
        $cart->load('items');
74
75
        return $cart;
76
    }
77
78
    /**
79
     * Get the current cart, or create one if none exists.
80
     *
81
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
82
     * @return \Bedard\Shop\Models\Cart
83
     */
84
    public function find()
85
    {
86
        $token = Session::get(self::CART_KEY);
87
88
        if (! $token && Cookie::has(self::CART_KEY)) {
89
            $token = Cookie::get(self::CART_KEY);
90
        }
91
92
        if (! $token) {
93
            return $this->create();
94
        }
95
96
        return Cart::whereToken($token)
97
            ->isOpen()
98
            ->with('items')
99
            ->firstOrFail();
100
    }
101
102
    /**
103
     * Update an item in the curent cart.
104
     *
105
     * @param  \Bedard\Shop\Models\Inventory
106
     * @param  int
107
     * @return \Bedard\Shop\Models\Cart
108
     */
109
    public function update(Inventory $inventory, $quantity)
110
    {
111
        if ($quantity <= 0) {
112
            return $this->delete($inventory);
113
        }
114
115
        $cart = $this->find();
116
        $item = CartItem::firstOrCreate([
117
            'cart_id' => $cart->id,
118
            'inventory_id' => $inventory->id,
119
        ]);
120
121
        $item->quantity = $quantity;
122
        if ($item->quantity > $inventory->quantity) {
123
            $item->quantity = $inventory->quantity;
124
        }
125
126
        $item->save();
127
128
        return $cart;
129
    }
130
}
131