Passed
Push — master ( b831b6...96f132 )
by John
06:04 queued 21s
created

ResponseModel::success()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 4
nop 3
dl 0
loc 11
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Http\Response;
8
9
class ResponseModel extends Model
10
{
11
    public static function success($statusCode=200, $desc=null, $data=null)
12
    {
13
        if (($statusCode>=1000)) {
14
            $statusCode=200;
15
        }
16
        $output=[
17
             'ret' => $statusCode,
18
            'desc' => is_null($desc) ? self::desc($statusCode) : $desc,
19
            'data' => $data
20
        ];
21
        return response()->json($output);
22
    }
23
24
    public static function err($statusCode, $desc=null, $data=null)
25
    {
26
        if (($statusCode<1000)) {
27
            $statusCode=1000;
28
        }
29
        $output=[
30
             'ret' => $statusCode,
31
            'desc' => is_null($desc) ? self::desc($statusCode) : $desc,
32
            'data' => $data
33
        ];
34
        return response()->json($output);
35
    }
36
37
    private static function desc($errCode)
38
    {
39
        $errDesc=[
40
41
            '200'  => "Successful",
42
            '201'  => "Partially Successful",
43
            '403'  => "Forbidden",
44
            '451'  => "Unavailable For Legal Reasons",
45
46
            '1000' => "Unspecified Response", /** Under normal condictions those errors shouldn't been displayed to end users
47
                                                 *  unless they attempt to do so, some submissions should be intercepted
48
                                                 *  by the frontend before the request sended
49
                                                 */
50
            '1001' => "Internal Sever Error",
51
            '1002' => "Service Currently Unavailable",
52
            '1003' => "Missing Params",
53
            '1004' => "Write/Read Permission Denied",
54
            '1005' => "Invalid File",
55
            '1006' => "Invalid length params",
56
            '1007' => "Invalid parameter passed",
57
            '1984' => "Ignorance is Strength",
58
59
            '2000' => "Account-Related Error",
60
            '2001' => "Permission Denied",
61
            '2002' => "Please Login First",
62
            '2003' => "A user with the same username already exists",
63
            '2004' => "New passwords do not match",
64
            '2005' => "Old passwords error",
65
            '2006' => "Can't find this user",
66
67
            '3000' => "Problem-Related Error",
68
            '3001' => "Problem Not Found",
69
            '3002' => "Submission Size Limit Exceed(64kb max)",
70
            '3003' => "Duplicate Problem Solution Submitted",
71
            '3004' => "Certain Problem Solution not Operatable",
72
            '3005' => "Copper", // Reserved for Copper in memory of OASIS and those who contributed a lot
73
74
            '4000' => "Contest-Related Error",
75
            '4001' => "Contest Not Found",
76
            '4002' => "Too Much Problems",
77
            '4003' => "No Need for Registration",
78
            '4004' => "Registration Ended",
79
            '4005' => "Registration Denied",
80
            '4006' => "AlreadyRegistered",
81
            '4007' => "A contest cannot be both a public and a practice contest",
82
            '4008' => "The contest is not over.",
83
            '4009' => 'Only freeze contest can join scrollboard',
84
            '4010' => 'Contest Still Under Judging.',
85
86
            '5000' => "Status-Related Error",
87
            '5001' => "Status Not Found",
88
89
            '6000' => "Submission-Related Error",
90
            '6001' => "Cannot Find Available Judgers",
91
            '6002' => "Sharing Method Not Allowed",
92
            '6003' => "No Need to Resubmit",
93
94
            '7000' => "Group-Related Error",
95
            '7001' => "Group Not Found",
96
            '7002' => "Insufficient Clearance",
97
            '7003' => "No Need to Approve",
98
            '7004' => "Group Member Not Found",
99
            '7005' => "Don't play just for fun",//gcode=="create"
100
            '7006' => "A group with the same gcode already exists",
101
            '7007' => "Group Problem Tag Exist",
102
            '7008' => "The group leader cannot leave the group",
103
104
            '8000' => "Job-Related Error",
105
            '8001' => "Job Still Running",
106
107
            '9000' => "Abuse-Related Error",
108
            '9001' => "Abuse Invalid",
109
110
            '10000' => "Dojo-Related Error",
111
            '10001' => "Dojo Cannot Complete",
112
            '10002' => "Dojo Not Found",
113
        ];
114
        return isset($errDesc[$errCode]) ? $errDesc[$errCode] : $errDesc['1000'];
115
    }
116
}
117