SavegameManagerController::download()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 9
nc 2
nop 1
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 App\Models\GamesFile;
11
use Illuminate\Http\Request;
12
use App\Helpers\PlayerHelper;
13
use App\Models\GamesSavegame;
14
use PhpBinaryReader\BinaryReader;
15
16
class SavegameManagerController extends Controller
17
{
18
    public function index()
19
    {
20
        $savegames = GamesSavegame::whereUserId(\Auth::id())
0 ignored issues
show
Bug introduced by
The method groupBy does only exist in Illuminate\Database\Query\Builder, but not in App\Models\GamesSavegame.

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...
21
            ->groupBy('gamefile_id')
22
            ->orderBy('updated_at', 'desc')
23
            ->get();
24
25
        return view('savegamemanager.index', [
26
            'games' => $savegames,
27
        ]);
28
    }
29
30
    public function show($gamefile_id)
31
    {
32
        if (\Auth::check()) {
33
            $savegames = GamesSavegame::whereUserId(\Auth::id())
34
                ->where('gamefile_id', '=', $gamefile_id)
35
                ->orderBy('slot_id', 'asc')
36
                ->get();
37
38
            $gamefile = GamesFile::whereId($gamefile_id)->first();
39
40
            $array = [];
41
42
            foreach ($savegames as $savegame) {
43
                $t['id'] = $savegame->id;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$t was never initialized. Although not strictly required by PHP, it is generally a good practice to add $t = 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...
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...
44
                $t['slot'] = $savegame->slot_id;
45
                $t['data'] = $this->get_lsd_headerdata($savegame->save_data, $gamefile_id);
46
                $t['date'] = $savegame->updated_at;
47
48
                $array[] = $t;
49
            }
50
51
            return view('savegamemanager.show', [
52
                'savegames'   => $array,
53
                'gamefile_id' => $gamefile_id,
54
                'gamefile'    => $gamefile,
55
            ]);
56
        } else {
57
            return redirect()->action('IndexController@index');
58
        }
59
    }
60
61
    public function get_lsd_headerdata($data, $gamefile_id)
62
    {
63
        $data = base64_decode($data);
64
65
        $br = new BinaryReader($data);
66
67
        $array = [];
68
        $br->setPosition(0);
69
70
        $array['header']['length'] = $br->readInt8();
71
        $array['header']['data'] = $br->readString($array['header']['length']);
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...
72
73
        $cat = $br->readInt8();
74
        $br->readInt8();
75
        $br->readInt8();
76
        $array[$cat]['date']['length'] = $br->readInt8();
77
        $array[$cat]['date']['data'] = $br->readString($array[$cat]['date']['length']);
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...
78
79
        $array[$cat]['char1_name']['idx'] = $br->readInt8();
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...
80
        $array[$cat]['char1_name']['length'] = $br->readInt8();
81
        $array[$cat]['char1_name']['data'] = $br->readString($array[$cat]['char1_name']['length']);
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...
82
83
        $array[$cat]['char1_level']['idx'] = $br->readInt8();
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...
84
        $array[$cat]['char1_level']['length'] = $br->readInt8();
85
        $array[$cat]['char1_level']['data'] = $br->readInt8();
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...
86
87
        $array[$cat]['char1_hp']['idx'] = $br->readInt8();
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...
88
        $array[$cat]['char1_hp']['length'] = $br->readInt8();
89
        if ($array[$cat]['char1_hp']['length'] == 2) {
90
            $array[$cat]['char1_hp']['data'] = $br->readInt16();
91
        } else {
92
            $array[$cat]['char1_hp']['data'] = $br->readInt8();
93
        }
94
95
        $array[$cat]['char1_face']['idx'] = $br->readInt8();
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...
96
        $array[$cat]['char1_face']['length'] = $br->readInt8();
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...
97
        $array[$cat]['char1_face']['data'] = $br->readString($array[$cat]['char1_face']['length']);
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...
98
        $array[$cat]['char1_face']['img_idx'] = $br->readInt8();
99
100
        if (! is_null($array[$cat]['char1_face']['img_idx'])) {
101
            $pc = new Player2kController();
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...
102
            $files = json_decode($pc->deliver_indexjson($gamefile_id), true);
103
104
            $file = $files['faceset/'.strtolower($array[$cat]['char1_face']['data'])];
105
106
            $file = action('Player2kController@deliver_files', [$gamefile_id, $file]);
107
108
            $array[$cat]['char1_face']['url'] = $file;
109
        } else {
110
            $array[$cat]['char1_face']['url'] = '';
111
        }
112
113
        return $array;
114
    }
115
116
    public function delete($savegame_id)
117
    {
118
        if (\Auth::check()) {
119
            $save = GamesSavegame::whereId($savegame_id)->where('user_id', '=', \Auth::id())->first();
120
            $save->forceDelete();
121
        }
122
123
        return redirect()->back();
124
    }
125
126
    public function download($savegame_id)
127
    {
128
        if (\Auth::check()) {
129
            $save = GamesSavegame::whereId($savegame_id)->where('user_id', '=', \Auth::id())->first();
130
            $data = base64_decode($save->save_data);
131
132
            $filename = 'Save'.str_pad($save->slot_id, 2, '0', STR_PAD_LEFT).'.lsd';
133
            $headers = [
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...
134
                'Content-type'        => 'application/octet-stream',
135
                'Content-Disposition' => sprintf('attachment; filename="%s"', $filename),
136
                ];
137
138
            return \Response::make($data, 200, $headers);
139
        }
140
    }
141
142
    public function store(Request $request)
143
    {
144
        if (\Auth::check()) {
145
            $this->validate($request, [
146
                'slot'        => 'required',
147
                'file'        => 'required',
148
                'gamefile_id' => 'required',
149
            ]);
150
151
            $data = file_get_contents($request->file('file')->getRealPath());
152
153
            if (PlayerHelper::getSavegameValidation($data) == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
154
                $check = GamesSavegame::whereGamefileId($request->get('gamefile_id'))
155
                    ->where('user_id', '=', \Auth::id())
156
                    ->where('slot_id', '=', $request->get('slot'))
157
                    ->first();
158
159
                if (! $check) {
160
                    $save = new GamesSavegame();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 14 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...
161
                    $save->gamefile_id = $request->get('gamefile_id');
162
                    $save->user_id = \Auth::id();
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...
163
                    $save->save_data = base64_encode($data);
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...
164
                    $save->slot_id = $request->get('slot');
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...
165
                    $save->save();
166
                } else {
167
                    $check->save_data = base64_encode($data);
168
                    $check->save();
169
                }
170
            }
171
        }
172
173
        return redirect()->action('SavegameManagerController@show', $request->get('gamefile_id'));
174
    }
175
}
176