BrowseController::show()   F
last analyzed

Complexity

Conditions 23
Paths 3840

Size

Total Lines 86
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 86
rs 0
c 0
b 0
f 0
cc 23
nc 3840
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 Blacklight\Releases;
8
use Illuminate\Http\Request;
9
10
class BrowseController extends BasePageController
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function index(Request $request)
16
    {
17
        $releases = new Releases;
18
19
        $ordering = $releases->getBrowseOrdering();
20
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
21
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
22
        $offset = ($page - 1) * config('nntmux.items_per_page');
23
24
        $rslt = $releases->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, -1);
25
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
26
27
        // Build order by URLs
28
        $orderByUrls = [];
29
        foreach ($ordering as $orderType) {
30
            $orderByUrls['orderby'.$orderType] = url('browse/All?ob='.$orderType);
31
        }
32
33
        $this->viewData = array_merge($this->viewData, [
34
            'category' => -1,
35
            'catname' => 'All',
36
            'results' => $results,
37
            'lastvisit' => $this->userdata->lastlogin,
38
            'meta_title' => 'Browse All Releases',
39
            'meta_keywords' => 'browse,nzb,description,details',
40
            'meta_description' => 'Browse for Nzbs',
41
        ], $orderByUrls);
42
43
        return view('browse.index', $this->viewData);
44
    }
45
46
    /**
47
     * @throws \Exception
48
     */
49
    public function show(Request $request, string $parentCategory, string $id = 'All')
50
    {
51
        $releases = new Releases;
52
53
        $parentId = RootCategory::query()->where('title', $parentCategory)->value('id');
54
55
        $query = Category::query()->where('title', $id)->where('root_categories_id', $parentId);
56
        if ($id !== 'All') {
57
            $cat = $query->first();
58
            $category = $cat !== null ? $cat->id : -1;
59
        } else {
60
            $category = $parentId ?? -1;
61
        }
62
63
        $grp = -1;
64
65
        $catarray = [];
66
        $catarray[] = $category;
67
68
        $ordering = $releases->getBrowseOrdering();
69
        $orderBy = $request->has('ob') && ! empty($request->input('ob')) ? $request->input('ob') : '';
70
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
71
        $offset = ($page - 1) * config('nntmux.items_per_page');
72
73
        $rslt = $releases->getBrowseRange($page, $catarray, $offset, config('nntmux.items_per_page'), $orderBy, -1, $this->userdata->categoryexclusions, $grp);
74
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
75
76
        $covgroup = '';
77
        $shows = false;
78
        if ($category === -1 && $grp === -1) {
79
            $catname = 'All';
80
        } elseif ($category !== -1 && $grp === -1) {
81
            $catname = $id;
82
            $cdata = Category::find($category);
83
            if ($cdata !== null) {
84
                if ($cdata->root_categories_id === Category::GAME_ROOT) {
85
                    $covgroup = 'console';
86
                } elseif ($cdata->root_categories_id === Category::MOVIE_ROOT) {
87
                    $covgroup = 'movies';
88
                } elseif ($cdata->root_categories_id === Category::PC_ROOT) {
89
                    $covgroup = 'games';
90
                } elseif ($cdata->root_categories_id === Category::MUSIC_ROOT) {
91
                    $covgroup = 'music';
92
                } elseif ($cdata->root_categories_id === Category::BOOKS_ROOT) {
93
                    $covgroup = 'books';
94
                } elseif ($cdata->root_categories_id === Category::TV_ROOT) {
95
                    $shows = true;
96
                }
97
            }
98
        } else {
99
            $catname = $grp;
100
        }
101
102
        // Build order by URLs
103
        $orderByUrls = [];
104
        if ($id === 'All' && $parentCategory === 'All') {
105
            $meta_title = 'Browse '.$parentCategory.' releases';
106
            foreach ($ordering as $orderType) {
107
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'?ob='.$orderType);
108
            }
109
        } else {
110
            $meta_title = 'Browse '.$parentCategory.' / '.$id.' releases';
111
            foreach ($ordering as $orderType) {
112
                $orderByUrls['orderby'.$orderType] = url('browse/'.$parentCategory.'/'.$id.'?ob='.$orderType);
113
            }
114
        }
115
116
        $viewData = [
117
            'parentcat' => ucfirst($parentCategory),
118
            'category' => $category,
119
            'catname' => $catname,
120
            'results' => $results,
121
            'lastvisit' => $this->userdata->lastlogin,
122
            'covgroup' => $covgroup,
123
            'meta_title' => $meta_title,
124
            'meta_keywords' => 'browse,nzb,description,details',
125
            'meta_description' => 'Browse for Nzbs',
126
        ];
127
128
        if ($shows) {
129
            $viewData['shows'] = true;
130
        }
131
132
        $this->viewData = array_merge($this->viewData, $viewData, $orderByUrls);
133
134
        return view('browse.index', $this->viewData);
135
    }
136
137
    /**
138
     * @throws \Exception
139
     */
140
    public function group(Request $request)
141
    {
142
        $releases = new Releases;
143
        if ($request->has('g')) {
144
            $group = $request->input('g');
145
            $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
146
            $offset = ($page - 1) * config('nntmux.items_per_page');
147
            $rslt = $releases->getBrowseRange($page, [-1], $offset, config('nntmux.items_per_page'), '', -1, $this->userdata->categoryexclusions, $group);
148
            $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
149
150
            $this->viewData = array_merge($this->viewData, [
151
                'results' => $results,
152
                'parentcat' => $group,
153
                'catname' => 'all',
154
                'lastvisit' => $this->userdata->lastlogin,
155
                'meta_title' => 'Browse Groups',
156
                'meta_keywords' => 'browse,nzb,description,details',
157
                'meta_description' => 'Browse Groups',
158
            ]);
159
160
            return view('browse.index', $this->viewData);
161
        }
162
163
        return redirect()->back()->with('error', 'Group parameter is required');
164
    }
165
}
166