Completed
Push — master ( e6516a...3fa133 )
by John
17s queued 10s
created

BoardController   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 313
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 215
dl 0
loc 313
rs 9.0399
c 1
b 0
f 0
wmc 42

8 Methods

Rating   Name   Duplication   Size   Complexity  
A board() 0 3 1
B status() 0 35 7
A clarification() 0 27 4
B editor() 0 66 9
B rank() 0 46 8
A print() 0 23 4
A challenge() 0 34 5
A analysis() 0 22 4

How to fix   Complexity   

Complex Class

Complex classes like BoardController 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 BoardController, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
namespace App\Http\Controllers\Contest;
4
5
use App\Models\ContestModel;
6
use App\Models\ProblemModel;
7
use App\Models\CompilerModel;
8
use App\Models\Submission\SubmissionModel;
9
use App\Models\AccountModel;
10
use App\Http\Controllers\Controller;
11
use Illuminate\Http\Request;
12
use Auth;
13
use Redirect;
14
15
class BoardController extends Controller
16
{
17
    /**
18
     * Redirect the Contest Board Page.
19
     *
20
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Http\Controllers\Contest\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
21
     */
22
    public function board($cid)
23
    {
24
        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\Contest\Response.
Loading history...
25
    }
26
27
    /**
28
     * Show the Contest Challenge Page.
29
     *
30
     * @return Response
31
     */
32
    public function challenge($cid)
33
    {
34
        $contestModel=new ContestModel();
35
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
36
        $basicInfo=$contestModel->basic($cid);
37
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
38
            if($clearance == 3){
39
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
40
            }else{
41
                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\Contest\Response.
Loading history...
42
            }
43
        }
44
        $contest_name=$contestModel->contestName($cid);
45
        $contest_rule=$contestModel->contestRule($cid);
46
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
47
        $remainingTime=$contestModel->remainingTime($cid);
48
        $customInfo=$contestModel->getCustomInfo($cid);
49
        $clarificationList=$contestModel->getLatestClarification($cid);
50
        if ($remainingTime<=0) {
51
            $remainingTime=0;
52
        }
53
        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\Contest\Response.
Loading history...
54
            'page_title'=>"Challenge",
55
            'navigation' => "Contest",
56
            'site_title'=>$contest_name,
57
            'cid'=>$cid,
58
            'contest_name'=>$contest_name,
59
            'contest_rule'=>$contest_rule,
60
            'problem_set'=> $problemSet,
61
            'remaining_time'=>$remainingTime,
62
            'custom_info' => $customInfo,
63
            'clarification_list' => $clarificationList,
64
            'clearance'=> $clearance,
65
            'basic'=>$basicInfo,
66
        ]);
67
    }
68
69
    /**
70
     * Show the Contest Editor Page.
71
     *
72
     * @return Response
73
     */
74
    public function editor($cid, $ncode)
75
    {
76
        $contestModel=new ContestModel();
77
        $problemModel=new ProblemModel();
78
        $compilerModel=new CompilerModel();
79
        $submissionModel=new SubmissionModel();
80
        $accountModel=new AccountModel();
81
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
82
        $basicInfo=$contestModel->basic($cid);
83
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
84
            if($clearance == 3){
85
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
86
            }else{
87
                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\Contest\Response.
Loading history...
88
            }
89
        }
90
        $contest_name=$contestModel->contestName($cid);
91
        $contest_rule=$contestModel->rule($cid);
92
        $contest_ended=$contestModel->isContestEnded($cid);
93
        $pid=$contestModel->getPid($cid, $ncode);
94
        if (empty($pid)) {
95
            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\Contest\Response.
Loading history...
96
        }
97
        $pcode=$problemModel->pcode($pid);
98
99
        $prob_detail=$problemModel->detail($pcode, $cid);
100
        if ($problemModel->isBlocked($prob_detail["pid"], $cid)) {
101
            return abort('403');
0 ignored issues
show
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\Contest\Response.
Loading history...
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...
102
        }
103
        $compiler_list=$compilerModel->list($prob_detail["OJ"], $prob_detail["pid"]);
104
        $prob_status=$submissionModel->getProblemStatus($prob_detail["pid"], Auth::user()->id, $cid);
105
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
106
        $compiler_pref=$compilerModel->pref($compiler_list, $prob_detail["pid"], Auth::user()->id, $cid);
