GameFileController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 225
Duplicated Lines 7.11 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 17
c 3
b 0
f 0
lcom 0
cbo 11
dl 16
loc 225
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
B download() 0 63 4
A download_wo_count() 0 8 1
A create() 16 16 1
B store() 0 44 2
A destroy() 0 9 1
A edit() 0 10 1
A restore() 0 9 1
B update() 0 56 6

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/*
4
 * rmarchiv.tk
5
 * (c) 2016-2017 by Marcel 'ryg' Hering
6
 */
7
8
namespace App\Http\Controllers;
9
10
use Carbon\Carbon;
11
use App\Events\Obyx;
12
use App\Models\Game;
13
use App\Models\GamesFile;
14
use Illuminate\Http\Request;
15
use App\Models\GamesFilesType;
16
use App\Helpers\DatabaseHelper;
17
use App\Models\UserDownloadLog;
18
19
class GameFileController extends Controller
20
{
21
    public function download($id)
22
    {
23
        \DB::table('games_files')
24
            ->where('id', '=', $id)
25
            ->increment('downloadcount');
26
27
        $g = \DB::table('games')
28
            ->select([
29
                'games.title as gametitle',
30
                'games.subtitle as gamesubtitle',
31
                'games_files.filename as filename',
32
                'games_files.extension as fileextension',
33
                'games_files_types.title as filetype',
34
                'games_files.release_version as fileversion',
35
                'games_files.release_day as fileday',
36
                'games_files.release_month as filemonth',
37
                'games_files.release_year as fileyear',
38
            ])
39
            ->leftJoin('games_files', 'games.id', '=', 'games_files.game_id')
40
            ->leftJoin('games_files_types', 'games_files.release_type', '=', 'games_files_types.id')
41
            ->where('games_files.id', '=', $id)
42
            ->limit(1)
43
            ->first();
44
45
        if (\Auth::check()) {
46
            UserDownloadLog::insert([
0 ignored issues
show
Bug introduced by
The method insert() does not exist on App\Models\UserDownloadLog. Did you maybe mean insertAndSetId()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
47
                'user_id'     => \Auth::id(),
48
                'gamefile_id' => $id,
49
                'created_at'  => Carbon::now(),
50
            ]);
51
        } else {
52
            UserDownloadLog::insert([
0 ignored issues
show
Bug introduced by
The method insert() does not exist on App\Models\UserDownloadLog. Did you maybe mean insertAndSetId()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
53
                'user_id'     => 0,
54
                'gamefile_id' => $id,
55
                'created_at'  => Carbon::now(),
56
            ]);
57
        }
58
59
        $filepath = storage_path('app/public/'.$g->filename);
60
61
        $newfilename = $g->gametitle.' - '.$g->gamesubtitle.' ['.$g->filetype.'-'.$g->fileversion.']-'.str_pad($g->fileyear, 2, 0, STR_PAD_LEFT)
62
            .'-'.str_pad($g->filemonth, 2, 0, STR_PAD_LEFT).'-'.str_pad($g->fileday, 2, 0, STR_PAD_LEFT).'.'.$g->fileextension;
63
64
        if (\Auth::check()) {
65
            //Check for existing download Template
66
            if (\Auth::user()->settings->download_template != '') {
67
68
                //Replace all stuff for download template
69
                $t = \Auth::user()->settings->download_template;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
70
                $t = str_replace('{title}', $g->gametitle, $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71
                $t = str_replace('{subtitle}', $g->gamesubtitle, $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72
                $t = str_replace('{reltype}', $g->filetype, $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
73
                $t = str_replace('{relversion}', $g->fileversion, $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74
                $t = str_replace('{relyear}', str_pad($g->fileyear, 2, 0, STR_PAD_LEFT), $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75
                $t = str_replace('{relmonth}', str_pad($g->filemonth, 2, 0, STR_PAD_LEFT), $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
76
                $t = str_replace('{relday}', str_pad($g->fileday, 2, 0, STR_PAD_LEFT), $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
77
                $t = str_replace('{ext}', $g->fileextension, $t);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
78
                $newfilename = $t;
79
            }
80
        }
81
82
        return response()->download($filepath, $newfilename);
83
    }
84
85
    public function download_wo_count(Request $request)
86
    {
87
        $filepath = storage_path('app/public/'.$request->get('filename'));
88
89
        $newfilename = $request->get('id');
90
91
        return response()->download($filepath, $newfilename);
92
    }
93
94 View Code Duplication
    public function create($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $gamefiles = GamesFile::whereGameId($id)
0 ignored issues
show
Bug introduced by
The method orderBy does only exist in Illuminate\Database\Query\Builder, but not in App\Models\GamesFile.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
97
            ->orderBy('release_year', 'desc')
98
            ->orderBy('release_month', 'desc')
99
            ->orderBy('release_day', 'desc')
100
            ->get();
101
        $game = Game::whereId($id)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
102
        $filetypes = GamesFilesType::get();
103
104
        return view('games.gamefiles', [
105
            'gamefiles' => $gamefiles,
106
            'game'      => $game,
107
            'filetypes' => $filetypes,
108
        ]);
109
    }
110
111
    public function store(Request $request, $id)
112
    {
113
        $this->validate($request, [
114
            'uuid'              => 'required',
115
            'version'           => 'required',
116
            'releasedate_day'   => 'required|not_in:0',
117
            'releasedate_month' => 'required|not_in:0',
118
            'releasedate_year'  => 'required|not_in:0',
119
            'filetype'          => 'required|not_in:0',
120
        ]);
121
122
        $storagetemp = 'temp/'.$request->get('uuid').'/file';
123
        $storagedest = 'games/'.$request->get('uuid').'.'.$request->get('ext');
124
125
        $meta['mime'] = \Storage::mimeType($storagetemp);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$meta was never initialized. Although not strictly required by PHP, it is generally a good practice to add $meta = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
126
        $meta['size'] = \Storage::size($storagetemp);
127
        $meta['ext'] = $request->get('ext');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
128
129
        $exists = \Storage::disk('local')->exists($storagetemp);
130
131
        if ($exists === true) {
132
            \Storage::move($storagetemp, $storagedest);
133
134
            \DB::table('games_files')->insert([
135
                'game_id'         => $id,
136
                'filesize'        => $meta['size'],
137
                'extension'       => $meta['ext'],
138
                'release_type'    => $request->get('filetype'),
139
                'release_version' => $request->get('version'),
140
                'release_day'     => $request->get('releasedate_day'),
141
                'release_month'   => $request->get('releasedate_month'),
142
                'release_year'    => $request->get('releasedate_year'),
143
                'user_id'         => \Auth::id(),
144
                'filename'        => $storagedest,
145
                'created_at'      => Carbon::now(),
146
            ]);
147
148
            event(new Obyx('gamefile-add', \Auth::id()));
149
        }
150
151
        DatabaseHelper::setReleaseInfos($id);
152
153
        return redirect()->route('gamefiles.index', [$id]);
154
    }
155
156
    public function destroy($id, $fileid)
157
    {
158
        $gf = GamesFile::whereId($fileid)->first();
159
        \Storage::delete($gf->filename);
160
161
        $gf->delete();
162
163
        return redirect()->route('gamefiles.index', [$id]);
164
    }
165
166
    public function edit($id, $gamefileid)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
167
    {
168
        $gamefile = GamesFile::whereId($gamefileid)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
169
        $filetypes = GamesFilesType::get();
170
171
        return view('games.gamefiles_edit', [
172
            'gamefile'  => $gamefile,
173
            'filetypes' => $filetypes,
174
        ]);
175
    }
176
177
    public function restore($gamefileid)
178
    {
179
        $gamefile = GamesFile::whereId($gamefileid)->first();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 12 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
180
        $gamefile->forbidden = 0;
181
        $gamefile->reason = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
182
        $gamefile->save();
183
184
        return redirect()->back();
185
    }
186
187
    public function update(Request $request, $id, $gamefileid)
188
    {
189
        $this->validate($request, [
190
            //'uuid' => 'required',
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
191
            'version'           => 'required',
192
            'releasedate_day'   => 'required|not_in:0',
193
            'releasedate_month' => 'required|not_in:0',
194
            'releasedate_year'  => 'required|not_in:0',
195
            'filetype'          => 'required|not_in:0',
196
        ]);
197
198
        $gamefile = GamesFile::whereId($gamefileid)->first();
199
200
        $gamefile->release_version = $request->get('version');
201
        $gamefile->release_day = $request->get('releasedate_day');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
202
        $gamefile->release_month = $request->get('releasedate_month');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
203
        $gamefile->release_year = $request->get('releasedate_year');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
204
        $gamefile->release_type = $request->get('filetype');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
205
206
        if ($request->get('uuid')) {
207
            //Create Backupfile
208
            if (\Storage::exists($gamefile->filename)) {
209
                \Storage::move($gamefile->filename, $gamefile->filename.'-'.time());
210
            }
211
212
            $storagetemp = 'temp/'.$request->get('uuid').'/file';
213
            $storagedest = 'games/'.$request->get('uuid').'.'.$request->get('ext');
214
215
            $meta['mime'] = \Storage::mimeType($storagetemp);
0 ignored issues
show
Coding Style Comprehensibility introduced by
$meta was never initialized. Although not strictly required by PHP, it is generally a good practice to add $meta = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
216
            $meta['size'] = \Storage::size($storagetemp);
217
            $meta['ext'] = $request->get('ext');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
218
219
            $exists = \Storage::disk('local')->exists($storagetemp);
220
221
            if ($exists === true) {
222
                \Storage::move($storagetemp, $storagedest);
223
                $gamefile->filesize = $meta['size'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
224
                $gamefile->extension = $meta['ext'];
225
                $gamefile->filename = $storagedest;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
226
            }
227
        }
228
229
        if ($request->get('forbidden') && ($request->get('forbidden') != '')) {
230
            $gamefile->forbidden = 1;
231
            $gamefile->reason = $request->get('forbidden');
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
232
        } else {
233
            $gamefile->forbidden = 0;
234
            $gamefile->reason = '';
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
235
        }
236
237
        $gamefile->save();
238
239
        DatabaseHelper::setReleaseInfos($id);
240
241
        return redirect()->route('gamefiles.index', [$id]);
242
    }
243
}
244