Completed
Push — master ( a4261d...fc5e52 )
by John
14s
created

ContestController   A

Complexity

Total Complexity 41

Size/Duplication

Total Lines 393
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 265
dl 0
loc 393
rs 9.1199
c 2
b 0
f 0
wmc 41

12 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 35 11
A detail() 0 25 4
A rank() 0 29 2
A status() 0 25 2
A board() 0 3 1
A challenge() 0 30 3
A admin() 0 26 2
A analysis() 0 18 2
A clarification() 0 23 2
B editor() 0 61 7
A print() 0 19 2
A downloadContestAccountXlsx() 0 14 3

How to fix   Complexity   

Complex Class

Complex classes like ContestController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ContestController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers;
4
5
use App\Models\ContestModel;
6
use App\Models\GroupModel;
7
use App\Models\ProblemModel;
8
use App\Models\CompilerModel;
9
use App\Models\SubmissionModel;
10
use App\Models\AccountModel;
11
use App\Http\Controllers\Controller;
12
use Illuminate\Http\Request;
13
use Auth;
14
use Redirect;
15
use App\Exports\AccountExport;
16
use Excel;
17
18
class ContestController extends Controller
19
{
20
    /**
21
     * Show the Contest Page.
22
     *
23
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
24
     */
25
    public function index(Request $request)
26
    {
27
        $all_data=$request->all();
28
        $contestModel=new ContestModel();
29
        $filter["rule"]=isset($all_data["rule"]) ? $all_data["rule"] : null;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$filter was never initialized. Although not strictly required by PHP, it is generally a good practice to add $filter = array(); before regardless.
Loading history...
30
        $filter["public"]=isset($all_data["public"]) ? $all_data["public"] : null;
31
        $filter["verified"]=isset($all_data["verified"]) ? $all_data["verified"] : null;
32
        $filter["rated"]=isset($all_data["rated"]) ? $all_data["rated"] : null;
33
        $filter["anticheated"]=isset($all_data["anticheated"]) ? $all_data["anticheated"] : null;
34
        $filter["practice"]=isset($all_data["practice"]) ? $all_data["practice"] : null;
35
        $return_list=$contestModel->list($filter,Auth::check()?Auth::user()->id:0);
36
        $featured=$contestModel->featured();
37
        if (is_null($return_list)) {
0 ignored issues
show
introduced by
The condition is_null($return_list) is always false.
Loading history...
38
            if (isset($all_data["page"]) && $all_data["page"]>1) {
39
                return redirect("/contest");
40
            } else {
41
                return view('contest.index', [
42
                    'page_title'=>"Contest",
43
                    'site_title'=>config("app.name"),
44
                    'navigation' => "Contest",
45
                    'contest_list'=> null,
46
                    'paginator' => null,
47
                    'featured'=>$featured,
48
                    'filter' => $filter
49
                ]);
50
            }
51
        } else {
52
            return view('contest.index', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.ind..., 'filter' => $filter)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
53
                'page_title'=>"Contest",
54
                'site_title'=>config("app.name"),
55
                'navigation' => "Contest",
56
                'contest_list'=>$return_list['contents'],
57
                'paginator' => $return_list['paginator'],
58
                'featured'=>$featured,
59
                'filter' => $filter
60
            ]);
61
        }
62
    }
63
64
    /**
65
     * Show the Contest Detail Page.
66
     *
67
     * @return Response
68
     */
69
    public function detail($cid)
70
    {
71
        $contestModel=new ContestModel();
72
        $groupModel=new GroupModel();
73
        $clearance=Auth::check() ? $contestModel->judgeClearance($cid, Auth::user()->id) : 0;
74
        if (Auth::check()) {
75
            $contest_detail=$contestModel->detail($cid, Auth::user()->id);
76
            $registration=$contestModel->registration($cid, Auth::user()->id);
77
            $inGroup=$groupModel->isMember($contest_detail["data"]["contest_detail"]["gid"], Auth::user()->id);
78
        } else {
79
            $contest_detail=$contestModel->detail($cid);
80
            $registration=[];
81
            $inGroup=false;
82
        }
83
        if ($contest_detail["ret"]!=200) {
84
            return Redirect::route('contest_index');
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('contest_index') returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
85
        }
86
        return view('contest.detail', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.det...'inGroup' => $inGroup)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
87
            'page_title'=>"Contest",
88
            'site_title'=>config("app.name"),
89
            'navigation' => "Contest",
90
            'detail'=>$contest_detail["data"]["contest_detail"],
91
            'clearance' => $clearance,
92
            'registration' => $registration,
93
            'inGroup' => $inGroup
94
        ]);
