Passed
Push — dev ( 9d591d...c59ba0 )
by Darko
22:01
created

MovieController   A

Complexity

Total Complexity 40

Size/Duplication

Total Lines 199
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 40
eloc 119
c 0
b 0
f 0
dl 0
loc 199
rs 9.2

3 Methods

Rating   Name   Duplication   Size   Complexity  
B showMovie() 0 33 6
B showTrailer() 0 32 6
F showMovies() 0 110 28

How to fix   Complexity   

Complex Class

Complex classes like MovieController 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 MovieController, 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 Blacklight\Movie;
7
use Illuminate\Http\Request;
8
use Illuminate\Support\Arr;
9
10
class MovieController extends BasePageController
11
{
12
    /**
13
     * @param \Illuminate\Http\Request $request
14
     *
15
     * @return \Illuminate\Http\JsonResponse
16
     * @throws \Exception
17
     */
18
    public function showMovie(Request $request)
19
    {
20
        $this->setPrefs();
21
        if ($request->has('modal') && $request->has('id') && ctype_digit($request->input('id'))) {
22
            $mov = (new Movie(['Settings' => $this->settings]))->getMovieInfo($request->input('id'));
23
24
            if (! $mov) {
0 ignored issues
show
introduced by
$mov is of type App\Models\MovieInfo, thus it always evaluated to true.
Loading history...
25
                return response()->json(['message' => 'No movie with imdbid: '.$request->input('id').' found'], 404);
26
            }
27
28
            $mov['actors'] = makeFieldLinks($mov, 'actors', 'movies');
29
            $mov['genre'] = makeFieldLinks($mov, 'genre', 'movies');
30
            $mov['director'] = makeFieldLinks($mov, 'director', 'movies');
31
32
            $this->smarty->assign(['movie' => $mov, 'modal' => true]);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

32
            $this->smarty->/** @scrutinizer ignore-call */ 
33
                           assign(['movie' => $mov, 'modal' => true]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
33
34
            $title = 'Info for '.$mov['title'];
35
            $meta_title = '';
36
            $meta_keywords = '';
37
            $meta_description = '';
38
            $this->smarty->registerPlugin('modifier', 'ss', 'stripslashes');
0 ignored issues
show
Bug introduced by
The method registerPlugin() does not exist on Illuminate\Foundation\Application. Did you maybe mean register()? ( Ignorable by Annotation )

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

38
            $this->smarty->/** @scrutinizer ignore-call */ 
39
                           registerPlugin('modifier', 'ss', 'stripslashes');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
39
40
            if ($request->has('modal')) {
41
                $content = $this->smarty->fetch('viewmovie.tpl');
0 ignored issues
show
Bug introduced by
The method fetch() does not exist on Illuminate\Foundation\Application. ( Ignorable by Annotation )

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

41
                /** @scrutinizer ignore-call */ 
42
                $content = $this->smarty->fetch('viewmovie.tpl');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
42
                $this->smarty->assign('modal', true);
43
                echo $this->content;
44
            } else {
45
                $content = $this->smarty->fetch('viewmoviefull.tpl');
46
            }
47
        }
48
49
        $this->smarty->assign(compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description'));
50
        $this->pagerender();
51
    }
52
53
    /**
54
     * @param \Illuminate\Http\Request $request
55
     * @param string                   $id
56
     *
57
     * @throws \Exception
58
     */
59
    public function showMovies(Request $request, $id = '')
60
    {
61
        $this->setPrefs();
62
        $movie = new Movie(['Settings' => $this->settings]);
63
64
        $moviecats = Category::getChildren(Category::MOVIE_ROOT);
65
        $mtmp = [];
66
        foreach ($moviecats as $mcat) {
67
            $mtmp[] =
68
                [
69
                    'id' => $mcat->id,
70
                    'title' => $mcat->title,
71
                ];
72
        }
73
74
        $category = $request->has('imdb') ? -1 : ($request->has('t') ? $request->input('t') : Category::MOVIE_ROOT);
75
        if ($id && \in_array($id, Arr::pluck($mtmp, 'title'), false)) {
76
            $cat = Category::query()
77
                ->where(['title'=> $id, 'root_categories_id' => Category::MOVIE_ROOT])
78
                ->first(['id']);
79
            $category = $cat !== null ? $cat['id'] : Category::MOVIE_ROOT;
80
        }
81
82
        $this->smarty->assign('cpapi', $this->userdata->cp_api);
83
        $this->smarty->assign('cpurl', $this->userdata->cp_url);
84
85
        $catarray = [];
86
        if ((int) $category !== -1) {
87
            $catarray[] = $category;
88
        }
89
90
        $this->smarty->assign('catlist', $mtmp);
91
        $this->smarty->assign('category', $category);
92
        $this->smarty->assign('categorytitle', $id);
93
94
        $page = $request->has('page') && is_numeric($request->input('page')) ? $request->input('page') : 1;
95
        $offset = ($page - 1) * config('nntmux.items_per_cover_page');
96
97
        $ordering = $movie->getMovieOrdering();
98
        $orderby = request()->has('ob') && \in_array(request()->input('ob'), $ordering, false) ? request()->input('ob') : '';
99
100
        $movies = [];
101
        $rslt = $movie->getMovieRange($page, $catarray, $offset, config('nntmux.items_per_cover_page'), $orderby, -1, $this->userdata->categoryexclusions);
0 ignored issues
show
Bug introduced by
The property categoryexclusions does not seem to exist on App\Models\User. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
102
        $results = $this->paginate($rslt ?? [], $rslt[0]->_totalcount ?? 0, config('nntmux.items_per_cover_page'), $page, $request->url(), $request->query());
103
104
        foreach ($results as $result) {
105
            $result->genre = makeFieldLinks($result, 'genre', 'movies');
106
            $result->actors = makeFieldLinks($result, 'actors', 'movies');
107
            $result->director = makeFieldLinks($result, 'director', 'movies');
108
            $result->languages = explode(', ', $result->language);
109
110
            $movies[] = $result;
111
        }
112
113
        $title = ($request->has('title') && ! empty($request->input('title'))) ? stripslashes($request->input('title')) : '';
114
        $this->smarty->assign('title', $title);
115
116
        $actors = ($request->has('actors') && ! empty($request->input('actors'))) ? stripslashes($request->input('actors')) : '';
117
        $this->smarty->assign('actors', $actors);
118
119
        $director = ($request->has('director') && ! empty($request->input('director'))) ? stripslashes($request->input('director')) : '';
120
        $this->smarty->assign('director', $director);
121
122
        $ratings = range(1, 9);
123
        $rating = ($request->has('rating') && \in_array($request->input('rating'), $ratings, false)) ? $request->input('rating') : '';
124
        $this->smarty->assign('ratings', $ratings);
125
        $this->smarty->assign('rating', $rating);
126
127
        $genres = $movie->getGenres();
128
        $genre = ($request->has('genre') && \in_array($request->input('genre'), $genres, false)) ? $request->input('genre') : '';
129
        $this->smarty->assign('genres', $genres);
130
        $this->smarty->assign('genre', $genre);
131
132
        $years = range(1903, now()->addYear()->year);
133
        rsort($years);
134
        $year = ($request->has('year') && \in_array($request->input('year'), $years, false)) ? $request->input('year') : '';
135
        $this->smarty->assign('years', $years);
136
        $this->smarty->assign('year', $year);
137
138
        if ((int) $category === -1) {
139
            $this->smarty->assign('catname', 'All');
140
        } else {
141
            $cdata = Category::find($category);
142
            if ($cdata !== null) {
143
                $this->smarty->assign('catname', $cdata);
144
            } else {
145
                $this->smarty->assign('catname', 'All');
146
            }
147
        }
148
149
        $this->smarty->assign(
150
            [
151
                'resultsadd'=>  $movies,
152
                'results' => $results,
153
                'covgroup' => 'movies',
154
            ]
155
        );
156
157
        $meta_title = 'Browse Movies';
158
        $meta_keywords = 'browse,nzb,description,details';
159
        $meta_description = 'Browse for Movies';
160
161
        if ($request->has('imdb')) {
162
            $content = $this->smarty->fetch('viewmoviefull.tpl');
163
        } else {
164
            $content = $this->smarty->fetch('movies.tpl');
165
        }
166
167
        $this->smarty->assign(compact('content', 'meta_title', 'meta_keywords', 'meta_description'));
168
        $this->pagerender();
169
    }
170
171
    /**
172
     * @param \Illuminate\Http\Request $request
173
     *
174
     * @return \Illuminate\Http\JsonResponse
175
     * @throws \Exception
176
     */
177
    public function showTrailer(Request $request)
178
    {
179
        $movie = new Movie;
180
181
        if ($request->has('id') && ctype_digit($request->input('id'))) {
182
            $mov = $movie->getMovieInfo($request->input('id'));
183
184
            if (! $mov) {
0 ignored issues
show
introduced by
$mov is of type App\Models\MovieInfo, thus it always evaluated to true.
Loading history...
185
                return response()->json(['message' => 'There is no trailer for this movie.'], 404);
186
            }
187
188
            $this->smarty->assign('movie', $mov);
189
190
            $title = 'Info for '.$mov['title'];
191
            $meta_title = '';
192
            $meta_keywords = '';
193
            $meta_description = '';
194
            $this->smarty->registerPlugin('modifier', 'ss', 'stripslashes');
195
196
            $modal = false;
197
            if ($request->has('modal')) {
198
                $modal = true;
199
                $this->smarty->assign('modal', true);
200
            }
201
202
            $content = $this->smarty->fetch('viewmovietrailer.tpl');
203
204
            if ($modal) {
205
                echo $content;
206
            } else {
207
                $this->smarty->assign(compact('content', 'title', 'meta_title', 'meta_keywords', 'meta_description'));
208
                $this->pagerender();
209
            }
210
        }
211
    }
212
}
213