ApiController::report()   B
last analyzed

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