BrowseController::show()   F
last analyzed

Complexity

Conditions 26
Paths 13056

Size

Total Lines 103
Code Lines 69

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 69
dl 0
loc 103
rs 0
c 0
b 0
f 0
cc 26
nc 13056
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\Category;
6
use App\Models\RootCategory;
7
use App\Services\Releases\ReleaseBrowseService;
8
use Illuminate\Http\Request;
9
10
class BrowseController extends BasePageController
11
{
12
    private ReleaseBrowseService $releaseBrowseService;
13
14
    public function __construct(ReleaseBrowseService $releaseBrowseService)
15
    {
16
        parent::__construct();
17
        $this->releaseBrowseService = $releaseBrowseService;
18
    }
19
20
    /**
21
     * @throws \Exception
22
     */
23
    public function index(Request $request)
24
    {
25
        $ordering = $this->releaseBrowseService->getBrowseOrdering();
26
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
27
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
28
        $offset = ($page - 1) * config('nntmux.items_per_page');
29
30
        $rslt = $this->releaseBrowseService->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, -1);
31
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
32
33
        // Build order by URLs
34
        $orderByUrls = [];
35
        foreach ($ordering as $orderType) {
36
            $orderByUrls['orderby'.$orderType] = url('browse/All?ob='.$orderType);
37
        }
38
39
        $this->viewData = array_merge($this->viewData, [
40
            'category' => -1,
41
            'catname' => 'All',
42
            'results' => $results,
43
            'lastvisit' => $this->userdata->lastlogin,
44
            'meta_title' => 'Browse All Releases',
45
            'meta_keywords' => 'browse,nzb,description,details',
46
            'meta_description' => 'Browse for Nzbs',
47
        ], $orderByUrls);
48
49
        return view('browse.index', $this->viewData);
50
    }
51
52
    /**
53
     * @throws \Exception
54
     */
55
    public function show(Request $request, string $parentCategory, string $id = 'All')
56
    {
57
58
        $parentId = RootCategory::query()->where('title', $parentCategory)->value('id');
59
60
        $query = Category::query()->where('title', $id)->where('root_categories_id', $parentId);
61
        if ($id !== 'All') {
62
            $cat = $query->first();
63
            $category = $cat !== null ? $cat->id : -1;
64
        } else {
65
            $category = $parentId ?? -1;
66
        }
67
68
        $grp = -1;
69
70
        $catarray = [];
71
        $catarray[] = $category;
72
73
        $ordering = $this->releaseBrowseService->getBrowseOrdering();
74
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
75
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
76
        $offset = ($page - 1) * config('nntmux.items_per_page');
77
78
        $rslt = $this->releaseBrowseService->getBrowseRange($page, $catarray, $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, $grp);
79
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
80
81
        $covgroup = '';
82
        $shows = false;
83
        if ($category === -1 && $grp === -1) {
84
            $catname = 'All';
85
        } elseif ($category !== -1 && $grp === -1) {
86
            $catname = $id;
87
88
            // Determine the root category ID - either from the category's root_categories_id
89
            // or the category itself if it IS a root category
90
            $rootCategoryId = null;
91
            $cdata = Category::find($category);
92
            if ($cdata !== null) {
93
                $rootCategoryId = $cdata->root_categories_id ?? $category;
94
            } else {
95
                // Category not found in categories table, might be a root category
96
                // Check if it matches a known root category ID
97
                $rootCategoryId = $category;
98
            }
99
100
            // Also check RootCategory table for parent categories (when $id is 'All')
101
            if ($id === 'All' && $parentId !== null) {
102
                $rootCategoryId = $parentId;
103
            }
104
105
            // Set covgroup based on root category
106
            if ($rootCategoryId === Category::GAME_ROOT) {
107
                $covgroup = 'console';
108
            } elseif ($rootCategoryId === Category::MOVIE_ROOT) {
109
                $covgroup = 'movies';
110
            } elseif ($rootCategoryId === Category::PC_ROOT) {
111
                $covgroup = 'games';
112
            } elseif ($rootCategoryId === Category::MUSIC_ROOT) {
113
                $covgroup = 'music';
114
            } elseif ($rootCategoryId === Category::BOOKS_ROOT) {
115
                $covgroup = 'books';
116
            } elseif ($rootCategoryId === Category::XXX_ROOT) {
117
                $covgroup = 'xxx';
118
            } elseif ($rootCategoryId === Category::TV_ROOT) {
119
                $shows = true;
120
            }
121
        } else {
122
            $catname = $grp;
123
        }
124
125
        // Build order by URLs
126
        $orderByUrls = [];
127
        if ($id === 'All' && $parentCategory === 'All') {
128
            $meta_title = 'Browse '.$parentCategory.' releases';
129
            foreach ($ordering as $orderType) {
130
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'?ob='.$orderType);
131
            }
132
        } else {
133
            $meta_title = 'Browse '.$parentCategory.' / '.$id.' releases';
134
            foreach ($ordering as $orderType) {
135
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'/'.$id.'?ob='.$orderType);
136
            }
137
        }
138
139
        $viewData = [
140
            'parentcat' => ucfirst($parentCategory),
141
            'category' => $category,
142
            'catname' => $catname,
143
            'results' => $results,
144
            'lastvisit' => $this->userdata->lastlogin,
145
            'covgroup' => $covgroup,
146
            'meta_title' => $meta_title,
147
            'meta_keywords' => 'browse,nzb,description,details',
148
            'meta_description' => 'Browse for Nzbs',
149
        ];
150
151
        if ($shows) {
152
            $viewData['shows'] = true;
153
        }
154
155
        $this->viewData = array_merge($this->viewData, $viewData, $orderByUrls);
156
157
        return view('browse.index', $this->viewData);
158
    }
159
160
    /**
161
     * @throws \Exception
162
     */
163
    public function group(Request $request)
164
    {
165
        if ($request->has('g')) {
166
            $group = $request->input('g');
167
            $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
168
            $offset = ($page - 1) * config('nntmux.items_per_page');
169
            $rslt = $this->releaseBrowseService->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), '', -1, $this->userdata->categoryexclusions, $group);
170
            $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
171
172
            $this->viewData = array_merge($this->viewData, [
173
                'results' => $results,
174
                'parentcat' => $group,
175
                'catname' => 'all',
176
                'lastvisit' => $this->userdata->lastlogin,
177
                'meta_title' => 'Browse Groups',
178
                'meta_keywords' => 'browse,nzb,description,details',
179
                'meta_description' => 'Browse Groups',
180
            ]);
181
182
            return view('browse.index', $this->viewData);
183
        }
184
185
        return redirect()->back()->with('error', 'Group parameter is required');
186
    }
187
}
188