107
        $pref=$compiler_pref["pref"];
108
        $submit_code=$compiler_pref["code"];
109
        $oj_detail=$problemModel->ojdetail($prob_detail["OJ"]);
110
111
        if (empty($prob_status)) {
112
            $prob_status=[
113
                "verdict"=>"NOT SUBMIT",
114
                "color"=>""
115
            ];
116
        }
117
118
        $editor_left_width = $accountModel->getExtra(Auth::user()->id, 'editor_left_width');
119
        if(empty($editor_left_width)) $editor_left_width='40';
120
121
        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\Contest\Response.
Loading history...
122
            'page_title'=>"Problem Detail",
123
            'navigation' => "Contest",
124
            'site_title'=>$contest_name,
125
            'contest_name'=>$contest_name,
126
            'cid'=> $cid,
127
            'detail' => $prob_detail,
128
            'compiler_list' => $compiler_list,
129
            'status' => $prob_status,
130
            'pref' => $pref<0 ? 0 : $pref,
131
            'submit_code' => $submit_code,
132
            'contest_mode' => true,
133
            'contest_ended' => $contest_ended,
134
            'ncode' => $ncode,
135
            'contest_rule' => $contest_rule,
136
            'problem_set' => $problemSet,
137
            'clearance' => $clearance,
138
            'oj_detail' => $oj_detail,
139
            'editor_left_width'=>$editor_left_width,
140
        ]);
141
    }
142
143
    /**
144
     * Show the Contest Rank Page.
145
     *
146
     * @return Response
147
     */
148
    public function rank($cid)
149
    {
150
        $contestModel=new ContestModel();
151
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
152
        $basicInfo=$contestModel->basic($cid);
153
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
154
            if($clearance == 3){
155
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
156
            }else{
157
                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\Contest\Response.
Loading history...
158
            }
159
        }
160
        $contest_name=$contestModel->contestName($cid);
161
        $contest_rule=$contestModel->contestRule($cid);
162
        $problemSet=$contestModel->contestProblems($cid, Auth::user()->id);
163
        $customInfo=$contestModel->getCustomInfo($cid);
164
        $contestRank=$contestModel->contestRank($cid, Auth::user()->id);
165
166
        // To determine the ranking
167
        foreach ($contestRank as $i => &$r) {
168
            if($i != 0) {
169
                if($r['score'] == $contestRank[$i-1]['score'] && $r['penalty'] == $contestRank[$i-1]['penalty']){
170
                    $r['rank'] = $contestRank[$i-1]['rank'];
171
                }else{
172
                    $r['rank'] = $i + 1;
173
                }
174
            }else{
175
                $r['rank'] = 1;
176
            }
177
        }
178
        $rankFrozen=$contestModel->isFrozen($cid);
179
        $frozenTime=$contestModel->frozenTime($cid);
180
        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\Contest\Response.
Loading history...
181
            'page_title'=>"Rank",
182
            'navigation' => "Contest",
183
            'site_title'=>$contest_name,
184
            'contest_name'=>$contest_name,
185
            'contest_rule'=>$contest_rule,
186
            'cid'=>$cid,
187
            'problem_set'=>$problemSet,
188
            'custom_info' => $customInfo,
189
            'contest_rank' => $contestRank,
190
            'rank_frozen' => $rankFrozen,
191
            'frozen_time' => $frozenTime,
192
            'clearance'=> $clearance,
193
            'basic'=>$basicInfo,
194
        ]);
195
    }
196
197
    /**
198
     * Show the Contest Status Page.
199
     *
200
     * @return Response
201
     */
202
    public function status(Request $request)
203
    {
204
        $all_data=$request->all();
205
        $filter["ncode"]=isset($all_data["ncode"]) ? $all_data["ncode"] : 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...
206
        $filter["result"]=isset($all_data["result"]) ? $all_data["result"] : null;
207
        $filter["account"]=isset($all_data["account"]) ? $all_data["account"] : null;
208
        $cid=$request->cid;
209
        $contestModel=new ContestModel();
210
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
211
        $basicInfo=$contestModel->basic($cid);
212
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
213
            if($clearance == 3){
214
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
215
            }else{
216
                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\Contest\Response.
Loading history...
217
            }
218
        }
219
        $contest_name=$contestModel->contestName($cid);
220
        $customInfo=$contestModel->getCustomInfo($cid);
