ProblemController::submitSolution()   B
last analyzed

Complexity

Conditions 7
Paths 4

Size

Total Lines 48
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 34
nc 4
nop 1
dl 0
loc 48
rs 8.4426
c 0
b 0
f 0
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Models\ProblemModel;
6
use App\Models\Eloquent\Problem;
7
use App\Models\Submission\SubmissionModel;
8
use App\Models\ResponseModel;
9
use App\Models\CompilerModel;
10
use App\Babel\Babel;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\Response;
14
use App\Jobs\ProcessSubmission;
15
use Illuminate\Support\Facades\Validator;
16
use Auth;
17
18
class ProblemController extends Controller
19
{
20
    /**
21
     * The Ajax Problem Solution Submit.
22
     *
23
     * @param Request $request web request
24
     *
25
     * @return Response
26
     */
27
    public function submitSolution(Request $request)
28
    {
29
        $problemModel=new ProblemModel();
30
        $submissionModel=new SubmissionModel();
31
        $compilerModel=new CompilerModel();
32
33
        $all_data=$request->all();
34
35
        $validator=Validator::make($all_data, [
36
            'solution' => 'required|string|max:65535',
37
        ]);
38
39
        if ($validator->fails()) {
40
            return ResponseModel::err(3002);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(3002) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
41
        }
42
        if (!$problemModel->ojdetail($problemModel->detail($problemModel->pcode($all_data['pid']))['OJ'])['status']) {
43
            return ResponseModel::err(6001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(6001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
44
        }
45
        if ($problemModel->isBlocked($all_data["pid"], isset($all_data["contest"]) ? $all_data["contest"] : null)) {
46
            return header("HTTP/1.1 403 Forbidden");
0 ignored issues
show
Bug Best Practice introduced by
The expression return header('HTTP/1.1 403 Forbidden') returns the type void which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
Bug introduced by
Are you sure the usage of header('HTTP/1.1 403 Forbidden') is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
47
        }
48
49
        $lang=$compilerModel->detail($all_data["coid"]);
50
51
        $sid=$submissionModel->insert([
52
            'time'=>'0',
53
            'verdict'=>'Pending',
54
            'solution'=>$all_data["solution"],
55
            'language'=>$lang['display_name'],
56
            'submission_date'=>time(),
57
            'memory'=>'0',
58
            'uid'=>Auth::user()->id,
0 ignored issues
show
Bug introduced by
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...
59
            'pid'=>$all_data["pid"],
60
            'remote_id'=>'',
61
            'coid'=>$all_data["coid"],
62
            'cid'=>isset($all_data["contest"]) ? $all_data["contest"] : null,
63
            'vcid'=>isset($all_data["vcid"]) ? $all_data["vcid"] : null,
64
            'jid'=>null,
65
            'score'=>0
66
        ]);
67
68
        $all_data["sid"]=$sid;
69
        $all_data["oj"]=$problemModel->ocode($all_data["pid"]);
70
        $all_data["lang"]=$lang['lcode'];
71
        dispatch(new ProcessSubmission($all_data))->onQueue($all_data["oj"]);
72
73
        return ResponseModel::success(200, null, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon..., array('sid' => $sid)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
74
            "sid"=>$sid
75
        ]);
76
    }
77
    /**
78
     * The Ajax Problem Status Check.
79
     *
80
     * @param Request $request web request
81
     *
82
     * @return Response
83
     */
84
    public function problemExists(Request $request)
85
    {
86
        $request->validate(["pcode" => "required|string|max:100"]);
87
        $problem = Problem::where('pcode', $request->pcode)->first();
88
        if (filled($problem)) {
89
            return ResponseModel::success(200, null, $problem->only(["pcode", "title"]));
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon...ray('pcode', 'title'))) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
90
        } else {
91
            return ResponseModel::err(3001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(3001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
92
        }
93
    }
94
    /**
95
     * The Ajax Problem Solution Discussion Submission.
96
     *
97
     * @param Request $request web request
98
     *
99
     * @return Response
100
     */
101
    public function submitSolutionDiscussion(Request $request)
102
    {
103
        $all_data=$request->all();
104
        $problemModel=new ProblemModel();
105
        $pid=$all_data["pid"];
106
        $content=$all_data["content"];
107
        $basic=$problemModel->basic($pid);
108
        if (empty($basic)) {
109
            return ResponseModel::err(3001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(3001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
110
        }
111
        $ret=$problemModel->addSolution($pid, Auth::user()->id, $content);
0 ignored issues
show
Bug introduced by
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...
112
        return $ret ?ResponseModel::success(200) : ResponseModel::err(3003);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret ? App\Models...esponseModel::err(3003) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
113
    }
114
    /**
115
     * The Ajax Problem Solution Discussion Update.
116
     *
117
     * @param Request $request web request
118
     *
119
     * @return Response
120
     */
121
    public function updateSolutionDiscussion(Request $request)
122
    {
123
        $all_data=$request->all();
124
        $problemModel=new ProblemModel();
125
        $psoid=$all_data["psoid"];
126
        $content=$all_data["content"];
127
        $ret=$problemModel->updateSolution($psoid, Auth::user()->id, $content);
0 ignored issues
show
Bug introduced by
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...
128
        return $ret ?ResponseModel::success(200) : ResponseModel::err(3004);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret ? App\Models...esponseModel::err(3004) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
129
    }
130
    /**
131
     * The Ajax Problem Solution Discussion Delete.
132
     *
133
     * @param Request $request web request
134
     *
135
     * @return Response
136
     */
137
    public function deleteSolutionDiscussion(Request $request)
138
    {
139
        $all_data=$request->all();
140
        $problemModel=new ProblemModel();
141
        $psoid=$all_data["psoid"];
142
        $ret=$problemModel->removeSolution($psoid, Auth::user()->id);
0 ignored issues
show
Bug introduced by
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...
143
        return $ret ?ResponseModel::success(200) : ResponseModel::err(3004);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret ? App\Models...esponseModel::err(3004) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
144
    }
145
    /**
146
     * The Ajax Problem Solution Discussion Vote.
147
     *
148
     * @param Request $request web request
149
     *
150
     * @return Response
151
     */
152
    public function voteSolutionDiscussion(Request $request)
153
    {
154
        $all_data=$request->all();
155
        $problemModel=new ProblemModel();
156
        $psoid=$all_data["psoid"];
157
        $type=$all_data["type"];
158
        $ret=$problemModel->voteSolution($psoid, Auth::user()->id, $type);
0 ignored issues
show
Bug introduced by
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...
159
        return $ret["ret"] ?ResponseModel::success(200, null, ["votes"=>$ret["votes"], "select"=>$ret["select"]]) : ResponseModel::err(3004);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret['ret'] ? App...esponseModel::err(3004) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
160
    }
161
    /**
162
     * The Ajax Problem Solution Submit.
163
     *
164
     * @param Request $request web request
165
     *
166
     * @return Response
167
     */
168
    public function downloadCode(Request $request)
169
    {
170
        $all_data=$request->all();
171
        $submissionModel=new SubmissionModel();
172
        $sid=$all_data["sid"];
173
        $downloadFile=$submissionModel->downloadCode($sid, Auth::user()->id);
0 ignored issues
show
Bug introduced by
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...
174
        if (empty($downloadFile)) {
175
            return ResponseModel::err(2001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
176
        }
177
        return response()->streamDownload(function() use ($downloadFile) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return response()->strea... $downloadFile['name']) returns the type Symfony\Component\HttpFoundation\StreamedResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
178
            echo $downloadFile["content"];
179
        }, $downloadFile["name"]);
180
    }
181
    /**
182
     * The Ajax Problem Judge.
183
     *
184
     * @param Request $request web request
185
     *
186
     * @return Response
187
     */
188
    public function judgeStatus(Request $request)
189
    {
190
        $all_data=$request->all();
191
        $submission=new SubmissionModel();
192
        $status=$submission->getJudgeStatus($all_data["sid"], Auth::user()->id);
0 ignored issues
show
Bug introduced by
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...
193
        return ResponseModel::success(200, null, $status);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon...ess(200, null, $status) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
194
    }
195
196
    /**
197
     * The Ajax Problem Manual Judge.
198
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
199
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
200
     *
201
     * @param Request $request web request
202
     *
203
     * @return Response
204
     */
205
    public function manualJudge(Request $request)
0 ignored issues
show
Unused Code introduced by
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

205
    public function manualJudge(/** @scrutinizer ignore-unused */ Request $request)

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...
206
    {
207
        if (Auth::user()->id!=1) {
0 ignored issues
show
Bug introduced by
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...
208
            return ResponseModel::err(2001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
209
        }
210
211
        $babel=new Babel();
212
        $vj_judge=$babel->judge();
213
214
        return ResponseModel::success(200, null, $vj_judge->ret);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon..., null, $vj_judge->ret) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
215
    }
216
217
    /**
218
     * Get the Submit History.
219
     *
220
     * @param Request $request web request
221
     *
222
     * @return Response
223
     */
224
    public function submitHistory(Request $request)
225
    {
226
        $all_data=$request->all();
227
        $submission=new SubmissionModel();
228
        if (isset($all_data["cid"])) {
229
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id, $all_data["cid"]);
0 ignored issues
show
Bug introduced by
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...
230
        } else {
231
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id);
232
        }
233
234
        return ResponseModel::success(200, null, ["history"=>$history]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon...'history' => $history)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
235
    }
236
237
    public function postDiscussion(Request $request)
238
    {
239
        $request->validate([
240
            'pid' => 'required|integer',
241
            'title' => 'required',
242
            'content' => 'required'
243
        ]);
244
        $all_data=$request->all();
245
        $problemModel=new ProblemModel();
246
        $pid=$all_data["pid"];
247
        $title=$all_data["title"];
248
        $content=$all_data["content"];
249
        $basic=$problemModel->basic($pid);
250
        if (empty($basic)) {
251
            return ResponseModel::err(3001);
252
        }
253
        $ret=$problemModel->addDiscussion(Auth::user()->id, $pid, $title, $content);
0 ignored issues
show
Bug introduced by
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...
254
        return $ret ?ResponseModel::success(200, null, $ret) : ResponseModel::err(3003);
255
    }
256
257
    public function addComment(Request $request)
258
    {
259
        $request->validate([
260
            'pdid' => 'required|integer',
261
            'content' => 'required'
262
        ]);
263
        $all_data=$request->all();
264
        $problemModel=new ProblemModel();
265
        $pdid=$all_data['pdid'];
266
        $content=$all_data['content'];
267
        $reply_id=$all_data['reply_id'];
268
        $pid=$problemModel->pidByPdid($pdid);
269
        $basic=$problemModel->basic($pid);
270
        if (empty($basic)) {
271
            return ResponseModel::err(3001);
272
        }
273
        $ret=$problemModel->addComment(Auth::user()->id, $pdid, $content, $reply_id);
0 ignored issues
show
Bug introduced by
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...
274
        return $ret ?ResponseModel::success(200, null, $ret) : ResponseModel::err(3003);
275
    }
276
277
    /**
278
     * Resubmit Submission Error Problems.
279
     *
280
     * @param Request $request web request
281
     *
282
     * @return Response
283
     */
284
    public function resubmitSolution(Request $request)
285
    {
286
        $all_data=$request->all();
287
        $submissionModel=new SubmissionModel();
288
        $problemModel=new ProblemModel();
289
        $compilerModel=new CompilerModel();
290
291
        $submissionData=$submissionModel->basic($all_data["sid"]);
292
293
        if ($submissionData["uid"]!=Auth::user()->id) {
0 ignored issues
show
Bug introduced by
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...
294
            return ResponseModel::err(2001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(2001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
295
        }
296
297
        if ($submissionData["verdict"]!="Submission Error") {
298
            return ResponseModel::err(6003);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(6003) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
299
        }
300
301
        $submissionModel->updateSubmission($all_data["sid"], [
302
            "verdict"=>"Pending",
303
            "time"=>0,
304
            "memory"=>0
305
        ]);
306
307
        $problemDetails=$problemModel->basic($submissionData["pid"]);
308
        $lang=$compilerModel->detail($submissionData["coid"]);
309
310
        if (!$problemModel->ojdetail($problemDetails['OJ'])['status']) {
311
            return ResponseModel::err(6001);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::err(6001) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
312
        }
313
314
        $proceedData=[];
315
        $proceedData["lang"]=$lang["lcode"];
316
        $proceedData["pid"]=$problemDetails["pid"];
317
        $proceedData["pcode"]=$problemDetails["pcode"];
318
        $proceedData["cid"]=$problemDetails["contest_id"];
319
        $proceedData["contest"]=$submissionData["cid"];
320
        $proceedData["vcid"]=$submissionData["vcid"];
321
        $proceedData["iid"]=$problemDetails["index_id"];
322
        $proceedData["oj"]=$problemModel->ocode($problemDetails["pid"]);
323
        $proceedData["coid"]=$lang["coid"];
324
        $proceedData["solution"]=$submissionData["solution"];
325
        $proceedData["sid"]=$submissionData["sid"];
326
327
        dispatch(new ProcessSubmission($proceedData))->onQueue($proceedData["oj"]);
328
329
        return ResponseModel::success(200);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\ResponseModel::success(200) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
330
    }
331
}
332