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

ContestController::updateOrSave()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 17
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 23
rs 9.7

1 Method

Rating   Name   Duplication   Size   Complexity  
A ContestController::update() 0 16 4
1
<?php
2
3
namespace App\Http\Controllers\Admin;
4
5
use App\Entities\Contest;
6
use App\Repositories\ContestRepository;
7
use App\Repositories\Criteria\Like;
8
use App\Repositories\Criteria\Where;
9
use App\Services\Contest\ContestManager;
10
use Illuminate\Database\Eloquent\Model;
11
12
class ContestController extends DataController
13
{
14
    public function index()
15
    {
16
        if (request()->filled('id')) {
17
            $this->repository->pushCriteria(new Where('id', request('id')));
18
        }
19
        if (request()->filled('title')) {
20
            $this->repository->pushCriteria(new Like('title', request('title')));
21
        }
22
        if (request()->filled('private')) {
23
            $this->repository->pushCriteria(new Where('private', request('private')));
24
        }
25
26
        return parent::index();
27
    }
28
29
    public function problems($contestId)
30
    {
31
        /** @var Contest $contest */
32
        $contest = $this->repository->findOrFail($contestId);
33
34
        return $contest->problems;
35
    }
36
37
    public function users($contestId)
38
    {
39
        /** @var Contest $contest */
40
        $contest = $this->repository->findOrFail($contestId);
41
42
        return $contest->users;
43
    }
44
45
    public function store()
46
    {
47
        app('db')->transaction(function () {
48
            $manager = new ContestManager();
49
            $attrs = request()->except(['start_time', 'end_time']);
50
            $contest = $manager->create($attrs, request('start_time'), request('end_time'));
0 ignored issues
show
Bug introduced by
It seems like request('start_time') can also be of type array; however, parameter $startAt of App\Services\Contest\ContestManager::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            $contest = $manager->create($attrs, /** @scrutinizer ignore-type */ request('start_time'), request('end_time'));
Loading history...
Bug introduced by
It seems like request('end_time') can also be of type array; however, parameter $endAt of App\Services\Contest\ContestManager::create() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

50
            $contest = $manager->create($attrs, request('start_time'), /** @scrutinizer ignore-type */ request('end_time'));
Loading history...
51
            if (request()->has('problem_list')) {
52
                $manager->syncProblems($contest, request('problem_list', []));
53
            }
54
            if (request()->has('user_list')) {
55
                $manager->syncProblems($contest, request('user_list', []));
56
            }
57
        });
58
    }
59
60
    public function update($contest)
61
    {
62
        /* @var Contest $contest */
63
        if (!($contest instanceof Model)) {
0 ignored issues
show
introduced by
$contest is always a sub-type of Illuminate\Database\Eloquent\Model.
Loading history...
64
            $contest = $this->repository->findOrFail($contest);
65
        }
66
67
        app('db')->transaction(function () use ($contest) {
68
            $manager = new ContestManager();
69
            $attrs = request()->except(['start_time', 'end_time']);
70
            $contest = $manager->update($contest, $attrs, request('start_time'), request('end_time'));
0 ignored issues
show
Bug introduced by
It seems like request('end_time') can also be of type array; however, parameter $endAt of App\Services\Contest\ContestManager::update() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $contest = $manager->update($contest, $attrs, request('start_time'), /** @scrutinizer ignore-type */ request('end_time'));
Loading history...
Bug introduced by
It seems like request('start_time') can also be of type array; however, parameter $startAt of App\Services\Contest\ContestManager::update() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

70
            $contest = $manager->update($contest, $attrs, /** @scrutinizer ignore-type */ request('start_time'), request('end_time'));
Loading history...
71
            if (request()->has('problem_list')) {
72
                $manager->syncProblems($contest, request('problem_list', []));
73
            }
74
            if (request()->has('user_list')) {
75
                $manager->syncProblems($contest, request('user_list', []));
76
            }
77
        });
78
    }
79
80
    protected function getRepository()
81
    {
82
        return ContestRepository::class;
83
    }
84
}
85