221
        $submissionRecordSet=$contestModel->getContestRecord($filter, $cid);
222
        $rankFrozen=$contestModel->isFrozen($cid);
223
        $frozenTime=$contestModel->frozenTime($cid);
224
        return view('contest.board.status', [
0 ignored issues
show
Bug Best Practice introduced by
The expression return view('contest.boa..., 'filter' => $filter)) returns the type Illuminate\View\View which is incompatible with the documented return type App\Http\Controllers\Contest\Response.
Loading history...
225
            'page_title'=>"Status",
226
            'navigation' => "Contest",
227
            'site_title'=>$contest_name,
228
            'contest_name'=>$contest_name,
229
            'basic_info'=>$basicInfo,
230
            'cid'=>$cid,
231
            'custom_info' => $customInfo,
232
            'submission_record' => $submissionRecordSet,
233
            'rank_frozen' => $rankFrozen,
234
            'frozen_time' => $frozenTime,
235
            'clearance'=> $clearance,
236
            'filter' => $filter,
237
        ]);
238
    }
239
240
    /**
241
     * Show the Contest Clarification Page.
242
     *
243
     * @return Response
244
     */
245
    public function clarification($cid)
246
    {
247
        $contestModel=new ContestModel();
248
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
249
        $basicInfo=$contestModel->basic($cid);
250
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
251
            if($clearance == 3){
252
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
253
            }else{
254
                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\Contest\Response.
Loading history...
255
            }
256
        }
257
        $contest_name=$contestModel->contestName($cid);
258
        $customInfo=$contestModel->getCustomInfo($cid);
259
        $clarificationList=$contestModel->getClarificationList($cid);
260
        $contest_ended=$contestModel->isContestEnded($cid);
261
        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\Contest\Response.
Loading history...
262
            'page_title'=>"Clarification",
263
            'navigation' => "Contest",
264
            'site_title'=>$contest_name,
265
            'contest_name'=>$contest_name,
266
            'cid'=>$cid,
267
            'custom_info' => $customInfo,
268
            'clarification_list' => $clarificationList,
269
            'contest_ended' => $contest_ended,
270
            'clearance'=> $clearance,
271
            'basic'=>$basicInfo,
272
        ]);
273
    }
274
275
    /**
276
     * Show the Contest Print Page.
277
     *
278
     * @return Response
279
     */
280
    public function print($cid)
281
    {
282
        $contestModel=new ContestModel();
283
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
284
        $basicInfo=$contestModel->basic($cid);
285
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
286
            if($clearance == 3){
287
                return Redirect::route('contest.admin', ['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\Contest\Response.
Loading history...
288
            }else{
289
                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\Contest\Response.
Loading history...
290
            }
291
        }
292
        $contest_name=$contestModel->contestName($cid);
293
        $customInfo=$contestModel->getCustomInfo($cid);
294
        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\Contest\Response.
Loading history...
295
            'page_title'=>"Print",
296
            'navigation' => "Contest",
297
            'site_title'=>$contest_name,
298
            'contest_name'=>$contest_name,
299
            'cid'=>$cid,
300
            'custom_info' => $customInfo,
301
            'clearance'=> $clearance,
302
            'basic'=>$basicInfo,
303
        ]);
304
    }
305
306
    public function analysis($cid){
307
        $contestModel=new ContestModel();
308
        $clearance=$contestModel->judgeClearance($cid, Auth::user()->id);
309
        $basicInfo=$contestModel->basic($cid);
310
        if (!$clearance || time() < strtotime($basicInfo['begin_time'])) {
311
            if($clearance == 3){
312
                return Redirect::route('contest.admin', ['cid' => $cid]);
313
            }else{
314
                return Redirect::route('contest.detail', ['cid' => $cid]);
315
            }
316
        }
317
        $contest_name=$contestModel->contestName($cid);
318
        $customInfo=$contestModel->getCustomInfo($cid);
319
        return view('contest.board.analysis', [
320
            'page_title'=>"Analysis",
321
            'navigation' => "Contest",
322
            'site_title'=>$contest_name,
323
            'contest_name'=>$contest_name,
324
            'cid'=>$cid,
325
            'custom_info' => $customInfo,
326
            'clearance'=> $clearance,
327
            'basic'=>$basicInfo,
328
        ]);
329
    }
330
}
331