Completed
Push — master ( bcc979...d4660e )
by Scott
02:49
created

Cart::applyPromotion()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 12
loc 12
rs 9.4285
cc 2
eloc 7
nc 3
nop 1
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
     * Apply a promotion to the cart.
31
     *
32
     * @param  CartRepository $repository
33
     * @return [type]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
34
     */
35 View Code Duplication
    public function applyPromotion(CartRepository $repository)
36
    {
37
        try {
38
            $code = input('code');
39
40
            return $repository->applyPromotion($code);
41
        } catch (Exception $e) {
42
            Log::error($e->getMessage());
43
44
            abort(500, $e->getMessage());
45
        }
46
    }
47
48
    /**
49
     * Determine if a cart exists or not.
50
     *
51
     * @param  CartRepository $repository
52
     * @return bool
53
     */
54 View Code Duplication
    public function exists(CartRepository $repository)
55
    {
56
        try {
57
            return $repository->exists() ? 'true' : 'false';
58
        } catch (Exception $e) {
59
            Log::error($e->getMessage());
60
61
            abort(500, $e->getMessage());
62
        }
63
    }
64
65
    /**
66
     * Show the current cart.
67
     *
68
     * @param  \Bedard\Shop\Repositories\CartRepository     $repository
69
     * @return \Bedard\Shop\Models\Cart
70
     */
71 View Code Duplication
    public function index(CartRepository $repository)
72
    {
73
        try {
74
            return $repository->loadCart();
75
        } catch (Exception $e) {
76
            Log::error($e->getMessage());
77
78
            abort(500, $e->getMessage());
79
        }
80
    }
81
82
    /**
83
     * Delete an item from the cart.
84
     *
85
     * @param  CartRepository $repository
86
     * @param  int            $inventoryId
87
     * @return \Bedard\Shop\Models\Cart
88
     */
89
    public function deleteItem(CartRepository $repository, $inventoryId)
90
    {
91
        try {
92
            $repository->deleteItem($inventoryId);
93
94
            return $repository->loadCart();
95
        } catch (Exception $e) {
96
            Log::error($e->getMessage());
97
98
            abort(500, $e->getMessage());
99
        }
100
    }
101
102
    /**
103
     * Create a new cart.
104
     *
105
     * @param  \Bedard\Shop\Repositories\CartRepository     $repository
106
     * @return \Bedard\Shop\Models\Cart
107
     */
108
    public function store(CartRepository $repository)
109
    {
110
        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...
111
112
            // @todo
113
        } 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...
114
            Log::error($e->getMessage());
115
116
            abort(500, $e->getMessage());
117
        }
118
    }
119
120
    /**
121
     * Update an item in the cart.
122
     *
123
     * @param  CartRepository $repository
124
     * @return [type]
0 ignored issues
show
Documentation introduced by
The doc-type [type] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
125
     */
126 View Code Duplication
    public function updateItem(CartRepository $repository, $itemId)
127
    {
128
        try {
129
            $quantity = input('quantity');
130
131
            return $repository->updateItem($itemId, $quantity);
132
        } catch (Exception $e) {
133
            Log::error($e->getMessage());
134
135
            abort(500, $e->getMessage());
136
        }
137
    }
138
}
139