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

AdminAjaxController::ajaxAction()   F

Complexity

Conditions 36
Paths 26

Size

Total Lines 134
Code Lines 106

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 106
dl 0
loc 134
rs 3.3333
c 0
b 0
f 0
cc 36
nc 26
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\ReleaseComment;
7
use App\Models\Sharing;
8
use App\Models\SharingSite;
9
use App\Models\UsenetGroup;
10
use Blacklight\Binaries;
11
use Blacklight\Regexes;
12
use Illuminate\Http\Request;
13
14
class AdminAjaxController extends BasePageController
15
{
16
    /**
17
     * @param \Illuminate\Http\Request $request
18
     *
19
     * @throws \Throwable
20
     */
21
    public function ajaxAction(Request $request)
22
    {
23
        if ($request->missing('action')) {
24
            exit();
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
25
        }
26
27
        $settings = ['Settings' => $this->settings];
28
        switch ($request->input('action')) {
29
            case 'binary_blacklist_delete':
30
                $id = (int) $request->input('row_id');
31
                (new Binaries($settings))->deleteBlacklist($id);
32
                echo "Blacklist $id deleted.";
33
                break;
34
35
            case 'category_regex_delete':
36
                $id = (int) $request->input('row_id');
37
                (new Regexes(['Settings' => $this->settings, 'Table_Name' => 'category_regexes']))->deleteRegex($id);
38
                echo "Regex $id deleted.";
39
                break;
40
41
            case 'collection_regex_delete':
42
                $id = (int) $request->input('row_id');
43
                (new Regexes(['Settings' => $this->settings, 'Table_Name' => 'collection_regexes']))->deleteRegex($id);
44
                echo "Regex $id deleted.";
45
                break;
46
47
            case 'release_naming_regex_delete':
48
                $id = (int) $request->input('row_id');
49
                (new Regexes(['Settings' => $this->settings, 'Table_Name' => 'release_naming_regexes']))->deleteRegex($id);
50
                echo "Regex $id deleted.";
51
                break;
52
53
            case 'group_edit_purge_all':
54
                UsenetGroup::purge();
55
                echo 'All groups purged.';
56
                break;
57
58
            case 'group_edit_reset_all':
59
                UsenetGroup::resetall();
60
                echo 'All groups reset.';
61
                break;
62
63
            case 'group_edit_purge_single':
64
                $id = (int) $request->input('group_id');
65
                UsenetGroup::purge($id);
66
                echo "Group $id purged.";
67
                break;
68
69
            case 'group_edit_reset_single':
70
                $id = (int) $request->input('group_id');
71
                UsenetGroup::reset($id);
72
                echo "Group $id reset.";
73
                break;
74
75
            case 'group_edit_delete_single':
76
                $id = (int) $request->input('group_id');
77
                UsenetGroup::deleteGroup($id);
78
                echo "Group $id deleted.";
79
                break;
80
81
            case 'toggle_group_active_status':
82
                print UsenetGroup::updateGroupStatus((int) $request->input('group_id'), 'active', ($request->has('group_status') ? (int) $request->input('group_status') : 0));
83
                break;
84
85
            case 'toggle_group_backfill_status':
86
                print UsenetGroup::updateGroupStatus(
87
                    (int) $request->input('group_id'),
88
                    'backfill',
89
                    ($request->has('backfill_status') ? (int) $request->input('backfill_status') : 0)
90
                );
91
                break;
92
93
            case 'sharing_toggle_status':
94
                SharingSite::query()->where('id', $request->input('site_id'))->update(['enabled' => $request->input('site_status')]);
95
                echo($request->input('site_status') === 1 ? 'Activated' : 'Deactivated').' site '.$request->input('site_id');
96
                break;
97
98
            case 'sharing_toggle_enabled':
99
                Sharing::query()->update(['enabled' => $request->input('enabled_status')]);
100
                echo($request->input('enabled_status') === 1 ? 'Enabled' : 'Disabled').' sharing!';
101
                break;
102
103
            case 'sharing_start_position':
104
                Sharing::query()->update(['start_position' => $request->input('start_position')]);
105
                echo($request->input('start_position') === 1 ? 'Enabled' : 'Disabled').' fetching from start of group!';
106
                break;
107
108
            case 'sharing_reset_settings':
109
                $guid = Sharing::query()->first(['site_guid']);
110
                $guid = ($guid === null ? '' : $guid['site_guid']);
111
                (new \Blacklight\Sharing(['Settings' => $this->settings]))->initSettings($guid);
0 ignored issues
show
Bug introduced by
It seems like $guid can also be of type Illuminate\Database\Eloq...uent\Relations\Relation and Illuminate\Database\Eloquent\Relations\Relation; however, parameter $siteGuid of Blacklight\Sharing::initSettings() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

111
                (new \Blacklight\Sharing(['Settings' => $this->settings]))->initSettings(/** @scrutinizer ignore-type */ $guid);
Loading history...
112
                echo 'Re-initiated sharing settings!';
113
                break;
114
115
            case 'sharing_purge_site':
116
                $guid = SharingSite::query()->where('id', $request->input('purge_site'))->first(['site_guid']);
117
                if ($guid === null) {
118
                    echo 'Error purging site '.$request->input('purge_site').'!';
119
                } else {
120
                    $ids = ReleaseComment::query()->where('siteid', $guid['site_guid'])->get(['id']);
121
                    $total = $ids->count();
122
                    if ($total > 0) {
123
                        foreach ($ids as $id) {
124
                            ReleaseComment::deleteComment($id['id']);
125
                        }
126
                    }
127
                    SharingSite::query()->where('id', $request->input('purge_site'))->update(['comments' => 0]);
128
                    echo 'Deleted '.$total.' comments for site '.$request->input('purge_site');
129
                }
130
                break;
131
132
            case 'sharing_toggle_posting':
133
                Sharing::query()->update(['posting' => $request->input('posting_status')]);
134
                echo($request->input('posting_status') === 1 ? 'Enabled' : 'Disabled').' posting!';
135
                break;
136
137
            case 'sharing_toggle_fetching':
138
                Sharing::query()->update(['fetching' => $request->input('fetching_status')]);
139
                echo($request->input('fetching_status') === 1 ? 'Enabled' : 'Disabled').' fetching!';
140
                break;
141
142
            case 'sharing_toggle_site_auto_enabling':
143
                Sharing::query()->update(['auto_enable' => $request->input('auto_status')]);
144
                echo($request->input('auto_status') === 1 ? 'Enabled' : 'Disabled').' automatic site enabling!';
145
                break;
146
147
            case 'sharing_toggle_hide_users':
148
                Sharing::query()->update(['hide_users' => $request->input('hide_status')]);
149
                echo($request->input('hide_status') === 1 ? 'Enabled' : 'Disabled').' hiding of user names!';
150
                break;
151
152
            case 'sharing_toggle_all_sites':
153
                SharingSite::query()->update(['enabled' => $request->input('toggle_all')]);
154
                break;
155
        }
156
    }
157
}
158