Completed
Push — master ( b3d33f...7c1d9a )
by Scott
02:34
created

CartRepository::deleteItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
1
<?php namespace Bedard\Shop\Repositories;
2
3
use Cookie;
4
use Session;
5
use Exception;
6
use Bedard\Shop\Models\Cart;
7
use Bedard\Shop\Models\CartItem;
8
use Bedard\Shop\Models\Inventory;
9
use Bedard\Shop\Models\CartSettings;
10
11
class CartRepository
12
{
13
    /**
14
     * @var string  Cart persistence key.
15
     */
16
    const CART_KEY = 'bedard_shop_cart';
17
18
    /**
19
     * @var \Bedard\Shop\Models\Cart
20
     */
21
    protected $cart = null;
22
23
    /**
24
     * Add an item to the curent cart.
25
     *
26
     * @param  int
27
     * @param  int
28
     * @return \Bedard\Shop\Models\Cart
29
     */
30
    public function addInventory($inventoryId, $quantity)
31
    {
32
        $inventory = Inventory::findOrFail($inventoryId);
33
34
        $cart = $this->getCart();
35
        $item = CartItem::firstOrCreate([
36
            'cart_id' => $cart->id,
37
            'inventory_id' => $inventory->id,
38
        ]);
39
40
        $item->quantity += $quantity;
41
        if ($item->quantity > $inventory->quantity) {
42
            $item->quantity = $inventory->quantity;
43
        }
44
45
        $item->save();
46
47
        return $item;
48
    }
49
50
    /**
51
     * Create a new cart.
52
     *
53
     * @return \Bedard\Shop\Models\Cart
54
     */
55
    public function create()
56
    {
57
        $this->cart = Cart::create();
58
59
        Session::put(self::CART_KEY, $this->cart->token);
60
        Cookie::queue(self::CART_KEY, $this->cart->token, CartSettings::getLifespan());
61
62
        return $this->cart;
63
    }
64
65
    /**
66
     * Delete an inventory from the cart.
67
     *
68
     * @param  int
69
     * @return \Bedard\Shop\Models\Cart
70
     */
71
    public function deleteItem($inventoryId)
72
    {
73
        $cart = $this->getCart();
74
75
        $item = $cart->items()->whereInventoryId($inventoryId)->first();
76
77
        if ($item) {
78
            return $item->delete();
79
        }
80
81
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type documented by Bedard\Shop\Repositories...tRepository::deleteItem of type Bedard\Shop\Models\Cart.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
82
    }
83
84
    /**
85
     * Determine if a cart exists.
86
     *
87
     * @return bool
88
     */
89
    public function exists()
90
    {
91
        $token = $this->getToken();
92
93
        return Cart::whereToken($token)->isOpen()->exists();
94
    }
95
96
    /**
97
     * Get the current cart, or create one if none exists.
98
     *
99
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
100
     * @return \Bedard\Shop\Models\Cart
101
     */
102
    public function getCart()
103
    {
104
        if ($this->cart !== null) {
105
            return $this->cart;
106
        }
107
108
        $token = $this->getToken();
109
110
        if (! $token) {
111
            return $this->create();
112
        }
113
114
        try {
115
            $this->cart = Cart::whereToken($token)
116
                ->isOpen()
117
                ->firstOrFail();
118
        } catch (Exception $e) {
119
            $this->cart = $this->create();
120
        }
121
122
        return $this->cart;
123
    }
124
125
    /**
126
     * Get the cart token.
127
     *
128
     * @return string
129
     */
130
    public function getToken()
131
    {
132
        $token = Session::get(self::CART_KEY);
133
        if (! $token && Cookie::has(self::CART_KEY)) {
134
            $token = Cookie::get(self::CART_KEY);
135
        }
136
137
        return $token;
138
    }
139
140
    /**
141
     * Load the related cart data.
142
     *
143
     * @return \Bedard\Shop\Models\Cart
144
     */
145
    public function loadCart()
146
    {
147
        $this->getCart()->load([
148
            'items.inventory.product' => function ($product) {
149
                $product->joinPrice()->with('thumbnails');
150
            },
151
            'items.inventory.optionValues.option',
152
        ]);
153
154
        return $this->cart;
155
    }
156
157
    /**
158
     * Set an item's quantity in the curent cart.
159
     *
160
     * @param  int
161
     * @param  int
162
     * @return \Bedard\Shop\Models\Cart
163
     */
164
    public function setInventory($inventoryId, $quantity)
165
    {
166
        $inventory = Inventory::findOrFail($inventoryId);
167
168
        if ($quantity <= 0) {
169
            return $this->deleteItem($inventory->id);
170
        }
171
172
        $cart = $this->getCart();
173
        $item = CartItem::firstOrCreate([
174
            'cart_id' => $cart->id,
175
            'inventory_id' => $inventory->id,
176
        ]);
177
178
        $item->quantity = $quantity;
179
        if ($item->quantity > $inventory->quantity) {
180
            $item->quantity = $inventory->quantity;
181
        }
182
183
        return $item->save();
184
    }
185
186
    /**
187
     * Update multiple inventories.
188
     *
189
     * @param  array  $inventories
190
     * @return void
191
     */
192
    public function updateInventories(array $inventories)
193
    {
194
        foreach ($inventories as $inventoryId => $quantity) {
195
            $this->setInventory($inventoryId, $quantity);
196
        }
197
    }
198
}
199