ContestManager   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A syncUser() 0 3 1
A create() 0 5 1
A syncProblems() 0 9 2
A update() 0 9 1
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
    {
19
        $model = new Contest();
20
21
        return $this->update($model, $attrs, $startAt, $endAt);
22
    }
23
24
    /**
25
     * @param Contest $contest
26
     * @param array $attrs
27
     * @param string $startAt
28
     * @param string $endAt
29
     *
30
     * @return \App\Entities\Contest
31
     */
32
    public function update($contest, $attrs, $startAt, $endAt)
33
    {
34
        $contest->fill($attrs);
35
        $contest->start_time = Carbon::parse($startAt);
36
        $contest->end_time = Carbon::parse($endAt);
37
38
        $contest->save();
39
40
        return $contest;
41
    }
42
43
    /**
44
     * @param Contest $contest
45
     * @param array $problemIds
46
     */
47
    public function syncProblems($contest, $problemIds)
48
    {
49
        $relations = [];
50
        $index = 0;
51
        foreach ($problemIds as $id) {
52
            $relations[$id] = ['order' => $index];
53
            $index++;
54
        }
55
        $contest->problems()->sync($relations);
56
    }
57
58
    /**
59
     * @param Contest $contest
60
     * @param array $userIds
61
     */
62
    public function syncUser($contest, $userIds)
63
    {
64
        $contest->users()->sync($userIds);
65
    }
66
}
67