CartApi   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 71
Duplicated Lines 19.72 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 3
dl 14
loc 71
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 7 7 2
A index() 0 10 3
A remove() 0 6 1
A touch() 0 4 1
A update() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace Bedard\Shop\Api;
2
3
use Bedard\Shop\Classes\ApiController;
4
use Bedard\Shop\Models\ApiSettings;
5
use Bedard\Shop\Repositories\CartRepository;
6
7
class CartApi extends ApiController
8
{
9
    /**
10
     * Add an item to the cart.
11
     *
12
     * @param  CartRepository               $repository
13
     * @return \Bedard\Shop\Models\CartItem
14
     */
15 View Code Duplication
    public function add(CartRepository $repository)
16
    {
17
        $inventoryId = (int) input('inventoryId');
18
        $quantity = (int) input('quantity') ?: 1;
19
20
        return $repository->add($inventoryId, $quantity);
21
    }
22
23
    /**
24
     * Find the current cart.
25
     *
26
     * @param  CartRepository           $repository
27
     * @return \Bedard\Shop\Models\Cart
28
     */
29
    public function index(CartRepository $repository)
30
    {
31
        $options = ApiSettings::getCartOptions();
32
33
        if (! array_key_exists('is_enabled', $options) || ! $options['is_enabled']) {
34
            return abort(403, 'Forbidden');
35
        }
36
37
        return $repository->findOrNew($options);
38
    }
39
40
    /**
41
     * Remove an item from the cart.
42
     *
43
     * @param  CartRepository $repository
44
     * @return \Bedard\Shop\Models\CartItem
45
     */
46
    public function remove(CartRepository $repository)
47
    {
48
        $itemId = (int) input('itemId');
49
50
        return $repository->remove($itemId);
51
    }
52
53
    /**
54
     * Touch a cart.
55
     *
56
     * @param  CartRepository $repository
57
     * @return \Bedard\Shop\Models\Cart
58
     */
59
    public function touch(CartRepository $repository)
60
    {
61
        return $repository->touch();
62
    }
63
64
    /**
65
     * Add or remove quantity to an existing CartItem.
66
     *
67
     * @param  CartRepository               $repository
68
     * @return \Bedard\Shop\Models\CartItem
69
     */
70 View Code Duplication
    public function update(CartRepository $repository)
71
    {
72
        $inventoryId = (int) input('inventoryId');
73
        $quantity = (int) input('quantity') ?: 1;
74
75
        return $repository->update($inventoryId, $quantity);
76
    }
77
}
78