JudgerService::getJudger()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace App\Services;
4
5
use App\Entities\Judger;
6
use App\Exceptions\Judger\JudgerNameExist;
7
use App\Repositories\JudgerRepository;
8
9
class JudgerService
10
{
11
    /**
12
     * @param string $name
13
     * @param null   $bind_ip
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $bind_ip is correct as it would always require null to be passed?
Loading history...
14
     *
15
     * @throws \App\Exceptions\Judger\JudgerNameExist
16
     *
17
     * @return \App\Entities\Judger
18
     */
19
    public function newJudger($name, $bind_ip = null)
20
    {
21
        if ($this->exist($name)) {
22
            throw new JudgerNameExist();
23
        }
24
        $judger = new Judger();
25
        $judger->name = $name;
26
        $judger->code = new_judge_code();
27
        $judger->status = Judger::ST_ACTIVITY;
28
        if ($bind_ip) {
0 ignored issues
show
introduced by
$bind_ip is of type null, thus it always evaluated to false.
Loading history...
29
            $judger->bind_ip = $bind_ip;
30
        }
31
        $judger->save();
32
33
        return $judger;
34
    }
35
36
    private function exist($name)
37
    {
38
        return app(JudgerRepository::class)->findBy('name', $name);
39
    }
40
41
    public function find($id)
42
    {
43
        return app(JudgerRepository::class)->findOrFail($id);
44
    }
45
46
    /**
47
     * @param $code
48
     *
49
     * @return Judger
50
     */
51
    public function getJudger($code)
52
    {
53
        if ($code) {
54
            return app(JudgerRepository::class)->findBy('code', $code);
55
        }
56
57
        return null;
58
    }
59
}
60