GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

ShoppingCart   A
last analyzed

Complexity

Total Complexity 32

Size/Duplication

Total Lines 327
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 32
lcom 1
cbo 4
dl 0
loc 327
c 0
b 0
f 0
rs 9.84

16 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A getContent() 0 8 2
A update() 0 9 1
A destroy() 0 8 1
B add() 0 39 5
A reduceOneItem() 0 19 1
A remove() 0 15 1
A count() 0 5 2
A total() 0 5 2
A createUniqueIndex() 0 5 1
A modify() 0 18 1
A getItemPrice() 0 10 2
A updateItem() 0 22 2
A validateQty() 0 6 3
A validateIndex() 0 11 3
A itemQtyStatusCheck() 0 7 2
1
<?php
2
3
namespace BahaaAlhagar\ShoppingCart;
4
5
use Illuminate\Support\Facades\Session;
6
use Illuminate\Contracts\Events\Dispatcher;
7
use BahaaAlhagar\ShoppingCart\Exceptions\CartIsEmptyException;
8
use BahaaAlhagar\ShoppingCart\Exceptions\InvalidQuantityException;
9
use BahaaAlhagar\ShoppingCart\Exceptions\UnknownUniqueIndexException;
10
11
class ShoppingCart
12
{
13
    public $items = null;
14
    public $totalQty;
15
    public $totalPrice;
16
17
    protected $events;
18
19
    /**
20
     * Create a new Skeleton Instance
21
     */
22
    public function __construct(Dispatcher $events)
23
    {
24
        $oldCart = Session::has('cart') ? Session::get('cart') : null;
25
26
        if ($oldCart) {
27
            $this->items = $oldCart->items;
28
            $this->totalQty = $oldCart->totalQty;
29
            $this->totalPrice = $oldCart->totalPrice;
30
        }
31
        
32
        $this->events = $events;
33
    }
34
35
    /**
36
     * get the cart from the Session
37
     *
38
     * @return get the session cart array and the cart object or null
39
     */
40
    public function getContent()
41
    {
42
        // Get the Cart from the Session if there is one
43
        $cart = Session::has('cart') ? Session::get('cart') : null;
44
45
        // return the Cart
46
        return $cart;
47
    }
48
49
    /**
50
     * update the cart in the Session
51
     *
52
     * @return update the session cart array and the cart object
53
     */
54
    public function update()
55
    {
56
        // unset the events add the Cart to the Session
57
        $this->events = null;
58
        Session::put('cart', $this);
59
60
        // return the Cart if needed
61
        return $this;
62
    }
63
64
    /**
65
     * empty the cart in the Session
66
     *
67
     * @return empty the session cart array
68
     */
69
    public function destroy()
70
    {
71
        // Cart destroyed event
72
        $this->events->fire('cart.destroyed', $this);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
74
        // remove the Cart from the Session
75
        Session::forget('cart');
76
    }
77
78
    /**
79
     * Add 1 item to the Cart
80
     *
81
     * @param object $item item to add
82
     *
83
     * @return update the session cart array and the cart object
84
     */
85
    public function add($item, $qty = null, array $options = null)
86
    {
87
        // if it is a mutated item by using options then give it another offset
88
        // else use the item model id
89
        $uniqueIndex = $options ? $this->createUniqueIndex($item->id, $options) : $item->id;
90
91
        $this->validateQty($qty);
92
93
        // if there is no certain qty provided by the method then set it to 1
94
        $Qty = $qty ? $qty : 1;
95
96
        // Cart item structure
97
        $storedItem = ['qty' => 0, 'price' => $item->price, 'options' => $options, 'item' => $item->toArray()];
98
99
100
        // check if the item exists in the cart before
101
        // and if it exists then get it from the cart
102
        if ($this->items) {
103
            if (array_key_exists($uniqueIndex, $this->items)) {
104
                $storedItem = $this->items[$uniqueIndex];
105
            }
106
        }
107
108
        // update the cart increase qty and price
109
        $storedItem['qty'] += $Qty;
110
        $storedItem['price'] = $item->price * $storedItem['qty'];
111
        $this->items[$uniqueIndex] = $storedItem;
112
        $this->totalQty += $Qty;
113
        $this->totalPrice += ($Qty * $item->price);
114
115
        // cartItem added event
116
        $this->events->fire('cartItem.added', $this->items[$uniqueIndex]);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
117
118
        // add the cart to the Session
119
        $this->update();
120
        
121
        // return the cart item that has been added
122
        return $this->items[$uniqueIndex];
123
    }
124
125
    /**
126
     * Reduce 1 item from the Cart
127
     *
128
     * @param object $item item to reduce by 1
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
129
     *
130
     * @return update the session cart array and the cart object
131
     */
132
    public function reduceOneItem($uniqueIndex)
133
    {
134
        $this->validateIndex($uniqueIndex);
135
136
        // decrease the qty and the price in the cart
137
        $this->items[$uniqueIndex]['qty']--;
138
        $this->items[$uniqueIndex]['price'] -= $this->items[$uniqueIndex]['item']['price'];
139
        $this->totalQty--;
140
        $this->totalPrice -= $this->items[$uniqueIndex]['item']['price'];
141
142
        // cartItem modified event
143
        $this->events->fire('cartItem.modified', $this->items[$uniqueIndex]);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
144
145
        // if the item qty is 0 or less remove the item from Cart
146
        $this->itemQtyStatusCheck($uniqueIndex);
147
148
        // update the Cart in the Session
149
        $this->update();
150
    }
151
152
    /**
153
     * Remove item from the Cart
154
     *
155
     * @param object $item item to remove
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
156
     *
157
     * @return update the session cart array and the cart object
158
     */
159
    public function remove($uniqueIndex)
160
    {
161
        // remove item qty and price from cart
162
        $this->totalQty -= $this->items[$uniqueIndex]['qty'];
163
        $this->totalPrice -= $this->items[$uniqueIndex]['price'];
164
165
        // cartItem removed event
166
        $this->events->fire('cartItem.removed', $this->items[$uniqueIndex]);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
167
168
        // remove the cart item
169
        unset($this->items[$uniqueIndex]);
170
171
        // update the Cart in the Session
172
        $this->update();
173
    }
174
175
    /**
176
     * get the cart items count
177
     *
178
     * @return the cart total quantity
179
     */
180
    public function count()
181
    {
182
        // we need id for the array index
183
        return $this->totalQty ? $this->totalQty : null;
184
    }
185
186
    /**
187
     * get the cart items total price
188
     *
189
     * @return the cart total price
190
     */
191
    public function total()
192
    {
193
        // we need id for the array index
194
        return $this->totalPrice ? $this->totalPrice : null;
195
    }
196
197
    /**
198
     * create unique index
199
     *
200
     * @param item
201
     *
202
     * @param options array
203
     *
204
     * @return uniqueIndex for the shopping cart
205
     */
206
    public function createUniqueIndex($id, $options)
207
    {
208
        $uniqueIndex = md5($id.serialize($options));
209
        return $uniqueIndex;
210
    }
211
212
    /**
213
     * update item in the Cart
214
     *
215
     * @param unique item index to modify
216
     *
217
     * @param amount to edit
218
     *
219
     * @return update the session cart array and the cart object
220
     */
221
    public function modify($uniqueIndex, $qty)
222
    {
223
        $this->validateQty($qty);
224
225
        $this->validateIndex($uniqueIndex);
226
227
        // update the cart item
228
        $this->updateItem($uniqueIndex, $qty);
229
230
        // cartItem modified event
231
        $this->events->fire('cartItem.modified', $this->items[$uniqueIndex]);
0 ignored issues
show
Bug introduced by
The method fire() does not seem to exist on object<Illuminate\Contracts\Events\Dispatcher>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
232
233
        // if the item qty is 0 or less remove the item from Cart
234
        $this->itemQtyStatusCheck($uniqueIndex);
235
236
        // add the cart to the Session
237
        $this->update();
238
    }
239
240
    /**
241
     * get item price
242
     *
243
     * @param unique index
244
     *
245
     * @return the item price
246
     */
247
    public function getItemPrice($uniqueIndex)
248
    {
249
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array<*,array> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
250
            throw new CartIsEmptyException('cart is empty!');
251
        }
252
253
        $item = $this->items[$uniqueIndex];
254
255
        return $itemPrice = $item['price']/$item['qty'];
0 ignored issues
show
Unused Code introduced by
$itemPrice is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
256
    }
