Test Setup Failed
Push — master ( 60f940...0da644 )
by he
04:47
created

ContestManager::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 3
dl 0
loc 4
rs 10
c 1
b 0
f 0
1
<?php
2
3
namespace App\Services\Contest;
4
5
use App\Entities\Contest;
6
use Carbon\Carbon;
7
8
class ContestManager
9
{
10
    /**
11
     * @param array $attrs
12
     * @param string $startAt
13
     * @param string $endAt
14
     *
15
     * @return \App\Entities\Contest
16
     */
17
    public function create($attrs, $startAt, $endAt) {
18
        $model = new Contest();
19
20
        return $this->update($model, $attrs, $startAt, $endAt);
21
    }
22
23
    /**
24
     * @param Contest $contest
25
     * @param array $attrs
26
     * @param string $startAt
27
     * @param string $endAt
28
     *
29
     * @return \App\Entities\Contest
30
     */
31
    public function update($contest, $attrs, $startAt, $endAt) {
32
        $contest->fill($attrs);
33
        $contest->start_time = Carbon::parse($startAt);
34
        $contest->end_time = Carbon::parse($endAt);
35
36
        $contest->save();
37
38
        return $contest;
39
    }
40
41
    /**
42
     * @param Contest $contest
43
     * @param array $problemIds
44
     */
45
    public function syncProblems($contest, $problemIds) {
46
        $relations = [];
47
        $index = 0;
48
        foreach ($problemIds as $id) {
49
            $relations[$id] = ['order' => $index];
50
            $index++;
51
        }
52
        $contest->problems()->sync($relations);
53
    }
54
55
    /**
56
     * @param Contest $contest
57
     * @param array $userIds
58
     */
59
    public function syncUser($contest, $userIds) {
60
        $contest->users()->sync($userIds);
61
    }
62
}
63