Passed
Push — dev ( c55b95...34fb50 )
by Darko
06:54
created

AdminSiteController::edit()   D

Complexity

Conditions 16
Paths 108

Size

Total Lines 158
Code Lines 101

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 101
dl 0
loc 158
rs 4.3999
c 0
b 0
f 0
cc 16
nc 108
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\Admin;
4
5
use App\Http\Controllers\BasePageController;
6
use App\Models\Category;
7
use App\Models\Release;
8
use App\Models\Settings;
9
use App\Models\User;
10
use Blacklight\SABnzbd;
11
use Blacklight\utility\Utility;
12
use Illuminate\Http\Request;
13
use Spatie\Permission\Models\Role;
14
15
class AdminSiteController extends BasePageController
16
{
17
    /**
18
     * @param \Illuminate\Http\Request $request
19
     *
20
     * @throws \Exception
21
     */
22
    public function edit(Request $request)
23
    {
24
        $this->setAdminPrefs();
25
26
        $meta_title = $title = 'Site Edit';
27
28
        // set the current action
29
        $action = $request->input('action') ?? 'view';
30
31
        switch ($action) {
32
            case 'submit':
33
                if ($request->missing('book_reqids')) {
34
                    $request->merge(['book_reqids' => []]);
35
                }
36
                $error = '';
37
                $ret = Settings::settingsUpdate($request->all());
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $ret is correct as App\Models\Settings::set...Update($request->all()) targeting App\Models\Settings::settingsUpdate() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
38
                if (\is_int($ret)) {
0 ignored issues
show
introduced by
The condition is_int($ret) is always false.
Loading history...
39
                    if ($ret === Settings::ERR_BADUNRARPATH) {
40
                        $error = 'The unrar path does not point to a valid binary';
41
                    } elseif ($ret === Settings::ERR_BADFFMPEGPATH) {
42
                        $error = 'The ffmpeg path does not point to a valid binary';
43
                    } elseif ($ret === Settings::ERR_BADMEDIAINFOPATH) {
44
                        $error = 'The mediainfo path does not point to a valid binary';
45
                    } elseif ($ret === Settings::ERR_BADNZBPATH) {
46
                        $error = 'The nzb path does not point to a valid directory';
47
                    } elseif ($ret === Settings::ERR_DEEPNOUNRAR) {
48
                        $error = 'Deep password check requires a valid path to unrar binary';
49
                    } elseif ($ret === Settings::ERR_BADTMPUNRARPATH) {
50
                        $error = 'The temp unrar path is not a valid directory';
51
                    } elseif ($ret === Settings::ERR_BADLAMEPATH) {
52
                        $error = 'The lame path is not a valid directory';
53
                    } elseif ($ret === Settings::ERR_SABCOMPLETEPATH) {
54
                        $error = 'The sab complete path is not a valid directory';
55
                    }
56
                }
57
58
                if ($error === '') {
0 ignored issues
show
introduced by
The condition $error === '' is always true.
Loading history...
59
                    $site = $ret;
0 ignored issues
show
Unused Code introduced by
The assignment to $site is dead and can be removed.
Loading history...
60
61
                    return redirect('admin/site-edit');
62
                }
63
64
                $this->smarty->assign('error', $error);
65
                $site = (object) $request->all();
66
                $this->smarty->assign('site', $site);
67
68
                break;
69
            case 'view':
70
            default:
71
                $site = $this->settings;
72
                $this->smarty->assign('site', $site);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
                $this->smarty->/** @scrutinizer ignore-call */ 
73
                               assign('site', $site);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
73
                $this->smarty->assign('settings', Settings::toTree());
74
                break;
75
        }
76
77
        $this->smarty->assign('yesno_ids', [1, 0]);
78
        $this->smarty->assign('yesno_names', ['Yes', 'No']);
79
80
        $this->smarty->assign('passwd_ids', [1, 0]);
81
        $this->smarty->assign('passwd_names', ['Deep (requires unrar)', 'None']);
82
83
        /*0 = English, 2 = Danish, 3 = French, 1 = German*/
84
        $this->smarty->assign('langlist_ids', [0, 2, 3, 1]);
85
        $this->smarty->assign('langlist_names', ['English', 'Danish', 'French', 'German']);
86
87
        $this->smarty->assign(
88
            'imdblang_ids',
89
            [
90
                'en', 'da', 'nl', 'fi', 'fr', 'de', 'it', 'tlh', 'no', 'po', 'ru', 'es',
91
                'sv',
92
            ]
93
        );
94
        $this->smarty->assign(
95
            'imdblang_names',
96
            [
97
                'English', 'Danish', 'Dutch', 'Finnish', 'French', 'German', 'Italian',
98
                'Klingon', 'Norwegian', 'Polish', 'Russian', 'Spanish', 'Swedish',
99
            ]
100
        );
101
102
        $this->smarty->assign('sabintegrationtype_ids', [SABnzbd::INTEGRATION_TYPE_USER, SABnzbd::INTEGRATION_TYPE_NONE]);
103
        $this->smarty->assign('sabintegrationtype_names', ['User', 'None (Off)']);
104
105
        $this->smarty->assign('newgroupscan_names', ['Days', 'Posts']);
106
107
        $this->smarty->assign('registerstatus_ids', [Settings::REGISTER_STATUS_OPEN, Settings::REGISTER_STATUS_INVITE, Settings::REGISTER_STATUS_CLOSED]);
108
        $this->smarty->assign('registerstatus_names', ['Open', 'Invite', 'Closed']);
109
110
        $this->smarty->assign('passworded_ids', [0, 1]);
111
        $this->smarty->assign('passworded_names', [
112
            'Hide passworded',
113
            'Show everything',
114
        ]);
115
116
        $this->smarty->assign('lookuplanguage_iso', ['en', 'de', 'es', 'fr', 'it', 'nl', 'pt', 'sv']);
117
        $this->smarty->assign('lookuplanguage_names', ['English', 'Deutsch', 'Español', 'Français', 'Italiano', 'Nederlands', 'Português', 'Svenska']);
118
119
        $this->smarty->assign('imdb_urls', [0, 1]);
120
        $this->smarty->assign('imdburl_names', ['imdb.com', 'akas.imdb.com']);
121
122
        $this->smarty->assign('lookupbooks_ids', [0, 1, 2]);
123
        $this->smarty->assign('lookupbooks_names', ['Disabled', 'Lookup All Books', 'Lookup Renamed Books']);
124
125
        $this->smarty->assign('lookupgames_ids', [0, 1, 2]);
126
        $this->smarty->assign('lookupgames_names', ['Disabled', 'Lookup All Consoles', 'Lookup Renamed Consoles']);
127
128
        $this->smarty->assign('lookupmusic_ids', [0, 1, 2]);
129
        $this->smarty->assign('lookupmusic_names', ['Disabled', 'Lookup All Music', 'Lookup Renamed Music']);
130
131
        $this->smarty->assign('lookupmovies_ids', [0, 1, 2]);
132
        $this->smarty->assign('lookupmovies_names', ['Disabled', 'Lookup All Movies', 'Lookup Renamed Movies']);
133
134
        $this->smarty->assign('lookuptv_ids', [0, 1, 2]);
135
        $this->smarty->assign('lookuptv_names', ['Disabled', 'Lookup All TV', 'Lookup Renamed TV']);
136
137
        $this->smarty->assign('lookup_reqids_ids', [0, 1, 2]);
138
        $this->smarty->assign('lookup_reqids_names', ['Disabled', 'Lookup Request IDs', 'Lookup Request IDs Threaded']);
139
140
        $this->smarty->assign('coversPath', NN_COVERS);
141
142
        // return a list of audiobooks, mags, ebooks, technical and foreign books
143
        $result = Category::query()->whereIn('id', [Category::MUSIC_AUDIOBOOK, Category::BOOKS_MAGAZINES, Category::BOOKS_TECHNICAL, Category::BOOKS_FOREIGN])->get(['id', 'title']);
144
145
        // setup the display lists for these categories, this could have been static, but then if names changed they would be wrong
146
        $book_reqids_ids = [];
147
        $book_reqids_names = [];
148
        foreach ($result as $bookcategory) {
149
            $book_reqids_ids[] = $bookcategory['id'];
150
            $book_reqids_names[] = $bookcategory['title'];
151
        }
152
153
        // convert from a string array to an int array as we want to use int
154
        $book_reqids_ids = array_map(function ($value) {
155
            return (int) $value;
156
        }, $book_reqids_ids);
157
        $this->smarty->assign('book_reqids_ids', $book_reqids_ids);
158
        $this->smarty->assign('book_reqids_names', $book_reqids_names);
159
160
        // convert from a list to an array as we need to use an array, but teh Settings table only saves strings
161
        $books_selected = explode(',', Settings::settingValue('..book_reqids'));
162
163
        // convert from a string array to an int array
164
        $books_selected = array_map(function ($value) {
165
            return (int) $value;
166
        }, $books_selected);
167
        $this->smarty->assign('book_reqids_selected', $books_selected);
168
169
        $this->smarty->assign('themelist', Utility::getThemesList());
170
171
        if (strpos(env('NNTP_SERVER'), 'astra') === false) {
172
            $this->smarty->assign('compress_headers_warning', 'compress_headers_warning');
173
        }
174
175
        $content = $this->smarty->fetch('site-edit.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

175
        /** @scrutinizer ignore-call */ 
176
        $content = $this->smarty->fetch('site-edit.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
177
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
178
179
        $this->adminrender();
180
    }
181
182
    /**
183
     * @throws \Exception
184
     */
185
    public function stats()
186
    {
187
        $this->setAdminPrefs();
188
189
        $meta_title = $title = 'Site Stats';
190
191
        $topgrabs = User::getTopGrabbers();
192
        $this->smarty->assign('topgrabs', $topgrabs);
193
194
        $topdownloads = Release::getTopDownloads();
195
        $this->smarty->assign('topdownloads', $topdownloads);
196
197
        $topcomments = Release::getTopComments();
198
        $this->smarty->assign('topcomments', $topcomments);
199
200
        $recent = Category::getRecentlyAdded();
201
        $this->smarty->assign('recent', $recent);
202
203
        $usersbymonth = User::getUsersByMonth();
204
        $this->smarty->assign('usersbymonth', $usersbymonth);
205
206
        $usersbyrole = Role::query()->select(['name'])->withCount('users')->groupBy('name')->having('users_count', '>', 0)->orderBy('users_count', 'desc')->get();
207
        $this->smarty->assign('usersbyrole', $usersbyrole);
208
        $this->smarty->assign('totusers', 0);
209
        $this->smarty->assign('totrusers', 0);
210
211
        $content = $this->smarty->fetch('site-stats.tpl');
212
213
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
214
215
        $this->adminrender();
216
    }
217
}
218