Passed
Push — master ( 26deea...6d666b )
by Darko
10:00
created

AdminReleasesController::destroy()   B

Complexity

Conditions 11
Paths 49

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 28
c 1
b 1
f 0
dl 0
loc 50
rs 7.3166
cc 11
nc 49
nop 1

How to fix   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 Blacklight\Releases;
9
use Illuminate\Http\Request;
10
11
class AdminReleasesController extends BasePageController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    public function index(Request $request)
17
    {
18
        $this->setAdminPrefs();
19
20
        $meta_title = $title = 'Release List';
21
22
        $page = $request->input('page', 1);
23
        $releaseList = Release::getReleasesRange($page);
24
25
        return view('admin.releases.index', [
26
            'releaselist' => $releaseList,
27
            'title' => $title,
28
            'meta_title' => $meta_title,
29
        ]);
30
    }
31
32
    /**
33
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|\Illuminate\View\View
34
     *
35
     * @throws \Exception
36
     */
37
    public function edit(Request $request)
38
    {
39
        // Set the current action.
40
        $action = ($request->input('action') ?? 'view');
41
42
        switch ($action) {
43
            case 'submit':
44
                Release::updateRelease(
45
                    $request->input('id'),
46
                    $request->input('name'),
47
                    $request->input('searchname'),
48
                    $request->input('fromname'),
49
                    $request->input('category'),
50
                    $request->input('totalpart'),
51
                    $request->input('grabs'),
52
                    $request->input('size'),
53
                    $request->input('postdate'),
54
                    $request->input('adddate'),
55
                    $request->input('videos_id'),
56
                    $request->input('tv_episodes_id'),
57
                    $request->input('imdbid'),
58
                    $request->input('anidbid')
59
                );
60
61
                $release = Release::getByGuid($request->input('guid'));
62
63
                return redirect('details/'.$release['guid'])->with('success', 'Release updated successfully');
64
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
65
66
            case 'view':
67
            default:
68
                $id = $request->input('id');
69
                $release = Release::getByGuid($id);
70
                break;
71
        }
72
73
        $yesno_ids = [1, 0];
74
        $yesno_names = ['Yes', 'No'];
75
        $catlist = Category::getForSelect(false);
76
77
        return view('admin.releases.edit', [
78
            'release' => $release,
79
            'yesno_ids' => $yesno_ids,
80
            'yesno_names' => $yesno_names,
81
            'catlist' => $catlist,
82
            'title' => 'Release Edit',
83
            'meta_title' => 'Release Edit',
84
        ]);
85
    }
86
87
    public function destroy($id)
88
    {
89
        try {
90
            if ($id) {
91
                $releases = new Releases;
92
                $releases->deleteMultiple($id);
93
94
                // Handle AJAX requests
95
                if (request()->wantsJson() || request()->ajax()) {
96
                    return response()->json([
97
                        'success' => true,
98
                        'message' => 'Release deleted successfully',
99
                    ]);
100
                }
101
102
                session()->flash('success', 'Release deleted successfully');
103
            }
104
105
            // Handle AJAX requests
106
            if (request()->wantsJson() || request()->ajax()) {
107
                return response()->json([
108
                    'success' => false,
109
                    'message' => 'No release ID provided',
110
                ], 400);
111
            }
112
113
            // Check if request is coming from the NZB details page
114
            $referer = request()->headers->get('referer');
115
            if ($referer && str_contains($referer, '/details/')) {
116
                // If coming from details page, redirect to home page
117
                return redirect()->route('All');
118
            }
119
120
            // Default redirection logic for other cases
121
            $redirectUrl = session('intended_redirect') ?? route('admin.release-list');
122
            session()->forget('intended_redirect');
123
124
            return redirect($redirectUrl);
125
        } catch (\Exception $e) {
126
            // Handle AJAX requests
127
            if (request()->wantsJson() || request()->ajax()) {
128
                return response()->json([
129
                    'success' => false,
130
                    'message' => 'Error deleting release: '.$e->getMessage(),
131
                ], 500);
132
            }
133
134
            session()->flash('error', 'Error deleting release: '.$e->getMessage());
135
136
            return redirect()->route('admin.release-list');
137
        }
138
    }
139
}
140