Test Setup Failed
Push — master ( 53259e...8b572c )
by he
07:31
created

can_view_code()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 3
nop 1
dl 0
loc 10
rs 10
1
<?php
2
3
use App\Entities\Contest;
4
use App\Entities\Option;
5
use App\Entities\Solution;
6
use App\Entities\User;
7
use App\Services\ContestService;
8
use Carbon\Carbon;
9
use Illuminate\Database\Eloquent\Collection;
10
use Ramsey\Uuid\Uuid;
11
12
if (!function_exists('new_judge_code')) {
13
    function new_judge_code()
14
    {
15
        return Uuid::getFactory()->uuid4();
16
    }
17
}
18
19
if (!function_exists('get_option')) {
20
    function get_option($name, $default = null)
21
    {
22
        /** @var Collection|Option[] $options */
23
        $options = Option::all();
24
        foreach ($options as $option) {
25
            if ($option->key === $name && $option->value) {
26
                return $option->value;
27
            }
28
        }
29
30
        return $default;
31
    }
32
}
33
34
if (!function_exists('can_attend')) {
35
    /**
36
     * @param Contest $contest
37
     *
38
     * @return bool
39
     */
40
    function can_attend($contest)
41
    {
42
        /** @var User $user */
43
        $user = auth()->user();
44
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
45
            return false;
46
        }
47
        if ($user->hasRole('admin')) {
48
            return true;
49
        }
50
51
        return $contest->users()->wherePivot('user_id', '=', $user->id)->get()->count();
52
    }
53
}
54
55
if (!function_exists('can_view_code')) {
56
    function can_view_code($solution) {
57
        /** @var User $user */
58
        $user = auth()->user();
59
        if (!$user) {
0 ignored issues
show
introduced by
$user is of type App\Entities\User, thus it always evaluated to true.
Loading history...
60
            return false;
61
        }
62
        if ($user->hasRole('admin')) {
63
            return true;
64
        }
65
        return $solution->user_id == $user->id;
66
    }
67
}
68
69
if (!function_exists('show_ratio')) {
70
    function show_ratio($number, $total)
71
    {
72
        $result = 0;
73
        if ($total > 0) {
74
            $result = 100.0 * $number / $total;
75
        }
76
77
        return sprintf('%2.2f%%', $result);
78
    }
79
}
80
81
if (!function_exists('display_penalize_time')) {
82
    function display_penalize_time($seconds)
83
    {
84
        $hour = (int) ($seconds / (Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR));
85
        $seconds -= $hour * Carbon::SECONDS_PER_MINUTE * Carbon::MINUTES_PER_HOUR;
86
        $minute = (int) ($seconds / Carbon::SECONDS_PER_MINUTE);
87
        $leftSeconds = $seconds % Carbon::SECONDS_PER_MINUTE;
88
89
        return sprintf('%d:%02d:%02d', $hour, $minute, $leftSeconds);
90
    }
91
}
92
93
if (!function_exists('show_order')) {
94
    function show_order($order)
95
    {
96
        return chr($order + ord('A'));
97
    }
98
}
99
100
if (!function_exists('show_problem_id')) {
101
    /**
102
     * @param Solution $solution
103
     *
104
     * @return int|string
105
     */
106
    function show_problem_id($solution)
107
    {
108
        if ($solution->contest_id) {
109
            return show_order($solution->order);
110
        }
111
112
        return $solution->problem_id;
113
    }
114
}
115
116
if (!function_exists('opening_contest')) {
117
    function opening_contest()
118
    {
119
        return (new ContestService())->openingContest();
120
    }
121
}
122
123
if (!function_exists('original_order')) {
124
    function original_order($order)
125
    {
126
        return ord($order) - ord('A');
127
    }
128
}
129
130
if (!function_exists('!show_status')) {
131
    function show_status($status)
132
    {
133
        if (array_key_exists($status, Solution::$status)) {
134
            return Solution::$status[$status];
135
        }
136
137
        return 'not found!';
138
    }
139
}
140