SearchController::search()   F
last analyzed

Complexity

Conditions 33
Paths 3456

Size

Total Lines 170
Code Lines 113

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 113
dl 0
loc 170
rs 0
c 0
b 0
f 0
cc 33
nc 3456
nop 1

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\UsenetGroup;
7
use Blacklight\Releases;
8
use Illuminate\Http\Request;
9
10
class SearchController extends BasePageController
11
{
12
    /**
13
     * @throws \Exception
14
     */
15
    public function search(Request $request): void
16
    {
17
        $this->setPreferences();
18
        $releases = new Releases;
19
20
        $meta_title = 'Search Nzbs';
21
        $meta_keywords = 'search,nzb,description,details';
22
        $meta_description = 'Search for Nzbs';
23
24
        $results = [];
25
26
        $searchType = 'basic';
27
        if ($request->has('search_type') && $request->input('search_type') === 'adv') {
28
            $searchType = 'advanced';
29
        }
30
31
        $ordering = $releases->getBrowseOrdering();
32
        $orderBy = ($request->has('ob') && \in_array($request->input('ob'), $ordering, false) ? $request->input('ob') : '');
33
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
34
        $offset = ($page - 1) * config('nntmux.items_per_page');
35
36
        $this->smarty->assign(
37
            [
38
                'subject' => '', 'search' => '', 'category' => [0], 'covgroup' => '',
39
            ]
40
        );
41
42
        if ($searchType === 'basic' && $request->missing('searchadvr') && ($request->has('id') || $request->has('subject') || $request->has('search'))) {
43
            $searchString = [];
44
            switch (true) {
45
                case $request->filled('subject'):
46
                    $searchString['searchname'] = (string) $request->input('subject') ?? [];
47
                    $this->smarty->assign('subject', $searchString['searchname']);
48
                    break;
49
                case $request->filled('id'):
50
                    $searchString['searchname'] = (string) $request->input('id') ?? [];
51
                    $this->smarty->assign('id', $searchString['searchname']);
52
                    break;
53
                case $request->filled('search'):
54
                    $searchString['searchname'] = (string) $request->input('search') ?? [];
55
                    $this->smarty->assign('search', $searchString['searchname']);
56
                    break;
57
                default:
58
                    $searchString['searchname'] = '';
59
            }
60
61
            $categoryID[] = -1;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$categoryID was never initialized. Although not strictly required by PHP, it is generally a good practice to add $categoryID = array(); before regardless.
Loading history...
62
            if ($request->has('t')) {
63
                $categoryID = explode(',', $request->input('t'));
64
            }
65
            foreach ($releases->getBrowseOrdering() as $orderType) {
66
                $this->smarty->assign(
67
                    'orderby'.$orderType,
68
                    url('/search?search='.htmlentities($searchString['searchname'], ENT_QUOTES | ENT_HTML5).'&t='.implode(',', $categoryID).'&amp;ob='.$orderType));
69
            }
70
71
            $rslt = $releases->search(
72
                $searchString,
73
                -1,
74
                -1,
75
                -1,
76
                -1,
77
                -1,
78
                $offset,
79
                config('nntmux.items_per_page'),
80
                $orderBy,
81
                -1,
82
                $this->userdata->categoryexclusions ?? [],
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User.
Loading history...
83
                'basic',
84
                $categoryID);
85
86
            $results = $this->paginate($rslt ?? [], $rslt[0]->_totalrows ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
87
88
            $this->smarty->assign(
89
                [
90
                    'lastvisit' => $this->userdata->lastlogin,
91
                    'category' => $categoryID,
92
                ]
93
            );
94
        }
95
96
        $searchVars = [
97
            'searchadvr' => '',
98
            'searchadvsubject' => '',
99
            'searchadvposter' => '',
100
            'searchadvfilename' => '',
101
            'searchadvdaysnew' => '',
102
            'searchadvdaysold' => '',
103
            'searchadvgroups' => '',
104
            'searchadvcat' => '',
105
            'searchadvsizefrom' => '',
106
            'searchadvsizeto' => '',
107
            'searchadvhasnfo' => '',
108
            'searchadvhascomments' => '',
109
        ];
110
111
        foreach ($searchVars as $searchVarKey => $searchVar) {
112
            $searchVars[$searchVarKey] = ($request->has($searchVarKey) ? (string) $request->input($searchVarKey) : '');
113
        }
114
115
        $searchVars['selectedgroup'] = $searchVars['searchadvgroups'];
116
        $searchVars['selectedcat'] = $searchVars['searchadvcat'];
117
        $searchVars['selectedsizefrom'] = $searchVars['searchadvsizefrom'];
118
        $searchVars['selectedsizeto'] = $searchVars['searchadvsizeto'];
119
        foreach ($searchVars as $searchVarKey => $searchVar) {
120
            $this->smarty->assign($searchVarKey, $searchVars[$searchVarKey]);
121
        }
122
123
        if ($searchType !== 'basic' && $request->missing('id') && $request->missing('subject') && $request->anyFilled(['searchadvr', 'searchadvsubject', 'searchadvfilename', 'searchadvposter'])) {
124
            $orderByString = '';
125
            foreach ($searchVars as $searchVarKey => $searchVar) {
126
                $orderByString .= "&$searchVarKey=".htmlentities($searchVar, ENT_QUOTES | ENT_HTML5);
127
            }
128
            $orderByString = ltrim($orderByString, '&');
129
130
            foreach ($ordering as $orderType) {
131
                $this->smarty->assign(
132
                    'orderby'.$orderType,
133
                    url('/search?'.$orderByString.'&search_type=adv&ob='.$orderType
134
                    ));
135
            }
136
137
            $searchArr = [
138
                'searchname' => $searchVars['searchadvr'] === '' ? -1 : $searchVars['searchadvr'],
139
                'name' => $searchVars['searchadvsubject'] === '' ? -1 : $searchVars['searchadvsubject'],
140
                'fromname' => $searchVars['searchadvposter'] === '' ? -1 : $searchVars['searchadvposter'],
141
                'filename' => $searchVars['searchadvfilename'] === '' ? -1 : $searchVars['searchadvfilename'],
142
            ];
143
144
            $rslt = $releases->search(
145
                $searchArr,
146
                $searchVars['searchadvgroups'],
147
                $searchVars['searchadvsizefrom'],
148
                $searchVars['searchadvsizeto'],
149
                ($searchVars['searchadvdaysnew'] === '' ? -1 : $searchVars['searchadvdaysnew']),
150
                ($searchVars['searchadvdaysold'] === '' ? -1 : $searchVars['searchadvdaysold']),
151
                $offset,
152
                config('nntmux.items_per_page'),
153
                $orderBy,
154
                -1,
155
                $this->userdata->categoryexclusions ?? [],
156
                'advanced',
157
                [$searchVars['searchadvcat'] === '' ? -1 : $searchVars['searchadvcat']]
158
            );
159
160
            $results = $this->paginate($rslt ?? [], $rslt[0]->_totalrows ?? 0, config('nntmux.items_per_page'), $page, $request->url(), $request->query());
161
162
            $this->smarty->assign(
163
                [
164
                    'lastvisit' => $this->userdata->lastlogin,
165
                ]
166
            );
167
        }
168
169
        $this->smarty->assign(
170
            [
171
                'sizelist' => [
172
                    -1 => '--Select--', 1 => '100MB', 2 => '250MB', 3 => '500MB', 4 => '1GB', 5 => '2GB',
173
                    6 => '3GB', 7 => '4GB', 8 => '8GB', 9 => '16GB', 10 => '32GB', 11 => '64GB',
174
                ],
175
                'results' => $results,
176
                'sadvanced' => $searchType !== 'basic',
177
                'grouplist' => UsenetGroup::getGroupsForSelect(),
178
                'catlist' => Category::getForSelect(),
179
            ]
180
        );
181
182
        $content = $this->smarty->fetch('search.tpl');
183
        $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description'));
184
        $this->pagerender();
185
    }
186
}
187