Issues (465)

app/Http/Controllers/DnaController.php (13 issues)

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Jobs\DnaMatching;
6
use App\Models\Dna;
7
use Auth;
8
use Illuminate\Http\Request;
9
10
class DnaController extends Controller
11
{
12
    /**
13
     * Display a listing of the resource.
14
     *
15
     * @return \Illuminate\Http\Response
16
     */
17
    public function index(Request $request)
18
    {
19
        $query = Dna::query();
20
21
        if ($request->has('searchTerm')) {
22
            $columnsToSearch = ['name'];
23
            $search_term = json_decode($request->searchTerm)->searchTerm;
24
            if (! empty($search_term)) {
25
                $searchQuery = '%'.$search_term.'%';
26
                foreach ($columnsToSearch as $column) {
27
                    $query->orWhere($column, 'LIKE', $searchQuery);
28
                }
29
            }
30
        }
31
32
        if ($request->has('columnFilters')) {
33
            $filters = get_object_vars(json_decode($request->columnFilters));
34
35
            foreach ($filters as $key => $value) {
36
                if (! empty($value)) {
37
                    $query->orWhere($key, 'like', '%'.$value.'%');
38
                }
39
            }
40
        }
41
42
        if ($request->has('sort.0')) {
43
            $sort = json_decode($request->sort[0]);
44
            $query->orderBy($sort->field, $sort->type);
45
        }
46
47
        if ($request->has('perPage')) {
48
            $rows = $query->paginate($request->perPage);
49
        }
50
51
        return $rows;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $rows does not seem to be defined for all execution paths leading up to this point.
Loading history...
52
    }
53
54
    /**
55
     * Show the form for creating a new resource.
56
     *
57
     * @return \Illuminate\Http\Response
58
     */
59
    public function create()
60
    {
61
        //
62
    }
63
64
    /**
65
     * Store a newly created resource in storage.
66
     *
67
     * @param  \Illuminate\Http\Request  $request
68
     * @return \Illuminate\Http\Response
69
     */
70
    public function store(Request $request)
71
    {
72
        if ($request->hasFile('file')) {
73
            if ($request->file('file')->isValid()) {
74
                try {
75
                    $currentUser = Auth::user();
76
                    $file_name = 'dna_'.$request->file('file')->getClientOriginalName().uniqid().'.'.$request->file('file')->extension();
77
                    $request->file->storeAs('dna', $file_name);
78
                    define('STDIN', fopen('php://stdin', 'r'));
79
                    $random_string = unique_random('dnas', 'variable_name', 5);
80
                    $var_name = 'var_'.$random_string;
81
                    $filename = 'app/dna/'.$file_name;
0 ignored issues
show
The assignment to $filename is dead and can be removed.
Loading history...
82
                    $user_id = $currentUser->id;
0 ignored issues
show
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
83
                    $dna = new Dna();
84
                    $dna->name = 'DNA Kit for user '.$user_id;
85
                    $dna->user_id = $user_id;
86
                    $dna->variable_name = $var_name;
87
                    $dna->file_name = $file_name;
88
                    $dna->save();
89
                    DnaMatching::dispatch($currentUser, $var_name, $file_name);
90
91
                    return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('message' =...ray('dna' => $dna->id)) returns the type array<string,array|array<string,integer>|string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
92
                        'message' => __('The dna was successfully created'),
93
                        'redirect' => 'dna.edit',
94
                        'param' => ['dna' => $dna->id],
95
                    ];
96
                } catch (\Exception $e) {
97
                    return $e->getMessage();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $e->getMessage() returns the type string which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
98
                }
99
            }
100
101
            return ['File corrupted'];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('File corrupted') returns the type array<integer,string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
102
        }
103
104
        return response()->json(['Not uploaded'], 422);
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->json(...y('Not uploaded'), 422) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
105
    }
106
107
    /**
108
     * Display the specified resource.
109
     *
110
     * @param  int  $id
111
     * @return \Illuminate\Http\Response
112
     */
113
    public function show($id)
114
    {
115
        return Dna::find($id);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Dna::find($id) also could return the type App\Models\Dna which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
116
    }
117
118
    /**
119
     * Show the form for editing the specified resource.
120
     *
121
     * @param  int  $id
122
     * @return \Illuminate\Http\Response
123
     */
124
    public function edit($id)
0 ignored issues
show
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

124
    public function edit(/** @scrutinizer ignore-unused */ $id)

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

Loading history...
125
    {
126
        //
127
    }
128
129
    /**
130
     * Update the specified resource in storage.
131
     *
132
     * @param  \Illuminate\Http\Request  $request
133
     * @param  int  $id
134
     * @return \Illuminate\Http\Response
135
     */
136
    public function update(Request $request, $id)
0 ignored issues
show
The parameter $request is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function update(/** @scrutinizer ignore-unused */ Request $request, $id)

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

Loading history...
The parameter $id is not used and could be removed. ( Ignorable by Annotation )

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

136
    public function update(Request $request, /** @scrutinizer ignore-unused */ $id)

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

Loading history...
137
    {
138
    }
139
140
    /**
141
     * Remove the specified resource from storage.
142
     *
143
     * @param  int  $id
144
     * @return \Illuminate\Http\Response
145
     */
146
    public function destroy($id)
147
    {
148
        $user = auth()->user();
149
        $dna = Dna::find($id);
150
        if ($user->id == $dna->user_id) {
151
            $dna->delete();
152
153
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('message' =...direct' => 'dna.index') returns the type array<string,array|string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
154
                'message' => __('The dna was successfully deleted'),
155
                'redirect' => 'dna.index',
156
            ];
157
        } else {
158
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array('message' =...direct' => 'dna.index') returns the type array<string,array|string> which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
159
                'message' => __('The dna could not be deleted'),
160
                'redirect' => 'dna.index',
161
            ];
162
        }
163
    }
164
}
165