MyMoviesController::show()   F
last analyzed

Complexity

Conditions 43
Paths 466

Size

Total Lines 190
Code Lines 127

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 127
c 1
b 0
f 0
dl 0
loc 190
rs 0.5933
cc 43
nc 466
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\Settings;
7
use App\Models\UserMovie;
8
use Blacklight\Movie;
9
use Blacklight\Releases;
10
use Illuminate\Http\Request;
11
12
class MyMoviesController extends BasePageController
13
{
14
    public function show(Request $request)
15
    {
16
        $mv = new Movie;
17
18
        $action = $request->input('id') ?? '';
19
        $imdbid = $request->input('imdb') ?? '';
20
21
        if ($request->has('from')) {
22
            $this->viewData['from'] = url($request->input('from'));
23
        } else {
24
            $this->viewData['from'] = url('/mymovies');
25
        }
26
27
        switch ($action) {
28
            case 'delete':
29
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
30
                if (! $movie) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $movie of type App\Models\UserMovie[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
31
                    return redirect()->to('/mymovies');
32
                }
33
                UserMovie::delMovie($this->userdata->id, $imdbid);
34
                if ($request->has('from')) {
35
                    header('Location:'.url($request->input('from')));
0 ignored issues
show
Bug introduced by
Are you sure url($request->input('from')) of type Illuminate\Contracts\Routing\UrlGenerator|string can be used in concatenation? ( Ignorable by Annotation )

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

35
                    header('Location:'./** @scrutinizer ignore-type */ url($request->input('from')));
Loading history...
36
                } else {
37
                    return redirect()->to('/mymovies');
38
                }
39
40
                break;
41
            case 'add':
42
            case 'doadd':
43
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
44
                if ($movie) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $movie of type App\Models\UserMovie[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
45
                    return redirect()->to('/mymovies');
46
                }
47
48
                $movie = $mv->getMovieInfo($imdbid);
49
                if (! $movie) {
0 ignored issues
show
introduced by
$movie is of type App\Models\MovieInfo, thus it always evaluated to true.
Loading history...
50
                    return redirect()->to('/mymovies');
51
                }
52
53
                if ($action === 'doadd') {
54
                    $category = ($request->has('category') && \is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : [];
55
                    UserMovie::addMovie($this->userdata->id, $imdbid, $category);
56
                    if ($request->has('from')) {
57
                        return redirect()->to($request->input('from'));
58
                    }
59
60
                    return redirect()->to('/mymovies');
61
                }
62
63
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
64
                $categories = [];
65
                foreach ($tmpcats as $c) {
66
                    // If MOVIE WEB-DL categorization is disabled, don't include it as an option
67
                    if ((int) $c['id'] === Category::MOVIE_WEBDL && (int) Settings::settingValue('catwebdl') === 0) {
68
                        continue;
69
                    }
70
                    $categories[$c['id']] = $c['title'];
71
                }
72
                $this->viewData['type'] = 'add';
73
                $this->viewData['cat_ids'] = array_keys($categories);
74
                $this->viewData['cat_names'] = $categories;
75
                $this->viewData['cat_selected'] = [];
76
                $this->viewData['imdbid'] = $imdbid;
77
                $this->viewData['movie'] = $movie;
78
                $this->viewData['content'] = view('mymovies.add', $this->viewData)->render();
79
80
                return $this->pagerender();
81
82
            case 'edit':
83
            case 'doedit':
84
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
85
86
                if (! $movie) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $movie of type App\Models\UserMovie[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
87
                    return redirect()->to('/mymovies');
88
                }
89
90
                if ($action === 'doedit') {
91
                    $category = ($request->has('category') && \is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : [];
92
                    UserMovie::updateMovie($this->userdata->id, $imdbid, $category);
93
                    if ($request->has('from')) {
94
                        return redirect()->to($request->input('from'));
95
                    }
96
97
                    return redirect()->to('mymovies');
98
                }
99
100
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
101
                $categories = [];
102
                foreach ($tmpcats as $c) {
103
                    $categories[$c['id']] = $c['title'];
104
                }
105
106
                $this->viewData['type'] = 'edit';
107
                $this->viewData['cat_ids'] = array_keys($categories);
108
                $this->viewData['cat_names'] = $categories;
109
                $this->viewData['cat_selected'] = explode('|', $movie['categories']);
110
                $this->viewData['imdbid'] = $imdbid;
111
                $this->viewData['movie'] = $movie;
112
                $this->viewData['content'] = view('mymovies.add', $this->viewData)->render();
113
114
                return $this->pagerender();
115
116
            case 'browse':
117
118
                $title = 'Browse My Movies';
119
                $meta_title = 'My Movies';
120
                $meta_keywords = 'search,add,to,cart,nzb,description,details';
121
                $meta_description = 'Browse Your Movies';
122
123
                $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
124
125
                $offset = ($page - 1) * config('nntmux.items_per_cover_page');
126
127
                $movies = UserMovie::getMovies($this->userdata->id);
128
                $categories = $movie = [];
129
                foreach ($movies as $moviek => $movie) {
130
                    $showcats = explode('|', $movie['categories']);
131
                    if (\is_array($showcats) && \count($showcats) > 0) {
132
                        $catarr = [];
133
                        foreach ($showcats as $scat) {
134
                            if (! empty($scat)) {
135
                                $catarr[] = $categories[$scat];
136
                            }
137
                        }
138
                        $movie['categoryNames'] = implode(', ', $catarr);
139
                    } else {
140
                        $movie['categoryNames'] = '';
141
                    }
142
                }
143
144
                $ordering = (new Releases)->getBrowseOrdering();
145
146
                $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
147
148
                $results = $mv->getMovieRange($page, $movie['categoryNames'], $offset, config('nntmux.items_per_cover_page'), $ordering, -1, $this->userdata->categoryexclusions);
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User.
Loading history...
149
150
                $this->viewData['covgroup'] = '';
151
152
                foreach ($ordering as $ordertype) {
153
                    $this->viewData['orderby'.$ordertype] = url('/mymovies/browse?ob='.$ordertype.'&amp;offset=0');
154
                }
155
156
                $this->viewData['lastvisit'] = $this->userdata->lastlogin;
157
                $this->viewData['results'] = $results;
158
                $this->viewData['movies'] = true;
159
                $this->viewData['content'] = view('browse', $this->viewData)->render();
160
                $this->viewData = array_merge($this->viewData, compact('title', 'meta_title', 'meta_keywords', 'meta_description'));
161
162
                return $this->pagerender();
163
164
            default:
165
166
                $title = 'My Movies';
167
                $meta_title = 'My Movies';
168
                $meta_keywords = 'search,add,to,cart,nzb,description,details';
169
                $meta_description = 'Manage Your Movies';
170
171
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
172
                $categories = [];
173
                foreach ($tmpcats as $c) {
174
                    $categories[$c['id']] = $c['title'];
175
                }
176
177
                $movies = UserMovie::getMovies($this->userdata->id);
178
                $results = [];
179
                foreach ($movies as $moviek => $movie) {
180
                    $showcats = explode('|', $movie['categories']);
181
                    if (\is_array($showcats) && \count($showcats) > 0) {
182
                        $catarr = [];
183
                        foreach ($showcats as $scat) {
184
                            if (! empty($scat)) {
185
                                $catarr[] = $categories[$scat];
186
                            }
187
                        }
188
                        $movie['categoryNames'] = implode(', ', $catarr);
189
                    } else {
190
                        $movie['categoryNames'] = '';
191
                    }
192
193
                    $results[$moviek] = $movie;
194
                }
195
                $this->viewData['movies'] = $results;
196
                $this->viewData['content'] = view('mymovies.index', $this->viewData)->render();
197
                $this->viewData = array_merge($this->viewData, compact('title', 'meta_title', 'meta_keywords', 'meta_description'));
198
199
                return $this->pagerender();
200
        }
201
202
        // Fallback return in case no case matches
203
        return redirect()->to('/mymovies');
204
    }
205
}
206