Passed
Push — master ( b24fb5...31775c )
by John
04:37
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
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
            'jid'=>null,
63
            'score'=>0
64
        ]);
65
66
        $all_data["sid"]=$sid;
67
        $all_data["oj"]=$problemModel->ocode($all_data["pid"]);
68
        $all_data["lang"]=$lang['lcode'];
69
        dispatch(new ProcessSubmission($all_data))->onQueue($all_data["oj"]);
70
71
        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...
72
            "sid"=>$sid
73
        ]);
74
    }
75
    /**
76
     * The Ajax Problem Solution Submit.
77
     *
78
     * @param Request $request web request
79
     *
80
     * @return Response
81
     */
82
    public function problemExists(Request $request)
83
    {
84
        $all_data=$request->all();
85
        $problemModel=new ProblemModel();
86
        $pcode=$problemModel->existPCode($all_data["pcode"]);
87
        if ($pcode) {
88
            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...
89
                "pcode"=>$pcode
90
            ]);
91
        } else {
92
            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...
93
        }
94
    }
95
    /**
96
     * The Ajax Problem Solution Submit.
97
     *
98
     * @param Request $request web request
99
     *
100
     * @return Response
101
     */
102
    public function downloadCode(Request $request)
103
    {
104
        $all_data=$request->all();
105
        $submissionModel=new SubmissionModel();
106
        $sid=$all_data["sid"];
107
        $downloadFile=$submissionModel->downloadCode($sid, Auth::user()->id);
108
        if(empty($downloadFile)) {
109
            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...
110
        }
111
        return response()->streamDownload(function () {
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...
112
            echo $downloadFile["content"];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $downloadFile seems to be never defined.
Loading history...
113
        }, $downloadFile["name"]);
114
    }
115
    /**
116
     * The Ajax Problem Judge.
117
     *
118
     * @param Request $request web request
119
     *
120
     * @return Response
121
     */
122
    public function judgeStatus(Request $request)
123
    {
124
        $all_data=$request->all();
125
        $submission=new SubmissionModel();
126
        $status=$submission->getJudgeStatus($all_data["sid"], Auth::user()->id);
127
        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...
128
    }
129
130
    /**
131
     * The Ajax Problem Manual Judge.
132
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
133
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
134
     *
135
     * @param Request $request web request
136
     *
137
     * @return Response
138
     */
139
    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

139
    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...
140
    {
141
        if (Auth::user()->id!=1) {
142
            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...
143
        }
144
145
        $vj_judge=new Judge();
146
147
        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...
148
    }
149
150
    /**
151
     * Get the Submit History.
152
     *
153
     * @param Request $request web request
154
     *
155
     * @return Response
156
     */
157
    public function submitHistory(Request $request)
158
    {
159
        $all_data=$request->all();
160
        $submission=new SubmissionModel();
161
        if (isset($all_data["cid"])) {
162
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id, $all_data["cid"]);
163
        } else {
164
            $history=$submission->getProblemSubmission($all_data["pid"], Auth::user()->id);
165
        }
166
167
        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...
168
    }
169
170
    /**
171
     * Crawler Ajax Control.
172
     * [Notice] THIS FUNCTION IS FOR TEST ONLY
173
     * SHALL BE STRICTLY FORBIDDEN UNDER PRODUCTION ENVIRONMENT.
174
     *
175
     * @param Request $request web request
176
     *
177
     * @return Response
178
     */
179
    public function crawler(Request $request)
180
    {
181
        if (Auth::user()->id!=1) {
182
            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...
183
        }
184
185
        $all_data=$request->all();
186
187
        new Crawler($all_data["name"], $all_data["action"], $all_data["con"], $all_data["cached"]);
188
189
        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...
190
    }
191
}
192