Completed
Push — master ( 481865...ed4291 )
by John
24s queued 10s
created

ProblemController   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 274
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 125
c 0
b 0
f 0
dl 0
loc 274
rs 10
wmc 29

11 Methods

Rating   Name   Duplication   Size   Complexity  
A submitHistory() 0 11 2
A submitSolutionDiscussion() 0 12 3
A judgeStatus() 0 6 1
A deleteSolutionDiscussion() 0 7 2
A voteSolutionDiscussion() 0 8 2
A problemExists() 0 11 2
A downloadCode() 0 12 2
A updateSolutionDiscussion() 0 8 2
B submitSolution() 0 48 7
A resubmitSolution() 0 46 4
A manualJudge() 0 10 2
1
<?php
2
3
namespace App\Http\Controllers\Ajax;
4
5
use App\Models\ProblemModel;
6
use App\Models\Submission\SubmissionModel;
7
use App\Models\ResponseModel;
8
use App\Models\CompilerModel;
9
use App\Babel\Babel;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Http\Request;
12
use Illuminate\Http\Response;
13
use App\Jobs\ProcessSubmission;
14
use Illuminate\Support\Facades\Validator;
15
use Auth;
16
17
class ProblemController extends Controller
18
{
19
    /**
20
     * The Ajax Problem Solution Submit.
21
     *
22
     * @param Request $request web request
23
     *
24
     * @return Response
25
     */
26
    public function submitSolution(Request $request)
27
    {
28
        $problemModel=new ProblemModel();
29
        $submissionModel=new SubmissionModel();
30
        $compilerModel=new CompilerModel();
31
32
        $all_data=$request->all();
33
34
        $validator=Validator::make($all_data, [
35
            'solution' => 'required|string|max:65535',
36
        ]);
37
38
        if ($validator->fails()) {
39
            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...
40
        }
41
        if (!$problemModel->ojdetail($problemModel->detail($problemModel->pcode($all_data['pid']))['OJ'])['status']) {
42
            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...
43
        }
44
        if ($problemModel->isBlocked($all_data["pid"], isset($all_data["contest"]) ? $all_data["contest"] : null)) {
45
            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...
46
        }
47
48
        $lang=$compilerModel->detail($all_data["coid"]);
49
50
        $sid=$submissionModel->insert([
51
            'time'=>'0',
52
            'verdict'=>'Pending',
53
            'solution'=>$all_data["solution"],
54
            'language'=>$lang['display_name'],
55
            'submission_date'=>time(),
56
            'memory'=>'0',
57
            'uid'=>Auth::user()->id,
58
            'pid'=>$all_data["pid"],
59
            'remote_id'=>'',
60
            'coid'=>$all_data["coid"],
61
            'cid'=>isset($all_data["contest"]) ? $all_data["contest"] : null,
62
            'vcid'=>isset($all_data["vcid"]) ? $all_data["vcid"] : null,
63
            'jid'=>null,
64
            'score'=>0
65
        ]);
66
67
        $all_data["sid"]=$sid;
68
        $all_data["oj"]=$problemModel->ocode($all_data["pid"]);
69
        $all_data["lang"]=$lang['lcode'];
70
        dispatch(new ProcessSubmission($all_data))->onQueue($all_data["oj"]);
71
72
        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...
73
            "sid"=>$sid
74
        ]);
75
    }
76
    /**
77
     * The Ajax Problem Status Check.
78
     *
79
     * @param Request $request web request
80
     *
81
     * @return Response
82
     */
83
    public function problemExists(Request $request)
84
    {
85
        $all_data=$request->all();
86
        $problemModel=new ProblemModel();
87
        $pcode=$problemModel->existPCode($all_data["pcode"]);
88
        if ($pcode) {
89
            return ResponseModel::success(200, null, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon...ray('pcode' => $pcode)) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
90
                "pcode"=>$pcode
91
            ]);
92
        } else {
93
            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...
94
        }
95
    }
96
    /**
97
     * The Ajax Problem Solution Discussion Submission.
98
     *
99
     * @param Request $request web request
100
     *
101
     * @return Response
102
     */
103
    public function submitSolutionDiscussion(Request $request)
104
    {
105
        $all_data=$request->all();
106
        $problemModel=new ProblemModel();
107
        $pid=$all_data["pid"];
108
        $content=$all_data["content"];
109
        $basic=$problemModel->basic($pid);
110
        if (empty($basic)) {
111
            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...
112
        }
113
        $ret=$problemModel->addSolution($pid,Auth::user()->id,$content);
114
        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...
115
    }
116
    /**
117
     * The Ajax Problem Solution Discussion Update.
118
     *
119
     * @param Request $request web request
120
     *
121
     * @return Response
122
     */
123
    public function updateSolutionDiscussion(Request $request)
124
    {
125
        $all_data=$request->all();
126
        $problemModel=new ProblemModel();
127
        $psoid=$all_data["psoid"];
128
        $content=$all_data["content"];
129
        $ret=$problemModel->updateSolution($psoid,Auth::user()->id,$content);
130
        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...
131
    }
