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

JudgerService::newJudger()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 10
c 1
b 0
f 1
nc 3
nop 2
dl 0
loc 15
rs 9.9332
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
    public function find($id)
37
    {
38
        return app(JudgerRepository::class)->findOrFail($id);
39
    }
40
    /**
41
     * @param $code
42
     *
43
     * @return Judger
44
     */
45
    public function getJudger($code)
46
    {
47
        if ($code) {
48
            return app(JudgerRepository::class)->findBy('code', $code);
49
        }
50
51
        return null;
52
    }
53
54
    private function exist($name)
55
    {
56
        return app(JudgerRepository::class)->findBy('name', $name);
57
    }
58
}
59