Completed
Push — master ( 2e0e8b...76a437 )
by Scott
08:04
created

Cart   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 98
Duplicated Lines 20.41 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
c 1
b 0
f 0
lcom 0
cbo 2
dl 20
loc 98
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 13 2
A exists() 10 10 3
A index() 10 10 2
A store() 0 11 2
A deleteItem() 0 12 2
A updateItem() 0 4 1

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 Log;
4
use Exception;
5
use Bedard\Shop\Classes\ApiController;
6
use Bedard\Shop\Repositories\CartRepository;
7
8
class Cart extends ApiController
9
{
10
    /**
11
     * Add an inventory to the cart.
12
     *
13
     * @param CartRepository $repository
14
     */
15
    public function add(CartRepository $repository)
16
    {
17
        try {
18
            $inventoryId = input('inventoryId');
19
            $quantity = input('quantity');
20
21
            return $repository->addInventory($inventoryId, $quantity);
22
        } catch (Exception $e) {
23
            Log::error($e->getMessage());
24
25
            abort(500, $e->getMessage());
26
        }
27
    }
28
29
    /**
30
     * Determine if a cart exists or not.
31
     *
32
     * @param  CartRepository $repository
33
     * @return bool
34
     */
35 View Code Duplication
    public function exists(CartRepository $repository)
36
    {
37
        try {
38
            return $repository->exists() ? 'true' : 'false';
39
        } catch (Exception $e) {
40
            Log::error($e->getMessage());
41
42
            abort(500, $e->getMessage());
43
        }
44
    }
45
46
    /**
47
     * Show the current cart.
48
     *
49
     * @param  \Bedard\Shop\Repositories\CartRepository     $repository
50
     * @return \Bedard\Shop\Models\Cart
51
     */
52 View Code Duplication
    public function index(CartRepository $repository)
53
    {
54
        try {
55
            return $repository->loadCart();
56
        } catch (Exception $e) {
57
            Log::error($e->getMessage());
58
59
            abort(500, $e->getMessage());
60
        }
61
    }
62
63
    /**
64
     * Delete an item from the cart.
65
     *
66
     * @param  CartRepository $repository
67
     * @param  int            $inventoryId
68
     * @return \Bedard\Shop\Models\Cart
69
     */
70
    public function deleteItem(CartRepository $repository, $inventoryId)
71
    {
72
        try {
73
            $repository->deleteItem($inventoryId);
74
75
            return $repository->loadCart();
76
        } catch (Exception $e) {
77
            Log::error($e->getMessage());
78
79
            abort(500, $e->getMessage());
80
        }
81
    }
82
83
    /**
84
     * Create a new cart.
85
     *
86
     * @param  \Bedard\Shop\Repositories\CartRepository     $repository
87
     * @return \Bedard\Shop\Models\Cart
88
     */
89
    public function store(CartRepository $repository)
90
    {
91
        try {
0 ignored issues
show
Unused Code introduced by
This try statement is empty and can be removed.

This check looks for try blocks that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

If there is nothing in the try then the catch block can never be executed either. Thus, these try statements can be removed completely.

Loading history...
92
93
            // @todo
94
        } catch (Exception $e) {
0 ignored issues
show
Unused Code introduced by
catch (\Exception $e) { ...0, $e->getMessage()); } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
95
            Log::error($e->getMessage());
96
97
            abort(500, $e->getMessage());
98
        }
99
    }
100
101
    public function updateItem(CartRepository $repository)
102
    {
103
        return 'hello';
104
    }
105
}
106