132
    /**
133
     * The Ajax Problem Solution Discussion Delete.
134
     *
135
     * @param Request $request web request
136
     *
137
     * @return Response
138
     */
139
    public function deleteSolutionDiscussion(Request $request)
140
    {
141
        $all_data=$request->all();
142
        $problemModel=new ProblemModel();
143
        $psoid=$all_data["psoid"];
144
        $ret=$problemModel->removeSolution($psoid,Auth::user()->id);
145
        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...
146
    }
147
    /**
148
     * The Ajax Problem Solution Discussion Vote.
149
     *
150
     * @param Request $request web request
151
     *
152
     * @return Response
153
     */
154
    public function voteSolutionDiscussion(Request $request)
155
    {
156
        $all_data=$request->all();
157
        $problemModel=new ProblemModel();
158
        $psoid=$all_data["psoid"];
159
        $type=$all_data["type"];
160
        $ret=$problemModel->voteSolution($psoid,Auth::user()->id,$type);
161
        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...
162
    }
163
    /**
164
     * The Ajax Problem Solution Submit.
165
     *
166
     * @param Request $request web request
167
     *
168
     * @return Response
169
     */
170
    public function downloadCode(Request $request)
171
    {
172
        $all_data=$request->all();
173
        $submissionModel=new SubmissionModel();
174
        $sid=$all_data["sid"];
175
        $downloadFile=$submissionModel->downloadCode($sid, Auth::user()->id);
176
        if (empty($downloadFile)) {
177
            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...
178
        }
179
        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...
180
            echo $downloadFile["content"];
181
        }, $downloadFile["name"]);
182
    }
183
    /**
184
     * The Ajax Problem Judge.
185
     *
186
     * @param Request $request web request
187
     *
188
     * @return Response
189
     */
190
    public function judgeStatus(Request $request)
191
    {
192
        $all_data=$request->all();
193
        $submission=new SubmissionModel();
194
        $status=$submission->getJudgeStatus($all_data["sid"], Auth::user()->id);
195
        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...
196
    }
197
198
    /**
199
     * The Ajax Problem Manual Judge.
200
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
201
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
202
     *
203
     * @param Request $request web request
204
     *
205
     * @return Response
206
     */
207
    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

207
    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...
208
    {
209
        if (Auth::user()->id!=1) {
210
            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...
211
        }
212
213
        $babel=new Babel();
214
        $vj_judge=$babel->judge();
215
216
        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...
217
    }
218
219
    /**
220
     * Get the Submit History.
221
     *
222
     * @param Request $request web request
223
     *
224
     * @return Response
225
     */
226
    public function submitHistory(Request $request)
227
    {
228
        $all_data=$request->all();
229
        $submission=new SubmissionModel();
230
        if (isset($all_data["cid"])) {
231
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id, $all_data["cid"]);
232
        } else {
233
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id);
234
        }
235
236
        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...
237
    }
238
    /**
239
     * Resubmit Submission Error Problems.
240
     *
241
     * @param Request $request web request
242
     *
243
     * @return Response
244
     */
245
    public function resubmitSolution(Request $request)
246
    {
247
        $all_data=$request->all();
248
        $submissionModel=new SubmissionModel();
249
        $problemModel=new ProblemModel();
250
        $compilerModel=new CompilerModel();
251
252
        $submissionData=$submissionModel->basic($all_data["sid"]);
253
254
        if($submissionData["uid"]!=Auth::user()->id){
255
            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...
256
        }
257
258
        if($submissionData["verdict"]!="Submission Error"){
259
            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...
260
        }
261
262
        $submissionModel->updateSubmission($all_data["sid"],[
263
            "verdict"=>"Pending",
264
            "time"=>0,
265
            "memory"=>0
266
        ]);
267
268
        $problemDetails=$problemModel->basic($submissionData["pid"]);
269
        $lang=$compilerModel->detail($submissionData["coid"]);
270
271
        if (!$problemModel->ojdetail($problemDetails['OJ'])['status']) {
272
            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...
273
        }
274
275
        $proceedData=[];
276
        $proceedData["lang"]=$lang["lcode"];
277
        $proceedData["pid"]=$problemDetails["pid"];
278
        $proceedData["pcode"]=$problemDetails["pcode"];
279
        $proceedData["cid"]=$problemDetails["contest_id"];
280
        $proceedData["contest"]=$submissionData["cid"];
281
        $proceedData["vcid"]=$submissionData["vcid"];
282
        $proceedData["iid"]=$problemDetails["index_id"];
283
        $proceedData["oj"]=$problemModel->ocode($problemDetails["pid"]);
284
        $proceedData["coid"]=$lang["coid"];
285
        $proceedData["solution"]=$submissionData["solution"];
286
        $proceedData["sid"]=$submissionData["sid"];
287
288
        dispatch(new ProcessSubmission($proceedData))->onQueue($proceedData["oj"]);
289
290
        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...
291
    }
292
}
293