Completed
Push — master ( 5021ae...be2f55 )
by he
08:00
created

SolutionController::create()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
            $repository->pushCriteria(new Where('user_id', $user->id));
27
        }
28
29
        if ($request->getProblemId()) {
30
            $repository->pushCriteria(new Where('problem_id', $request->getProblemId()));
31
        }
32
33
        if ($request->getLanguage() != -1) {
34
            $filter = new Where('language', $request->getLanguage());
35
            $repository->pushCriteria($filter);
36
        }
37
38
        if ($request->getStatus() != -1 && $request->getStatus()) {
39
            $filter = new Where('result', $request->getStatus());
40
            $repository->pushCriteria($filter);
41
        }
42
43
        $per_page = 100;
44
        $repository->pushCriteria(new WithRelations(['user']));
45
        $repository->pushCriteria(new OrderBy('id', 'desc'));
46
        $solutions = $repository->paginate($per_page);
47
48
        return view('web.solution.index')->with('solutions', $solutions);
49
    }
50
51
    public function create($id)
52
    {
53
        /** @var Problem $problem */
54
        $problem = app(ProblemRepository::class)->findOrFail($id);
55
        if (!auth()->user()) {
56
            return redirect(route('problem.view', ['problem' => $problem->id]))->withErrors(__('Login first'));
57
        }
58
59
        return view('web.problem.submit', ['problem' => $problem]);
60
    }
61
62
    public function store()
63
    {
64
        $data = [
65
            'user_id'     => app('auth')->guard()->id(),
66
            'problem_id'  => request('problem_id', 0),
67
            'language'    => request('language'),
68
            'ip'          => request()->ip(),
69
            'order'       => request('order', 0),
70
            'contest_id'  => request('contest_id', 0),
71
            '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

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