Passed
Push — master ( 7753b7...16b395 )
by
unknown
04:57 queued 10s
created

ProblemController::submitHistory()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
rs 10
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\SubmissionModel;
7
use App\Models\ResponseModel;
8
use App\Models\CompilerModel;
9
use App\Http\Controllers\VirtualJudge\Submit;
10
use App\Http\Controllers\VirtualJudge\Judge;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Http\Request;
13
use Illuminate\Http\Response;
14
use App\Http\Controllers\VirtualCrawler\Crawler;
15
use App\Jobs\ProcessSubmission;
16
use Illuminate\Support\Facades\Validator;
17
use Auth;
18
19
class ProblemController extends Controller
20
{
21
    /**
22
     * The Ajax Problem Solution Submit.
23
     *
24
     * @param Request $request web request
25
     *
26
     * @return Response
27
     */
28
    public function submitSolution(Request $request)
29
    {
30
        $problemModel=new ProblemModel();
31
        $submissionModel=new SubmissionModel();
32
        $compilerModel=new CompilerModel();
33
34
        $all_data=$request->all();
35
36
        $validator=Validator::make($all_data, [
37
            'solution' => 'required|string|max:65535',
38
        ]);
39
40
        if ($validator->fails()) {
41
            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...
42
        }
43
        if (!$problemModel->ojdetail($problemModel->detail($problemModel->pcode($all_data['pid']))['OJ'])['status']) {
44
            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...
45
        }
46
        if ($problemModel->isBlocked($all_data["pid"], isset($all_data["contest"]) ? $all_data["contest"] : null)) {
47
            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...
48
        }
49
50
        $lang=$compilerModel->detail($all_data["coid"]);
51
52
        $sid=$submissionModel->insert([
53
            'time'=>'0',
54
            'verdict'=>'Pending',
55
            'solution'=>$all_data["solution"],
56
            'language'=>$lang['display_name'],
57
            'submission_date'=>time(),
58
            'memory'=>'0',
59
            'uid'=>Auth::user()->id,
60
            'pid'=>$all_data["pid"],
61
            'remote_id'=>'',
62
            'coid'=>$all_data["coid"],
63
            'cid'=>isset($all_data["contest"]) ? $all_data["contest"] : 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 Solution Submit.
79
     *
80
     * @param Request $request web request
81
     *
82
     * @return Response
83
     */
84
    public function problemExists(Request $request)
85
    {
86
        $all_data=$request->all();
87
        $problemModel=new ProblemModel();
88
        $pcode=$problemModel->existPCode($all_data["pcode"]);
89
        if ($pcode) {
90
            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...
91
                "pcode"=>$pcode
92
            ]);
93
        } else {
94
            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...
95
        }
96
    }
97
    /**
98
     * The Ajax Problem Solution Submit.
99
     *
100
     * @param Request $request web request
101
     *
102
     * @return Response
103
     */
104
    public function downloadCode(Request $request)
105
    {
106
        $all_data=$request->all();
107
        $submissionModel=new SubmissionModel();
108
        $sid=$all_data["sid"];
109
        $downloadFile=$submissionModel->downloadCode($sid, Auth::user()->id);
110
        if (empty($downloadFile)) {
111
            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...
112
        }
113
        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...
114
            echo $downloadFile["content"];
115
        }, $downloadFile["name"]);
116
    }
117
    /**
118
     * The Ajax Problem Judge.
119
     *
120
     * @param Request $request web request
121
     *
122
     * @return Response
123
     */
124
    public function judgeStatus(Request $request)
125
    {
126
        $all_data=$request->all();
127
        $submission=new SubmissionModel();
128
        $status=$submission->getJudgeStatus($all_data["sid"], Auth::user()->id);
129
        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...
130
    }
131
132
    /**
133
     * The Ajax Problem Manual Judge.
134
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
135
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
136
     *
137
     * @param Request $request web request
138
     *
139
     * @return Response
140
     */
141
    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

141
    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...
142
    {
143
        if (Auth::user()->id!=1) {
144
            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...
145
        }
146
147
        $vj_judge=new Judge();
148
149
        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...
150
    }
151
152
    /**
153
     * Get the Submit History.
154
     *
155
     * @param Request $request web request
156
     *
157
     * @return Response
158
     */
159
    public function submitHistory(Request $request)
160
    {
161
        $all_data=$request->all();
162
        $submission=new SubmissionModel();
163
        if (isset($all_data["cid"])) {
164
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id, $all_data["cid"]);
165
        } else {
166
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id);
167
        }
168
169
        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...
170
    }
171
172
    /**
173
     * Crawler Ajax Control.
174
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
175
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
176
     *
177
     * @param Request $request web request
178
     *
179
     * @return Response
180
     */
181
    public function crawler(Request $request)
182
    {
183
        if (Auth::user()->id!=1) {
184
            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...
185
        }
186
187
        $all_data=$request->all();
188
189
        $crawler = new Crawler($all_data["name"], $all_data["action"], $all_data["con"], $all_data["cached"]);
190
191
        return ResponseModel::success(200, null, $crawler->data);
0 ignored issues
show
Bug Best Practice introduced by
The expression return App\Models\Respon..., null, $crawler->data) returns the type Illuminate\Http\JsonResponse which is incompatible with the documented return type Illuminate\Http\Response.
Loading history...
192
    }
193
}
194