Passed
Push — main ( 5de758...6c1d70 )
by Carsten
13:39
created

EcommerceController::inc()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 12
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lenius\LaravelEcommerce\Http\Controllers;
4
5
use Illuminate\Contracts\View\View;
6
use Illuminate\Http\RedirectResponse;
7
use Illuminate\Http\Request;
8
use Lenius\Basket\Item;
9
use Lenius\Basket\ItemInterface;
10
use Lenius\LaravelEcommerce\Facades\Basket;
11
12
class EcommerceController extends Controller
13
{
14 4
    public static function routes(): void
15
    {
16 4
        $router = app()->make('router');
17
18 4
        $router->get('basket', [EcommerceController::class, 'index'])->name('ecommerce.basket');
19 4
        $router->get('basket/debug', [EcommerceController::class, 'debug'])->name('ecommerce.basket.debug');
20 4
        $router->get('basket/demo', [EcommerceController::class, 'demo'])->name('ecommerce.basket.demo');
21 4
        $router->get('basket/destroy', [EcommerceController::class, 'destroy'])->name('ecommerce.basket.destroy');
22 4
        $router->post('basket/update', [EcommerceController::class, 'update'])->name('ecommerce.basket.update');
23
24 4
        $router->any('basket/{id}/add', [EcommerceController::class, 'add'])->name('ecommerce.basket.item.add');
25 4
        $router->get('basket/{id}/dec', [EcommerceController::class, 'dec'])->name('ecommerce.basket.item.dec');
26 4
        $router->get('basket/{id}/inc', [EcommerceController::class, 'inc'])->name('ecommerce.basket.item.inc');
27 4
        $router->get('basket/{id}/remove', [EcommerceController::class, 'remove'])->name('ecommerce.basket.item.remove');
28 4
    }
29
30
    public function index(): View
31
    {
32
        return view('ecommerce::basket');
33
    }
34
35 1
    public function add(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function add(Request $request, /** @scrutinizer ignore-unused */ $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

35
    public function add(/** @scrutinizer ignore-unused */ Request $request, $id)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
36
    {
37 1
        $item = [
38
            'id'            => 1,
39
            'number'        => 'zxy',
40
            'name'          => 'My product',
41
            'stock'         => 'In stock',
42
            'unit'          => 'M',
43
            'tax'           => 25,
44
            'price'         => 100,
45
            'weight'        => 100,
46
            'quantity'      => 1,
47
            'type'          => 'item',
48
            'link'          => '',
49
        ];
50
51 1
        Basket::insert(new Item($item));
52
53 1
        return redirect()->route('ecommerce.basket');
54
    }
55
56 4
    public function debug(): array
57
    {
58
        return [
59 4
            'items'        => Basket::contents(),
60 4
            'sum'          => Basket::total(false),
61 4
            'tax'          => Basket::tax(),
62 4
            'total'        => Basket::total(),
63 4
            'weight'       => Basket::weight(),
64 4
            'total_items'  => Basket::totalItems(),
65
        ];
66
    }
67
68 1
    public function destroy(): RedirectResponse
69
    {
70 1
        Basket::destroy();
71
72 1
        return redirect()->route('ecommerce.basket');
73
    }
74
75
    public function update(Request $request): RedirectResponse
76
    {
77
        $items = $request->input('quantity');
78
79
        if ($items) {
80
            /* @var ItemInterface $item */
81
            foreach ($items as $itemIdentifier => $quantity) {
82
                if ($item = Basket::item($itemIdentifier)) {
83
                    if ($quantity > 0) {
84
                        $item->quantity = (int) $quantity;
85
                    } else {
86
                        Basket::remove($itemIdentifier);
87
                    }
88
                }
89
            }
90
        }
91
92
        return redirect()->route('ecommerce.basket');
93
    }
94
95 1
    public function inc(string $itemIdentifier): RedirectResponse
96
    {
97
        /** @var ItemInterface $item */
98 1
        if ($item = Basket::item($itemIdentifier)) {
99 1
            if ($item->quantity > 0) {
100 1
                ++$item->quantity;
101
            } else {
102
                Basket::remove($itemIdentifier);
103
            }
104
        }
105
106 1
        return redirect()->route('ecommerce.basket');
107
    }
108
109 1
    public function dec(string $itemIdentifier): RedirectResponse
110
    {
111
        /** @var ItemInterface $item */
112 1
        if ($item = Basket::item($itemIdentifier)) {
113 1
            if ($item->quantity > 1) {
114
                --$item->quantity;
115
            } else {
116 1
                Basket::remove($itemIdentifier);
117
            }
118
        }
119
120 1
        return redirect()->route('ecommerce.basket');
121
    }
122
123
    public function remove(string $itemIdentifier): RedirectResponse
124
    {
125
        if (Basket::item($itemIdentifier)) {
126
            Basket::remove($itemIdentifier);
127
        }
128
129
        return redirect()->route('ecommerce.basket');
130
    }
131
132 3
    public function demo(): RedirectResponse
133
    {
134 3
        $item = [
135
            'id'            => 1,
136
            'number'        => 'zxy',
137
            'name'          => 'My product',
138
            'stock'         => 'In stock',
139
            'unit'          => 'M',
140
            'tax'           => 25,
141
            'price'         => 100,
142
            'weight'        => 100,
143
            'quantity'      => 1,
144
            'type'          => 'item',
145
            'link'          => '',
146
        ];
147
148 3
        Basket::insert(new Item($item));
149
150 3
        return redirect()->route('ecommerce.basket');
151
    }
152
}
153