95
    }
96
97
    /**
98
     * Redirect the Contest Board Page.
99
     *
100
     * @return Response
101
     */
102
    public function board($cid)
103
    {
104
        return Redirect::route('contest.challenge', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
105
    }
106
107
    /**
108
     * Show the Contest Challenge Page.
109
     *
110
     * @return Response
111
     */
112
    public function challenge($cid)
113
    {
114
        $contestModel=new ContestModel();
115
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
116
        if (!$clearance) {
117
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
118
        }
119
        $contest_name=$contestModel->contestName($cid);
120
        $contest_rule=$contestModel->contestRule($cid);
121
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
122
        $remainingTime=$contestModel->remainingTime($cid);
123
        $customInfo=$contestModel->getCustomInfo($cid);
124
        $clarificationList=$contestModel->getLatestClarification($cid);
125
        $basicInfo=$contestModel->basic($cid);
126
        if ($remainingTime<=0) {
127
            $remainingTime=0;
128
        }
129
        return view('contest.board.challenge', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...'basic' => $basicInfo)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
130
            'page_title'=>"Challenge",
131
            'navigation' => "Contest",
132
            'site_title'=>$contest_name,
133
            'cid'=>$cid,
134
            'contest_name'=>$contest_name,
135
            'contest_rule'=>$contest_rule,
136
            'problem_set'=> $problemSet,
137
            'remaining_time'=>$remainingTime,
138
            'custom_info' => $customInfo,
139
            'clarification_list' => $clarificationList,
140
            'clearance'=> $clearance,
141
            'basic'=>$basicInfo,
142
        ]);
143
    }
144
145
    /**
146
     * Show the Contest Editor Page.
147
     *
148
     * @return Response
149
     */
150
    public function editor($cid, $ncode)
151
    {
152
        $contestModel=new ContestModel();
153
        $problemModel=new ProblemModel();
154
        $compilerModel=new CompilerModel();
155
        $submissionModel=new SubmissionModel();
156
        $accountModel=new AccountModel();
157
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
158
        if (!$clearance) {
159
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
160
        }
161
        $contest_name=$contestModel->contestName($cid);
162
        $contest_rule=$contestModel->rule($cid);
163
        $contest_ended=$contestModel->isContestEnded($cid);
164
        $pid=$contestModel->getPid($cid, $ncode);
165
        if (empty($pid)) {
166
            return Redirect::route('contest.board', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
167
        }
168
        $pcode=$problemModel->pcode($pid);
169
170
        $prob_detail=$problemModel->detail($pcode, $cid);
171
        if ($problemModel->isBlocked($prob_detail["pid"], $cid)) {
172
            return abort('403');
0 ignored issues
show
Bug introduced by
Are you sure the usage of abort('403') 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...
Bug Best Practice introduced by
The expression return abort('403') returns the type void which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
173
        }
174
        $compiler_list=$compilerModel->list($prob_detail["OJ"], $prob_detail["pid"]);
175
        $prob_status=$submissionModel->getProblemStatus($prob_detail["pid"], Auth::user()->id, $cid);
176
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
177
        $compiler_pref=$compilerModel->pref($compiler_list, $prob_detail["pid"], Auth::user()->id, $cid);
178
        $pref=$compiler_pref["pref"];
179
        $submit_code=$compiler_pref["code"];
180
        $oj_detail=$problemModel->ojdetail($prob_detail["OJ"]);
181
182
        if (empty($prob_status)) {
183
            $prob_status=[
184
                "verdict"=>"NOT SUBMIT",
185
                "color"=>""
186
            ];
187
        }
188
189
        $editor_left_width = $accountModel->getExtra(Auth::user()->id, 'editor_left_width');
190
        if(empty($editor_left_width)) $editor_left_width='40';
191
192
        return view('contest.board.editor', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...=> $editor_left_width)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
193
            'page_title'=>"Problem Detail",
194
            'navigation' => "Contest",
195
            'site_title'=>$contest_name,
196
            'contest_name'=>$contest_name,
197
            'cid'=> $cid,
198
            'detail' => $prob_detail,
199
            'compiler_list' => $compiler_list,
200
            'status' => $prob_status,
201
            'pref' => $pref<0 ? 0 : $pref,
202
            'submit_code' => $submit_code,
203
            'contest_mode' => true,
204
            'contest_ended' => $contest_ended,
205
            'ncode' => $ncode,
206
            'contest_rule' => $contest_rule,
207
            'problem_set' => $problemSet,
208
            'clearance' => $clearance,
209
            'oj_detail' => $oj_detail,
210
            'editor_left_width'=>$editor_left_width,
211
        ]);
