Passed
Push — master ( 4f5b3b...1c5d75 )
by Darko
09:48
created

AdminSiteController::edit()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 116
Code Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 76
c 0
b 0
f 0
dl 0
loc 116
rs 7.5903
cc 7
nc 18
nop 1

How to fix   Long Method   

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\GrabStat;
8
use App\Models\ReleaseStat;
9
use App\Models\RoleStat;
10
use App\Models\Settings;
11
use App\Models\SignupStat;
12
use Illuminate\Http\Request;
13
14
class AdminSiteController extends BasePageController
15
{
16
    /**
17
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
18
     *
19
     * @throws \Exception
20
     */
21
    public function edit(Request $request)
22
    {
23
24
        $meta_title = $title = 'Site Edit';
25
        $error = '';
26
27
        // set the current action
28
        $action = $request->input('action') ?? 'view';
29
30
        switch ($action) {
31
            case 'submit':
32
                if ($request->missing('book_reqids')) {
33
                    $request->merge(['book_reqids' => []]);
34
                }
35
                Settings::settingsUpdate($request->all());
36
37
                return redirect()->to('admin/site-edit')->with('success', 'Settings updated successfully');
38
39
            case 'view':
40
            default:
41
                break;
42
        }
43
44
        // return a list of audiobooks, mags, ebooks, technical and foreign books
45
        $result = Category::query()->whereIn('id', [Category::MUSIC_AUDIOBOOK, Category::BOOKS_MAGAZINES, Category::BOOKS_TECHNICAL, Category::BOOKS_FOREIGN])->get(['id', 'title']);
46
47
        // setup the display lists for these categories
48
        $book_reqids_ids = [];
49
        $book_reqids_names = [];
50
        foreach ($result as $bookcategory) {
51
            $book_reqids_ids[] = $bookcategory['id'];
52
            $book_reqids_names[] = $bookcategory['title'];
53
        }
54
55
        // convert from a string array to an int array as we want to use int
56
        $book_reqids_ids = array_map(fn ($value) => (int) $value, $book_reqids_ids);
57
58
        // convert from a list to an array as we need to use an array, but the Settings table only saves strings
59
        $bookReqidsValue = Settings::settingValue('book_reqids') ?? '';
60
        $books_selected = $bookReqidsValue !== '' ? explode(',', $bookReqidsValue) : [];
61
62
        // convert from a string array to an int array, filtering out empty values
63
        $books_selected = array_map(fn ($value) => (int) trim($value), array_filter($books_selected));
64
65
        $compress_headers_warning = ! str_contains(config('settings.nntp_server'), 'astra') ? 'compress_headers_warning' : '';
66
67
        $this->viewData = array_merge($this->viewData, [
68
            'error' => $error,
69
            'yesno' => [
70
                'ids' => [1, 0],
71
                'names' => ['Yes', 'No'],
72
            ],
73
            'passwd' => [
74
                'ids' => [1, 0],
75
                'names' => ['Deep (requires unrar)', 'None'],
76
            ],
77
            'langlist' => [
78
                'ids' => [0, 2, 3, 1],
79
                'names' => ['English', 'Danish', 'French', 'German'],
80
            ],
81
            'imdblang' => [
82
                'ids' => ['en', 'da', 'nl', 'fi', 'fr', 'de', 'it', 'tlh', 'no', 'po', 'ru', 'es', 'sv'],
83
                'names' => ['English', 'Danish', 'Dutch', 'Finnish', 'French', 'German', 'Italian', 'Klingon', 'Norwegian', 'Polish', 'Russian', 'Spanish', 'Swedish'],
84
            ],
85
            'newgroupscan_names' => ['Days', 'Posts'],
86
            'registerstatus' => [
87
                'ids' => [Settings::REGISTER_STATUS_OPEN, Settings::REGISTER_STATUS_INVITE, Settings::REGISTER_STATUS_CLOSED],
88
                'names' => ['Open', 'Invite', 'Closed'],
89
            ],
90
            'passworded' => [
91
                'ids' => [0, 1],
92
                'names' => ['Hide passworded', 'Show everything'],
93
            ],
94
            'lookuplanguage' => [
95
                'iso' => ['en', 'de', 'es', 'fr', 'it', 'nl', 'pt', 'sv'],
96
                'names' => ['English', 'Deutsch', 'Español', 'Français', 'Italiano', 'Nederlands', 'Português', 'Svenska'],
97
            ],
98
            'imdb_urls' => [
99
                'ids' => [0, 1],
100
                'names' => ['imdb.com', 'akas.imdb.com'],
101
            ],
102
            'lookupbooks' => [
103
                'ids' => [0, 1, 2],
104
                'names' => ['Disabled', 'Lookup All Books', 'Lookup Renamed Books'],
105
            ],
106
            'lookupgames' => [
107
                'ids' => [0, 1, 2],
108
                'names' => ['Disabled', 'Lookup All Consoles', 'Lookup Renamed Consoles'],
109
            ],
110
            'lookupmusic' => [
111
                'ids' => [0, 1, 2],
112
                'names' => ['Disabled', 'Lookup All Music', 'Lookup Renamed Music'],
113
            ],
114
            'lookupmovies' => [
115
                'ids' => [0, 1, 2],
116
                'names' => ['Disabled', 'Lookup All Movies', 'Lookup Renamed Movies'],
117
            ],
118
            'lookuptv' => [
119
                'ids' => [0, 1, 2],
120
                'names' => ['Disabled', 'Lookup All TV', 'Lookup Renamed TV'],
121
            ],
122
            'lookup_reqids' => [
123
                'ids' => [0, 1, 2],
124
                'names' => ['Disabled', 'Lookup Request IDs', 'Lookup Request IDs Threaded'],
125
            ],
126
            'book_reqids' => [
127
                'ids' => $book_reqids_ids,
128
                'names' => $book_reqids_names,
129
                'selected' => $books_selected,
130
            ],
131
            'compress_headers_warning' => $compress_headers_warning,
132
            'title' => $title,
133
            'meta_title' => $meta_title,
134
        ]);
135
136
        return view('admin.site.edit', $this->viewData);
137
    }
138
139
    /**
140
     * @throws \Exception
141
     */
142
    public function stats()
143
    {
144
145
        $meta_title = $title = 'Site Stats';
146
147
        $topGrabs = GrabStat::getTopGrabbers();
148
        $recent = ReleaseStat::getRecentlyAdded();
149
        $usersByMonth = SignupStat::getUsersByMonth();
150
        $usersByRole = RoleStat::getUsersByRole();
151
152
        $this->viewData = array_merge($this->viewData, [
153
            'topgrabs' => $topGrabs,
154
            'recent' => $recent,
155
            'usersbymonth' => $usersByMonth,
156
            'usersbyrole' => $usersByRole,
157
            'totusers' => 0,
158
            'totrusers' => 0,
159
            'title' => $title,
160
            'meta_title' => $meta_title,
161
        ]);
162
163
        return view('admin.site.stats', $this->viewData);
164
    }
165
}
166