EcommerceController::index()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Lenius\LaravelEcommerce\Http\Controllers;
4
5
use Illuminate\Contracts\Foundation\Application;
6
use Illuminate\Contracts\View\Factory;
7
use Illuminate\Contracts\View\View;
8
use Illuminate\Http\RedirectResponse;
9
use Illuminate\Http\Request;
10
use Lenius\Basket\Item;
11
use Lenius\Basket\ItemInterface;
12
use Lenius\LaravelEcommerce\Facades\Cart;
13
14
class EcommerceController extends Controller
15
{
16 4
    public static function routes(): void
17
    {
18 4
        $router = app()->make('router');
19
20 4
        $router->get('cart/show', [self::class, 'index'])->name('ecommerce.cart.show');
21 4
        $router->get('cart/debug', [self::class, 'debug'])->name('ecommerce.cart.debug');
22 4
        $router->get('cart/demo', [self::class, 'demo'])->name('ecommerce.cart.demo');
23 4
        $router->get('cart/destroy', [self::class, 'destroy'])->name('ecommerce.cart.destroy');
24 4
        $router->post('cart/update', [self::class, 'update'])->name('ecommerce.cart.update');
25
26 4
        $router->any('cart/{id}/add', [self::class, 'add'])->name('ecommerce.cart.item.add');
27 4
        $router->get('cart/{id}/dec', [self::class, 'dec'])->name('ecommerce.cart.item.dec');
28 4
        $router->get('cart/{id}/inc', [self::class, 'inc'])->name('ecommerce.cart.item.inc');
29 4
        $router->get('cart/{id}/remove', [self::class, 'remove'])->name('ecommerce.cart.item.remove');
30
    }
31
32
    public function index(): Factory|View|Application
33
    {
34
        return view('ecommerce::cart');
35
    }
36
37 1
    public function add(Request $request, string $id): RedirectResponse
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

37
    public function add(Request $request, /** @scrutinizer ignore-unused */ string $id): RedirectResponse

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

37
    public function add(/** @scrutinizer ignore-unused */ Request $request, string $id): RedirectResponse

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