Completed
Push — master ( 395fb2...b0764c )
by he
10:48 queued 10s
created

ApiController::report()   B

Complexity

Conditions 10
Paths 37

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 10
eloc 18
c 1
b 0
f 1
nc 37
nop 1
dl 0
loc 29
rs 7.6666

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace App\Http\Controllers\Judge;
4
5
use App\Entities\Solution;
6
use App\Exceptions\Judger\JudgerCodeInvalid;
7
use App\Http\Controllers\Controller;
8
use App\Http\Requests\Judger\JudgerRequest;
9
use App\Http\Requests\Judger\ReportRequest;
10
use App\Http\Requests\Request;
11
use App\Repositories\ProblemRepository;
12
use App\Services\DataProvider;
13
use App\Services\JudgerService;
14
use Illuminate\Contracts\Filesystem\FileNotFoundException;
15
16
class ApiController extends Controller
17
{
18
    /** @var \App\Entities\Judger */
19
    public $judger;
20
21
    /**
22
     * ApiController constructor.
23
     *
24
     * @param \App\Http\Requests\Judger\JudgerRequest $request
25
     *
26
     * @throws \App\Exceptions\Judger\JudgerCodeInvalid
27
     */
28
    public function __construct(JudgerRequest $request)
29
    {
30
        $request->validate();
31
        $this->judger = $request->getJudger();
32
    }
33
34
    public function data(Request $request)
35
    {
36
        $pid = $request->input('pid');
37
        app(ProblemRepository::class)->findOrFail($pid);
38
39
        try {
40
            /** @var DataProvider $dp */
41
            $dp = app(DataProvider::class);
42
43
            return $dp->getData($pid);
44
        } catch (\LogicException $e) {
45
            return [
46
                'message' => $e->getMessage(),
47
            ];
48
        } catch (FileNotFoundException $e) {
49
            return [
50
                'message' => $e->getMessage(),
51
            ];
52
        }
53
    }
54
55
    public function report(ReportRequest $request)
56
    {
57
        /** @var Solution $solution */
58
        $solution = Solution::query()->find($request->getSolutionId());
59
        if ($solution) {
0 ignored issues
show
introduced by
$solution is of type App\Entities\Solution, thus it always evaluated to true.
Loading history...
60
            $solution->result = $request->input('status');
61
            if ($request->input('memory_cost')) {
62
                $solution->memory_cost = $request->input('memory_cost');
63
            }
64
            if ($request->input('time_cost')) {
65
                $solution->time_cost = $request->input('time_cost');
66
            }
67
            if ($solution->isRuntimeError() && $request->input('runtime_info')) {
68
                if ($info = $solution->runtimeInfo) {
69
                    $info->delete();
70
                }
71
                $solution->runtimeInfo()->create(['content' => $request->input('runtime_info')]);
72
            }
73
            if ($solution->isCompileError() && $request->input('compile_info')) {
74
                if ($info = $solution->compileInfo) {
75
                    $info->delete();
76
                }
77
                $solution->compileInfo()->create(['content' => $request->input('compile_info')]);
78
            }
79
            $solution->save();
80
        }
81
82
        return [
83
            'code' => 0,
84
        ];
85
    }
86
87
    public function heartbeat()
88
    {
89
        return '';
90
    }
91
92
    private function beat()
93
    {
94
        $redis = app('redis');
95
        if ($redis && $this->judger) {
96
            $key = implode(':', ['judger', 'beat']);
97
            $redis->hset($key, $this->judger->id, time());
98
        }
99
    }
100
}
101