Completed
Push — master ( d868f1...86ce89 )
by Sherif
02:54
created

ErrorHandler::__construct()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 8.9197
cc 4
eloc 15
nc 4
nop 0
1
<?php namespace App\Modules\V1\Core\Utl;
2
3
class ErrorHandler
4
{
5
6
    /**
7
     * Array of error messags.
8
     */
9
    private $messages = [
10
        "en" => [
11
            "unAuthorized"           => "Please login before any action",
12
            "tokenExpired"           => "Login token expired",
13
            "noPermissions"          => "No permissions",
14
            "loginFailed"            => "Wrong mail or password",
15
            "redisNotRunning"        => "Your redis notification server isn't running",
16
            "dbQueryError"           => "Please check the given inputes",
17
            "cannotCreateSetting"    => "Can't create setting",
18
            "cannotUpdateSettingKey" => "Can't update setting key",
19
            "userIsBlocked"          => "You have been blocked",
20
            "invalidResetToken"      => "Reset password token is invalid",
21
            "invalidResetPassword"   => "Reset password is invalid",
22
            "notFound"               => "The requested {replace} not found",
23
            "generalError"           => "Something went wrong",
24
        ],
25
        "ar" => [
26
            "unAuthorized"           => "من فضلك قم بتسجيل الدخول",
27
            "tokenExpired"           => "انتهت صلاحية الدخول",
28
            "noPermissions"          => "لا توجد صلاحية",
29
            "loginFailed"            => "خطأ في البريد لاكتروني او كلمة المرور",
30
            "redisNotRunning"        => "سيرفير الاشعارات لايعمل",
31
            "dbQueryError"           => "خطا في البيانات",
32
            "cannotCreateSetting"    => "لا يمكن اضافة اعدادات",
33
            "cannotUpdateSettingKey" => "لا يمكن تعديل اعدادات",
34
            "userIsBlocked"          => "لقد تم حظرك",
35
            "invalidResetToken"      => "رمز تعديل كلمة المرور خطا",
36
            "invalidResetPassword"   => "خطا في نعديل كلمة المرور",
37
            "notFound"               => "ال {replace} المطلوب غير موجود",
38
            "generalError"           => "حدث خطا ما",
39
        ]
40
    ];
41
42
    /**
43
     * The locale language.
44
     */
45
    private $locale;
46
47
    public function __construct()
48
    {
49
        $locale = \Session::get('locale');
50
        switch ($locale) 
51
        {
52
            case 'en':
53
            $this->locale = 'en';
54
            break;
55
56
            case 'ar':
57
            $this->locale = 'ar';
58
            break;
59
60
            case 'all':
61
            $this->locale = 'en';
62
            break;
63
64
            default:
65
            $this->locale = 'en';
66
            break;
67
        }
68
    }
69
70
    public function unAuthorized()
71
    {
72
        $error = ['status' => 401, 'message' => $this->messages[$this->locale]['unAuthorized']];
73
        abort($error['status'], $error['message']);
74
    }
75
76
    public function tokenExpired()
77
    {
78
        $error = ['status' => 403, 'message' => $this->messages[$this->locale]['tokenExpired']];
79
        abort($error['status'], $error['message']);
80
    }
81
82
     public function noPermissions()
83
    {
84
        $error = ['status' => 403, 'message' => $this->messages[$this->locale]['noPermissions']];
85
        abort($error['status'], $error['message']);
86
    }
87
88
    public function loginFailed()
89
    {
90
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['loginFailed']];
91
        abort($error['status'], $error['message']);
92
    }
93
94
    public function redisNotRunning()
95
    {
96
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['redisNotRunning']];
97
        abort($error['status'], $error['message']);
98
    }
99
100
    public function dbQueryError()
101
    {
102
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['dbQueryError']];
103
        abort($error['status'], $error['message']);
104
    }
105
106
    public function cannotCreateSetting()
107
    {
108
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['cannotCreateSetting']];
109
        abort($error['status'], $error['message']);
110
    }
111
112
    public function cannotUpdateSettingKey()
113
    {
114
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['cannotUpdateSettingKey']];
115
        abort($error['status'], $error['message']);
116
    }
117
118
    public function userIsBlocked()
119
    {
120
        $error = ['status' => 403, 'message' => $this->messages[$this->locale]['userIsBlocked']];
121
        abort($error['status'], $error['message']);
122
    }
123
124
    public function invalidResetToken()
125
    {
126
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['invalidResetToken']];
127
        abort($error['status'], $error['message']);
128
    }
129
130
    public function invalidResetPassword()
131
    {
132
        $error = ['status' => 400, 'message' => $this->messages[$this->locale]['invalidResetPassword']];
133
        abort($error['status'], $error['message']);
134
    }
135
136
    public function notFound($text)
137
    {
138
        $error = ['status' => 404, 'message' => str_replace($this->messages[$this->locale]['notFound'], '{replace}', $text)];
139
        abort($error['status'], $error['message']);
140
    }
141
142
    public function generalError()
143
    {
144
        $error = ['status' => 404, 'message' => $this->messages[$this->locale]['generalError']];
145
        abort($error['status'], $error['message']);
146
    }
147
}