Completed
Push — dev ( f54957...86f725 )
by Darko
07:34
created

MovieController::edit()   C

Complexity

Conditions 12
Paths 36

Size

Total Lines 77
Code Lines 51

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 51
dl 0
loc 77
rs 6.9666
c 0
b 0
f 0
cc 12
nc 36
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 Blacklight\Movie;
6
use App\Models\Release;
7
use App\Models\MovieInfo;
8
use Illuminate\Http\Request;
9
use Blacklight\utility\Utility;
10
use App\Http\Controllers\BasePageController;
11
12
class MovieController extends BasePageController
13
{
14
    /**
15
     * @throws \Exception
16
     */
17
    public function index()
18
    {
19
        $this->setAdminPrefs();
20
21
        $meta_title = $title = 'Movie List';
22
23
        $movieList = Utility::getRange('movieinfo');
24
        $this->smarty->assign('movielist', $movieList);
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

24
        $this->smarty->/** @scrutinizer ignore-call */ 
25
                       assign('movielist', $movieList);

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...
25
26
        $content = $this->smarty->fetch('movie-list.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

26
        /** @scrutinizer ignore-call */ 
27
        $content = $this->smarty->fetch('movie-list.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...
27
28
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
29
30
        $this->adminrender();
31
    }
32
33
    /**
34
     * @param \Illuminate\Http\Request $request
35
     *
36
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
37
     * @throws \Exception
38
     */
39
    public function create(Request $request)
40
    {
41
        if (! \defined('STDOUT')) {
42
            \define('STDOUT', fopen('php://stdout', 'wb'));
43
        }
44
        $this->setAdminPrefs();
45
        $movie = new Movie(['Settings' => null]);
46
47
        $meta_title = $title = 'Movie Add';
48
49
        $id = $request->input('id');
50
51
        if ($request->has('id') && is_numeric($request->input('id'))) {
52
            $movCheck = $movie->getMovieInfo($id);
53
            $movieInfo = $movie->updateMovieInfo($id);
54
            if ($movieInfo && ($movCheck === null || ($request->has('update') && (int) $request->input('update') === 1))) {
55
                $forUpdate = Release::query()->where('imdbid', $id)->get(['id']);
56
                if ($forUpdate !== null) {
57
                    $movieInfoId = MovieInfo::query()->where('imdbid', $id)->first(['id']);
58
                    if ($movieInfoId !== null) {
59
                        foreach ($forUpdate as $movie) {
60
                            Release::query()->where('id', $movie->id)->update(['movieinfo_id' => $movieInfoId->id]);
61
                        }
62
                    }
63
                }
64
                if (($request->has('update') && (int) $request->input('update') === 1)) {
65
                    return back()->withInput();
66
                }
67
68
                return redirect('/admin/movie-list');
69
            }
70
71
            return redirect('/admin/movie-list');
72
        }
73
74
        $content = $this->smarty->fetch('movie-add.tpl');
75
76
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
77
78
        $this->adminrender();
79
    }
80
81
    /**
82
     * @param \Illuminate\Http\Request $request
83
     *
84
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
85
     * @throws \Exception
86
     */
87
    public function edit(Request $request)
88
    {
89
        $this->setAdminPrefs();
90
91
        $movie = new Movie();
92
        $meta_title = $title = 'Add Movie';
93
94
        // set the current action
95
        $action = $request->input('action') ?? 'view';
96
97
        if ($request->has('id')) {
98
            $id = $request->input('id');
99
            $mov = $movie->getMovieInfo($id);
100
101
            if ($mov === null) {
102
                abort(404, 'Movie you requested does not exist in database');
103
            }
104
105
            switch ($action) {
106
                case 'submit':
107
                    $coverLoc = resource_path().'/covers/movies/'.$id.'-cover.jpg';
108
                    $backdropLoc = resource_path().'covers/movies/'.$id.'-backdrop.jpg';
109
110
                    if ($_FILES['cover']['size'] > 0) {
111
                        $tmpName = $_FILES['cover']['tmp_name'];
112
                        $file_info = getimagesize($tmpName);
113
                        if (! empty($file_info)) {
114
                            move_uploaded_file($_FILES['cover']['tmp_name'], $coverLoc);
115
                        }
116
                    }
117
118
                    if ($_FILES['backdrop']['size'] > 0) {
119
                        $tmpName = $_FILES['backdrop']['tmp_name'];
120
                        $file_info = getimagesize($tmpName);
121
                        if (! empty($file_info)) {
122
                            move_uploaded_file($_FILES['backdrop']['tmp_name'], $backdropLoc);
123
                        }
124
                    }
125
126
                    $request->merge(['cover' => file_exists($coverLoc) ? 1 : 0]);
127
                    $request->merge(['backdrop' => file_exists($backdropLoc) ? 1 : 0]);
128
129
                    $movie->update([
130
                        'actors'   => $request->input('actors'),
131
                        'backdrop' => $request->input('backdrop'),
132
                        'cover'    => $request->input('cover'),
133
                        'director' => $request->input('director'),
134
                        'genre'    => $request->input('genre'),
135
                        'imdbid'   => $id,
136
                        'language' => $request->input('language'),
137
                        'plot'     => $request->input('plot'),
138
                        'rating'   => $request->input('rating'),
139
                        'tagline'  => $request->input('tagline'),
140
                        'title'    => $request->input('title'),
141
                        'year'     => $request->input('year'),
142
                    ]);
143
144
                    $movieInfo = MovieInfo::query()->where('imdbid', $id)->first(['id']);
145
                    if ($movieInfo !== null) {
146
                        Release::query()->where('imdbid', $id)->update(['movieinfo_id' => $movieInfo->id]);
147
                    }
148
149
                    return redirect('admin/movie-list');
150
                    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...
151
                case 'view':
152
                default:
153
                    $meta_title = $title = 'Movie Edit';
154
                    $this->smarty->assign('movie', $mov);
155
                    break;
156
            }
157
        }
158
159
        $content = $this->smarty->fetch('movie-edit.tpl');
160
161
        $this->smarty->assign(compact('title', 'meta_title', 'content'));
162
163
        $this->adminrender();
164
    }
165
}
166