Completed
Push — master ( 47d334...2a4dc4 )
by Scott
02:21
created

CartRepository::find()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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