MyMoviesController   B
last analyzed

Complexity

Total Complexity 43

Size/Duplication

Total Lines 192
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 43
eloc 134
dl 0
loc 192
rs 8.96
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
F show() 0 190 43

How to fix   Complexity   

Complex Class

Complex classes like MyMoviesController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use MyMoviesController, and based on these observations, apply Extract Interface, too.

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
        $this->setPreferences();
17
        $mv = new Movie;
18
19
        $action = $request->input('id') ?? '';
20
        $imdbid = $request->input('imdb') ?? '';
21
22
        if ($request->has('from')) {
23
            $this->smarty->assign('from', url($request->input('from')));
24
        } else {
25
            $this->smarty->assign('from', url('/mymovies'));
26
        }
27
28
        switch ($action) {
29
            case 'delete':
30
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
31
                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...
32
                    return redirect()->to('/mymovies');
33
                }
34
                UserMovie::delMovie($this->userdata->id, $imdbid);
35
                if ($request->has('from')) {
36
                    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

36
                    header('Location:'./** @scrutinizer ignore-type */ url($request->input('from')));
Loading history...
37
                } else {
38
                    return redirect()->to('/mymovies');
39
                }
40
41
                break;
42
            case 'add':
43
            case 'doadd':
44
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
45
                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...
46
                    return redirect()->to('/mymovies');
47
                }
48
49
                $movie = $mv->getMovieInfo($imdbid);
50
                if (! $movie) {
0 ignored issues
show
introduced by
$movie is of type App\Models\MovieInfo, thus it always evaluated to true.
Loading history...
51
                    return redirect()->to('/mymovies');
52
                }
53
54
                if ($action === 'doadd') {
55
                    $category = ($request->has('category') && \is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : [];
56
                    UserMovie::addMovie($this->userdata->id, $imdbid, $category);
57
                    if ($request->has('from')) {
58
                        return redirect()->to($request->input('from'));
59
                    }
60
61
                    return redirect()->to('/mymovies');
62
                }
63
64
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
65
                $categories = [];
66
                foreach ($tmpcats as $c) {
67
                    // If MOVIE WEB-DL categorization is disabled, don't include it as an option
68
                    if ((int) $c['id'] === Category::MOVIE_WEBDL && (int) Settings::settingValue('catwebdl') === 0) {
69
                        continue;
70
                    }
71
                    $categories[$c['id']] = $c['title'];
72
                }
73
                $this->smarty->assign('type', 'add');
74
                $this->smarty->assign('cat_ids', array_keys($categories));
75
                $this->smarty->assign('cat_names', $categories);
76
                $this->smarty->assign('cat_selected', []);
77
                $this->smarty->assign('imdbid', $imdbid);
78
                $this->smarty->assign('movie', $movie);
79
                $content = $this->smarty->fetch('mymovies-add.tpl');
80
                $this->smarty->assign('content', $content);
81
                $this->pagerender();
82
                break;
83
            case 'edit':
84
            case 'doedit':
85
                $movie = UserMovie::getMovie($this->userdata->id, $imdbid);
86
87
                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...
88
                    return redirect()->to('/mymovies');
89
                }
90
91
                if ($action === 'doedit') {
92
                    $category = ($request->has('category') && \is_array($request->input('category')) && ! empty($request->input('category'))) ? $request->input('category') : [];
93
                    UserMovie::updateMovie($this->userdata->id, $imdbid, $category);
94
                    if ($request->has('from')) {
95
                        return redirect()->to($request->input('from'));
96
                    }
97
98
                    return redirect()->to('mymovies');
99
                }
100
101
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
102
                $categories = [];
103
                foreach ($tmpcats as $c) {
104
                    $categories[$c['id']] = $c['title'];
105
                }
106
107
                $this->smarty->assign('type', 'edit');
108
                $this->smarty->assign('cat_ids', array_keys($categories));
109
                $this->smarty->assign('cat_names', $categories);
110
                $this->smarty->assign('cat_selected', explode('|', $movie['categories']));
111
                $this->smarty->assign('imdbid', $imdbid);
112
                $this->smarty->assign('movie', $movie);
113
                $content = $this->smarty->fetch('mymovies-add.tpl');
114
                $this->smarty->assign('content', $content);
115
                $this->pagerender();
116
                break;
117
            case 'browse':
118
119
                $title = 'Browse My Movies';
120
                $meta_title = 'My Movies';
121
                $meta_keywords = 'search,add,to,cart,nzb,description,details';
122
                $meta_description = 'Browse Your Movies';
123
124
                $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
125
126
                $offset = ($page - 1) * config('nntmux.items_per_cover_page');
127
128
                $movies = UserMovie::getMovies($this->userdata->id);
129
                $categories = $movie = [];
130
                foreach ($movies as $moviek => $movie) {
131
                    $showcats = explode('|', $movie['categories']);
132
                    if (\is_array($showcats) && \count($showcats) > 0) {
133
                        $catarr = [];
134
                        foreach ($showcats as $scat) {
135
                            if (! empty($scat)) {
136
                                $catarr[] = $categories[$scat];
137
                            }
138
                        }
139
                        $movie['categoryNames'] = implode(', ', $catarr);
140
                    } else {
141
                        $movie['categoryNames'] = '';
142
                    }
143
                }
144
145
                $ordering = (new Releases)->getBrowseOrdering();
146
147
                $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
148
149
                $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...
150
151
                $this->smarty->assign('covgroup', '');
152
153
                foreach ($ordering as $ordertype) {
154
                    $this->smarty->assign('orderby'.$ordertype, url('/mymovies/browse?ob='.$ordertype.'&amp;offset=0'));
155
                }
156
157
                $this->smarty->assign('lastvisit', $this->userdata->lastlogin);
158
159
                $this->smarty->assign('results', $results);
160
161
                $this->smarty->assign('movies', true);
162
163
                $content = $this->smarty->fetch('browse.tpl');
164
                $this->smarty->assign(compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description'));
165
                $this->pagerender();
166
                break;
167
            default:
168
169
                $title = 'My Movies';
170
                $meta_title = 'My Movies';
171
                $meta_keywords = 'search,add,to,cart,nzb,description,details';
172
                $meta_description = 'Manage Your Movies';
173
174
                $tmpcats = Category::getChildren(Category::MOVIE_ROOT);
175
                $categories = [];
176
                foreach ($tmpcats as $c) {
177
                    $categories[$c['id']] = $c['title'];
178
                }
179
180
                $movies = UserMovie::getMovies($this->userdata->id);
181
                $results = [];
182
                foreach ($movies as $moviek => $movie) {
183
                    $showcats = explode('|', $movie['categories']);
184
                    if (\is_array($showcats) && \count($showcats) > 0) {
185
                        $catarr = [];
186
                        foreach ($showcats as $scat) {
187
                            if (! empty($scat)) {
188
                                $catarr[] = $categories[$scat];
189
                            }
190
                        }
191
                        $movie['categoryNames'] = implode(', ', $catarr);
192
                    } else {
193
                        $movie['categoryNames'] = '';
194
                    }
195
196
                    $results[$moviek] = $movie;
197
                }
198
                $this->smarty->assign('movies', $results);
199
200
                $content = $this->smarty->fetch('mymovies.tpl');
201
                $this->smarty->assign(compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description'));
202
                $this->pagerender();
203
                break;
204
        }
205
    }
206
}
207