212
    }
213
214
    /**
215
     * Show the Contest Rank Page.
216
     *
217
     * @return Response
218
     */
219
    public function rank($cid)
220
    {
221
        $contestModel=new ContestModel();
222
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
223
        if (!$clearance) {
224
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
225
        }
226
        $contest_name=$contestModel->contestName($cid);
227
        $contest_rule=$contestModel->contestRule($cid);
228
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
229
        $customInfo=$contestModel->getCustomInfo($cid);
230
        $contestRank=$contestModel->contestRank($cid, Auth::user()->id);
231
        $rankFrozen=$contestModel->isFrozen($cid);
232
        $frozenTime=$contestModel->frozenTime($cid);
233
        $basicInfo=$contestModel->basic($cid);
234
        return view('contest.board.rank', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...'basic' => $basicInfo)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
235
            'page_title'=>"Challenge",
236
            'navigation' => "Contest",
237
            'site_title'=>$contest_name,
238
            'contest_name'=>$contest_name,
239
            'contest_rule'=>$contest_rule,
240
            'cid'=>$cid,
241
            'problem_set'=>$problemSet,
242
            'custom_info' => $customInfo,
243
            'contest_rank' => $contestRank,
244
            'rank_frozen' => $rankFrozen,
245
            'frozen_time' => $frozenTime,
246
            'clearance'=> $clearance,
247
            'basic'=>$basicInfo,
248
        ]);
249
    }
250
251
    /**
252
     * Show the Contest Status Page.
253
     *
254
     * @return Response
255
     */
256
    public function status($cid)
257
    {
258
        $contestModel=new ContestModel();
259
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
260
        if (!$clearance) {
261
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
262
        }
263
        $contest_name=$contestModel->contestName($cid);
264
        $customInfo=$contestModel->getCustomInfo($cid);
265
        $basicInfo=$contestModel->basic($cid);
266
        $submissionRecordSet=$contestModel->getContestRecord($cid);
267
        $rankFrozen=$contestModel->isFrozen($cid);
268
        $frozenTime=$contestModel->frozenTime($cid);
269
        return view('contest.board.status', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...arance' => $clearance)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
270
            'page_title'=>"Status",
271
            'navigation' => "Contest",
272
            'site_title'=>$contest_name,
273
            'contest_name'=>$contest_name,
274
            'basic_info'=>$basicInfo,
275
            'cid'=>$cid,
276
            'custom_info' => $customInfo,
277
            'submission_record' => $submissionRecordSet,
278
            'rank_frozen' => $rankFrozen,
279
            'frozen_time' => $frozenTime,
280
            'clearance'=> $clearance,
281
        ]);
282
    }
283
284
    /**
285
     * Show the Contest Clarification Page.
286
     *
287
     * @return Response
288
     */
289
    public function clarification($cid)
290
    {
291
        $contestModel=new ContestModel();
292
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
293
        if (!$clearance) {
294
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
295
        }
296
        $basicInfo=$contestModel->basic($cid);
297
        $contest_name=$contestModel->contestName($cid);
298
        $customInfo=$contestModel->getCustomInfo($cid);
299
        $clarificationList=$contestModel->getClarificationList($cid);
300
        $contest_ended=$contestModel->isContestEnded($cid);
301
        return view('contest.board.clarification', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...'basic' => $basicInfo)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
302
            'page_title'=>"Clarification",
303
            'navigation' => "Contest",
304
            'site_title'=>$contest_name,
305
            'contest_name'=>$contest_name,
306
            'cid'=>$cid,
307
            'custom_info' => $customInfo,
308
            'clarification_list' => $clarificationList,
309
            'contest_ended' => $contest_ended,
310
            'clearance'=> $clearance,
311
            'basic'=>$basicInfo,
312
        ]);