257
258
    /**
259
     * update the item depends on the qty
260
     *
261
     * @param unique index
262
     *
263
     * @param integer $qty
264
     *
265
     * @return void
266
     */
267
    public function updateItem($uniqueIndex, $qty)
268
    {
269
        // get the current item from the cart
270
        $currentItem = $this->items[$uniqueIndex];
271
272
        // if its the same qty do nothing
273
        if ($currentItem['qty'] == $qty) {
274
            return false;
275
        }
276
277
        $currentItemPrice = $this->getItemPrice($uniqueIndex);
278
        
279
        $this->totalPrice -= $currentItem['price'];
280
        $this->totalPrice += ($qty * $currentItemPrice);
281
282
        $this->totalQty -= $currentItem['qty'];
283
        $this->totalQty += $qty;
284
285
        $this->items[$uniqueIndex]['qty'] = $qty;
286
287
        $this->items[$uniqueIndex]['price'] = $qty * $currentItemPrice;
288
    }
289
290
    /**
291
     * validate quantity integer and not negative
292
     *
293
     * @param  $qty
294
     *
295
     * @return void
296
     */
297
    public function validateQty($qty)
298
    {
299
        if (is_float($qty + 0) || $qty < 0) {
300
            throw new InvalidQuantityException('Invalid quantity!');
301
        }
302
    }
303
304
    /**
305
     * check if the cart contain certain offset or not
306
     *
307
     * @param  $uniqueIndex
308
     *
309
     * @return void
310
     */
311
    public function validateIndex($uniqueIndex)
312
    {
313
        // check if the item exists in the cart before
314
        if (!$this->items) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->items of type array<*,array> is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
315
            throw new CartIsEmptyException('Cart is empty!');
316
        }
317
318
        if (!array_key_exists($uniqueIndex, $this->items)) {
319
            throw new UnknownUniqueIndexException("The cart does not contain this index {$uniqueIndex}.");
320
        }
321
    }
322
323
    /**
324
     * check if item qty is <= 0 and remove item if true
325
     *
326
     * @param  $uniqueIndex
327
     *
328
     * @return void
329
     */
330
    public function itemQtyStatusCheck($uniqueIndex)
331
    {
332
        // if the qty is 0 or less remove the item from Cart
333
        if ($this->items[$uniqueIndex]['qty'] <= 0) {
334
            unset($this->items[$uniqueIndex]);
335
        }
336
    }
337
}
338