Passed
Push — master ( 9f15d2...3a106b )
by Darko
10:30
created

AdminMovieController::edit()   D

Complexity

Conditions 18
Paths 109

Size

Total Lines 100
Code Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 62
c 0
b 0
f 0
dl 0
loc 100
rs 4.7916
cc 18
nc 109
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\MovieInfo;
7
use App\Models\Release;
8
use Blacklight\Movie;
9
use Illuminate\Http\Request;
10
11
class AdminMovieController extends BasePageController
12
{
13
    /**
14
     * @throws \Exception
15
     */
16
    public function index(Request $request)
17
    {
18
        $lastSearch = $request->input('moviesearch', '');
19
20
        if ($request->has('moviesearch')) {
21
            $movielist = MovieInfo::getAll($request->input('moviesearch'));
22
        } else {
23
            $movielist = MovieInfo::getAll();
24
        }
25
26
        $title = 'Movie List';
27
28
        return view('admin.movies.index', compact('title', 'movielist', 'lastSearch'));
29
    }
30
31
    /**
32
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
33
     *
34
     * @throws \Exception
35
     */
36
    public function create(Request $request)
37
    {
38
        if (! \defined('STDOUT')) {
39
            \define('STDOUT', fopen('php://stdout', 'wb'));
40
        }
41
42
        $title = 'Movie Add';
43
44
        // If no ID provided, show the add form
45
        if (! $request->has('id')) {
46
            return view('admin.movies.add', compact('title'));
47
        }
48
49
        // Validate the IMDB ID
50
        $id = $request->input('id');
51
52
        if (! is_numeric($id)) {
53
            return redirect()->back()
54
                ->with('error', 'Invalid IMDB ID. Please enter only numeric digits (without the "tt" prefix).')
55
                ->withInput();
56
        }
57
58
        // Check if movie already exists
59
        $movie = new Movie(['Settings' => null]);
0 ignored issues
show
Unused Code introduced by
The call to Blacklight\Movie::__construct() has too many arguments starting with array('Settings' => null). ( Ignorable by Annotation )

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

59
        $movie = /** @scrutinizer ignore-call */ new Movie(['Settings' => null]);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
60
        $movCheck = $movie->getMovieInfo($id);
61
62
        if ($movCheck !== null) {
63
            return redirect()->to('/admin/movie-edit?id='.$id)
64
                ->with('warning', 'Movie already exists in the database. Redirected to edit page.');
65
        }
66
67
        // Try to fetch and add the movie from TMDB
68
        try {
69
            $movieInfo = $movie->updateMovieInfo($id);
70
71
            if ($movieInfo) {
72
                // Link any existing releases to this movie
73
                $forUpdate = Release::query()->where('imdbid', $id)->get(['id']);
74
                if ($forUpdate !== null && $forUpdate->count() > 0) {
75
                    $movieInfoId = MovieInfo::query()->where('imdbid', $id)->first(['id']);
76
                    if ($movieInfoId !== null) {
77
                        foreach ($forUpdate as $rel) {
78
                            Release::query()->where('id', $rel->id)->update(['movieinfo_id' => $movieInfoId->id]);
79
                        }
80
                    }
81
                }
82
83
                return redirect()->to('/admin/movie-list')
84
                    ->with('success', 'Movie successfully added! IMDB ID: '.$id);
85
            } else {
86
                return redirect()->back()
87
                    ->with('error', 'Could not fetch movie information from TMDB/IMDB. Please verify the IMDB ID is correct.')
88
                    ->withInput();
89
            }
90
        } catch (\Exception $e) {
91
            return redirect()->back()
92
                ->with('error', 'Error adding movie: '.$e->getMessage())
93
                ->withInput();
94
        }
95
    }
96
97
    /**
98
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\View\View
99
     *
100
     * @throws \Exception
101
     */
102
    public function edit(Request $request)
103
    {
104
        $movie = new Movie;
105
        $title = 'Movie Edit';
106
107
        // Check if ID is provided
108
        if (! $request->has('id')) {
109
            return redirect()->to('admin/movie-list')
110
                ->with('error', 'No movie ID provided.');
111
        }
112
113
        $id = $request->input('id');
114
        $mov = $movie->getMovieInfo($id);
115
116
        if ($mov === null) {
117
            return redirect()->to('admin/movie-list')
118
                ->with('error', 'Movie with IMDB ID '.$id.' does not exist in database.');
119
        }
120
121
        // Handle update from TMDB
122
        if ($request->has('update') && (int) $request->input('update') === 1) {
123
            try {
124
                if (! \defined('STDOUT')) {
125
                    \define('STDOUT', fopen('php://stdout', 'wb'));
126
                }
127
128
                $movieInfo = $movie->updateMovieInfo($id);
129
130
                if ($movieInfo) {
131
                    return redirect()->back()
132
                        ->with('success', 'Movie information updated successfully from TMDB!');
133
                } else {
134
                    return redirect()->back()
135
                        ->with('error', 'Failed to update movie information from TMDB. Please try again.');
136
                }
137
            } catch (\Exception $e) {
138
                return redirect()->back()
139
                    ->with('error', 'Error updating movie: '.$e->getMessage());
140
            }
141
        }
142
143
        // Handle form submission
144
        $action = $request->input('action') ?? 'view';
145
146
        if ($action === 'submit') {
147
            try {
148
                $coverLoc = public_path('covers/movies/'.$id.'-cover.jpg');
149
                $backdropLoc = public_path('covers/movies/'.$id.'-backdrop.jpg');
150
151
                // Ensure directory exists
152
                if (! file_exists(public_path('covers/movies'))) {
153
                    mkdir(public_path('covers/movies'), 0755, true);
154
                }
155
156
                // Handle cover upload
157
                if ($request->hasFile('cover') && $request->file('cover')->isValid()) {
158
                    $coverFile = $request->file('cover');
159
                    $coverFile->move(public_path('covers/movies'), $id.'-cover.jpg');
160
                }
161
162
                // Handle backdrop upload
163
                if ($request->hasFile('backdrop') && $request->file('backdrop')->isValid()) {
164
                    $backdropFile = $request->file('backdrop');
165
                    $backdropFile->move(public_path('covers/movies'), $id.'-backdrop.jpg');
166
                }
167
168
                $request->merge(['cover' => file_exists($coverLoc) ? 1 : 0]);
169
                $request->merge(['backdrop' => file_exists($backdropLoc) ? 1 : 0]);
170
171
                $movie->update([
172
                    'actors' => $request->input('actors'),
173
                    'backdrop' => $request->input('backdrop'),
174
                    'cover' => $request->input('cover'),
175
                    'director' => $request->input('director'),
176
                    'genre' => $request->input('genre'),
177
                    'imdbid' => $id,
178
                    'language' => $request->input('language'),
179
                    'plot' => $request->input('plot'),
180
                    'rating' => $request->input('rating'),
181
                    'tagline' => $request->input('tagline'),
182
                    'title' => $request->input('title'),
183
                    'year' => $request->input('year'),
184
                ]);
185
186
                // Link releases to this movie
187
                $movieInfo = MovieInfo::query()->where('imdbid', $id)->first(['id']);
188
                if ($movieInfo !== null) {
189
                    Release::query()->where('imdbid', $id)->update(['movieinfo_id' => $movieInfo->id]);
190
                }
191
192
                return redirect()->to('admin/movie-list')
193
                    ->with('success', 'Movie updated successfully!');
194
            } catch (\Exception $e) {
195
                return redirect()->back()
196
                    ->with('error', 'Error saving movie: '.$e->getMessage())
197
                    ->withInput();
198
            }
199
        }
200
201
        return view('admin.movies.edit', compact('title', 'mov'))->with('movie', $mov);
202
    }
203
}
204