313
    }
314
315
    /**
316
     * Show the Contest Print Page.
317
     *
318
     * @return Response
319
     */
320
    public function print($cid)
321
    {
322
        $contestModel=new ContestModel();
323
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
324
        if (!$clearance) {
325
            return Redirect::route('contest.detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
326
        }
327
        $basicInfo=$contestModel->basic($cid);
328
        $contest_name=$contestModel->contestName($cid);
329
        $customInfo=$contestModel->getCustomInfo($cid);
330
        return view('contest.board.print', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...'basic' => $basicInfo)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
331
            'page_title'=>"Print",
332
            'navigation' => "Contest",
333
            'site_title'=>$contest_name,
334
            'contest_name'=>$contest_name,
335
            'cid'=>$cid,
336
            'custom_info' => $customInfo,
337
            'clearance'=> $clearance,
338
            'basic'=>$basicInfo,
339
        ]);
340
    }
341
342
    /**
343
     * Show the Contest Admin Page.
344
     *
345
     * @return Response
346
     */
347
    public function admin($cid)
348
    {
349
        $contestModel=new ContestModel();
350
        $verified=$contestModel->isVerified($cid);
351
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
352
        if ($clearance <= 2) {
353
            return Redirect::route('contest_detail', ['cid' => $cid]);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Redirect::route('..., array('cid' => $cid)) returns the type Illuminate\Http\RedirectResponse which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
354
        }
355
        $contest_name=$contestModel->contestName($cid);
356
        $customInfo=$contestModel->getCustomInfo($cid);
357
        $accountModel=new AccountModel();
358
        $basicInfo=$contestModel->basic($cid);
359
        $contest_accounts=$accountModel->getContestAccount($cid);
360
        $gcode=$contestModel->gcode($cid);
361
        return view('contest.board.admin', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa...'basic' => $basicInfo)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Response.
Loading history...
362
            'page_title'=>"Admin",
363
            'navigation' => "Contest",
364
            'site_title'=>$contest_name,
365
            'contest_name'=>$contest_name,
366
            'cid'=>$cid,
367
            'custom_info' => $customInfo,
368
            'clearance'=> $clearance,
369
            'contest_accounts'=>$contest_accounts,
370
            'verified'=>$verified,
371
            'gcode'=>$gcode,
372
            'basic'=>$basicInfo,
373
        ]);
374
    }
375
376
    public function downloadContestAccountXlsx($cid)
377
    {
378
        $contestModel=new ContestModel();
379
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
380
        if ($clearance <= 2) {
381
            return Redirect::route('contest_detail', ['cid' => $cid]);
382
        }
383
        $account=$contestModel->getContestAccount($cid);
384
        if($account==null){
385
            return ;
386
        }else{
387
            $AccountExport=new AccountExport($account);
388
            $filename="ContestAccount$cid";
389
            return Excel::download($AccountExport, $filename.'.xlsx');
390
        }
391
    }
392
393
    public function analysis($cid){
394
        $contestModel=new ContestModel();
395
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
396
        if (!$clearance) {
397
            return Redirect::route('contest.detail', ['cid' => $cid]);
398
        }
399
        $contest_name=$contestModel->contestName($cid);
400
        $customInfo=$contestModel->getCustomInfo($cid);
401
        $basicInfo=$contestModel->basic($cid);
402
        return view('contest.board.analysis', [
403
            'page_title'=>"Analysis",
404
            'navigation' => "Contest",
405
            'site_title'=>$contest_name,
406
            'contest_name'=>$contest_name,
407
            'cid'=>$cid,
408
            'custom_info' => $customInfo,
409
            'clearance'=> $clearance,
410
            'basic'=>$basicInfo,
411
        ]);
412
    }
413
}
414