Test Setup Failed
Push — master ( 942174...72d971 )
by he
05:46
created

SolutionController::index()   B

Complexity

Conditions 7
Paths 24

Size

Total Lines 32
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 1
Metric Value
cc 7
eloc 18
c 3
b 1
f 1
nc 24
nop 1
dl 0
loc 32
rs 8.8333
1
<?php
2
3
namespace App\Http\Controllers\Web;
4
5
use App\Entities\Problem;
6
use App\Http\Controllers\Controller;
7
use App\Http\Requests\Solution\IndexRequest;
8
use App\Repositories\Criteria\OrderBy;
9
use App\Repositories\Criteria\Where;
10
use App\Repositories\ProblemRepository;
11
use App\Repositories\SolutionRepository;
12
use App\Services\UserService;
13
use App\Status;
14
use App\Task\SolutionServer;
15
use Czim\Repository\Criteria\Common\WithRelations;
16
17
class SolutionController extends Controller
18
{
19
    public function index(IndexRequest $request)
20
    {
21
        /** @var SolutionRepository $repository */
22
        $repository = app(SolutionRepository::class);
23
24
        if ($request->getUserName()) {
25
            $user = app(UserService::class)->findByName($request->getUserName());
26
            if ($user != null) {
27
                $repository->pushCriteria(new Where('user_id', $user->id));
28
            }
29
        }
30
31
        if ($request->getProblemId()) {
32
            $repository->pushCriteria(new Where('problem_id', $request->getProblemId()));
33
        }
34
35
        if ($request->getLanguage() != -1) {
36
            $filter = new Where('language', $request->getLanguage());
37
            $repository->pushCriteria($filter);
38
        }
39
40
        if ($request->getStatus() != -1 && $request->getStatus()) {
41
            $filter = new Where('result', $request->getStatus());
42
            $repository->pushCriteria($filter);
43
        }
44
45
        $per_page = 100;
46
        $repository->pushCriteria(new WithRelations(['user']));
47
        $repository->pushCriteria(new OrderBy('id', 'desc'));
48
        $solutions = $repository->paginate($per_page);
49
50
        return view('web.solution.index')->with('solutions', $solutions);
51
    }
52
53
    public function create($id)
54
    {
55
        if (! auth()->user()) {
56
            return redirect(route('problem.view', ['problem' => $id]))->withErrors(__('Login first'));
57
        }
58
59
        /** @var Problem $problem */
60
        $problem = app(ProblemRepository::class)->findOrFail($id);
61
62
        if (! config('hustoj.special_judge_enabled') && $problem->isSpecialJudge()) {
63
            return redirect(route('problem.view', ['problem' => $id]))
64
                ->withErrors(__('Special judge current disabled!'));
65
        }
66
67
        return view('web.problem.submit', ['problem' => $problem]);
68
    }
69
70
    public function store()
71
    {
72
        $data = [
73
            'user_id'     => app('auth')->guard()->id(),
74
            'problem_id'  => request('problem_id', 0),
75
            'language'    => request('language'),
76
            'ip'          => request()->ip(),
77
            'order'       => request('order', 0),
78
            'contest_id'  => request('contest_id', 0),
79
            'code_length' => strlen(request('code')),
0 ignored issues
show
Bug introduced by
It seems like request('code') can also be of type array; however, parameter $string of strlen() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
            'code_length' => strlen(/** @scrutinizer ignore-type */ request('code')),
Loading history...
80
            'result'      => Status::PENDING,
81
        ];
82
83
        /** @var SolutionRepository $repository */
84
        $repository = app(SolutionRepository::class);
85
        /** @var \App\Entities\Solution $solution */
86
        $solution = $repository->create($data);
87
        $solution->source()->create([
88
            'code' => request('code', ''),
89
        ]);
90
91
        app(SolutionServer::class)->add($solution)->send();
92
93
        return redirect(route('solution.index'));
94
    }
95
96
    public function source($id)
97
    {
98
        /** @var \App\Entities\Solution $solution */
99
        $solution = app(SolutionRepository::class)->findOrFail($id);
100
        if (! can_view_code($solution)) {
101
            return redirect(route('solution.index'))->withErrors(__('You have no permission access solution source'));
102
        }
103
104
        return view('web.solution.source')->with('solution', $solution);
105
    }
106
107
    public function compileInfo($id)
108
    {
109
        /** @var \App\Entities\Solution $solution */
110
        $solution = app(SolutionRepository::class)->findOrFail($id);
111
        if (! can_view_code($solution)) {
112
            return back()->withErrors(__('You cannot access this solution'));
113
        }
114
115
        return view('web.solution.compile_info')->with('solution', $solution);
116
    }
117
118
    public function runtimeInfo($id)
119
    {
120
        /** @var \App\Entities\Solution $solution */
121
        $solution = app(SolutionRepository::class)->findOrFail($id);
122
        if (! can_view_code($solution)) {
123
            return back()->withErrors(__('You cannot access this solution'));
124
        }
125
126
        return view('web.solution.runtime_info')->with('solution', $solution);
127
    }
128
}
129