Passed
Push — master ( 39aa5c...787523 )
by Darko
08:11
created

CartController::store()   B

Complexity

Conditions 9
Paths 10

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 19
c 1
b 1
f 0
dl 0
loc 31
rs 8.0555
cc 9
nc 10
nop 1
1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Release;
6
use App\Models\UsersRelease;
7
use Illuminate\Http\RedirectResponse;
8
use Illuminate\Http\Request;
9
use Illuminate\Support\Facades\Auth;
10
11
class CartController extends BasePageController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    public function index(): void
17
    {
18
        $this->setPreferences();
19
        $meta_title = 'My Download Basket';
20
        $meta_keywords = 'search,add,to,cart,download,basket,nzb,description,details';
21
        $meta_description = 'Manage Your Download Basket';
22
23
        $results = UsersRelease::getCart(Auth::id());
24
        $this->smarty->assign('results', $results);
25
26
        $content = $this->smarty->fetch('cart.tpl');
27
        $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description'));
28
        $this->pagerender();
29
    }
30
31
    /**
32
     * @throws \Exception
33
     */
34
    public function store(Request $request): RedirectResponse|\Illuminate\Http\JsonResponse
35
    {
36
        $this->setPreferences();
37
        $guids = explode(',', $request->input('id'));
38
39
        $data = Release::query()->whereIn('guid', $guids)->select(['id'])->get();
40
41
        if ($data->isEmpty()) {
42
            return $request->ajax() || $request->wantsJson()
43
                ? response()->json(['success' => false, 'message' => 'No releases found'], 404)
44
                : redirect()->to('/cart/index');
45
        }
46
47
        $addedCount = 0;
48
        foreach ($data as $d) {
49
            $check = UsersRelease::whereReleasesId($d['id'])->first();
50
            if (empty($check)) {
51
                UsersRelease::addCart($this->userdata->id, $d['id']);
52
                $addedCount++;
53
            }
54
        }
55
56
        if ($request->ajax() || $request->wantsJson()) {
57
            return response()->json([
58
                'success' => true,
59
                'message' => $addedCount > 0 ? "{$addedCount} item(s) added to cart" : 'Items already in cart',
60
                'cartCount' => UsersRelease::where('users_id', $this->userdata->id)->count(),
61
            ]);
62
        }
63
64
        return redirect()->to('/cart/index');
65
    }
66
67
    /**
68
     * @throws \Exception
69
     */
70
    public function destroy(array|string $id): RedirectResponse
71
    {
72
        $this->setPreferences();
73
        $ids = null;
74
        if (! empty($id) && ! \is_array($id)) {
0 ignored issues
show
introduced by
The condition is_array($id) is always true.
Loading history...
75
            $ids = explode(',', $id);
76
        } elseif (\is_array($id)) {
77
            $ids = $id;
78
        }
79
80
        if (! empty($ids) && UsersRelease::delCartByGuid($ids, $this->userdata->id)) {
81
            return redirect()->to('/cart/index');
82
        }
83
84
        if (! $id) {
85
            return redirect()->to('/cart/index');
86
        }
87
88
        return redirect()->to('/cart/index');
89
    }
90
}
91