1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anetwork\Respond; |
4
|
|
|
|
5
|
|
|
class Messages extends Main |
6
|
|
|
{ |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Request succeeded and contains json result |
10
|
|
|
* @param array $data |
11
|
|
|
* @author Shima Payro <[email protected]> |
12
|
|
|
* @since May 2, 2016 9:50:51 AM |
13
|
|
|
* @uses |
14
|
|
|
* @see |
15
|
|
|
*/ |
16
|
|
|
public function succeed( $data ) { |
17
|
|
|
|
18
|
|
|
return $this->setStatusCode( 200 ) |
19
|
|
|
->setStatusText( 'success' ) |
20
|
|
|
->respondWithResult( $data ); |
21
|
|
|
|
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Delete action is succeed |
26
|
|
|
* @author Shima Payro <[email protected]> |
27
|
|
|
* @param String $message |
28
|
|
|
* @since May 2, 2016 9:52:05 AM |
29
|
|
|
* @uses |
30
|
|
|
* @see |
31
|
|
|
*/ |
32
|
|
View Code Duplication |
public function deleteSucceeded( $message = null ) { |
|
|
|
|
33
|
|
|
|
34
|
|
|
if ( is_null( $message ) ) |
35
|
|
|
$message = $this->config[ 'success' ][ 'delete' ]; |
36
|
|
|
|
37
|
|
|
return $this->setStatusCode( 200 ) |
38
|
|
|
->setStatusText( 'success' ) |
39
|
|
|
->respondWithMessage( $message ); |
40
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Update action is succeed |
45
|
|
|
* @author Shima Payro <[email protected]> |
46
|
|
|
* @param String $message |
47
|
|
|
* @since May 2, 2016 9:52:52 AM |
48
|
|
|
* @uses |
49
|
|
|
* @see |
50
|
|
|
*/ |
51
|
|
View Code Duplication |
public function updateSucceeded( $message = null ) { |
|
|
|
|
52
|
|
|
|
53
|
|
|
if ( is_null( $message ) ) |
54
|
|
|
$message = $this->config[ 'success' ][ 'update' ]; |
55
|
|
|
|
56
|
|
|
return $this->setStatusCode( 200 ) |
57
|
|
|
->setStatusText( 'success' ) |
58
|
|
|
->respondWithMessage( $message ); |
59
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Insert action is succeed |
64
|
|
|
* @author Shima Payro <[email protected]> |
65
|
|
|
* @param String $message |
66
|
|
|
* @since May 2, 2016 9:53:26 AM |
67
|
|
|
* @uses |
68
|
|
|
* @see |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function insertSucceeded( $message = null ) { |
|
|
|
|
71
|
|
|
|
72
|
|
|
if ( is_null( $message ) ) |
73
|
|
|
$message = $this->config[ 'success' ][ 'insert' ]; |
74
|
|
|
|
75
|
|
|
return $this->setStatusCode( 200 ) |
76
|
|
|
->setStatusText( 'success' ) |
77
|
|
|
->respondWithMessage( $message ); |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Delete action is faild |
83
|
|
|
* @author Shima Payro <[email protected]> |
84
|
|
|
* @param String $message |
85
|
|
|
* @since May 2, 2016 9:53:53 AM |
86
|
|
|
* @uses |
87
|
|
|
* @see |
88
|
|
|
*/ |
89
|
|
View Code Duplication |
public function deleteFaild( $message = null ) { |
|
|
|
|
90
|
|
|
|
91
|
|
|
if ( is_null( $message ) ) |
92
|
|
|
$message = $this->config[ 'fail' ][ 'delete' ]; |
93
|
|
|
|
94
|
|
|
return $this->setStatusCode( 447 ) |
95
|
|
|
->setStatusText( 'fail' ) |
96
|
|
|
->setErrorCode( 5447 ) |
97
|
|
|
->respondWithMessage( $message ); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Update action is succeed |
103
|
|
|
* @author Shima Payro <[email protected]> |
104
|
|
|
* @param String $message |
105
|
|
|
* @since May 2, 2016 9:54:09 AM |
106
|
|
|
* @uses |
107
|
|
|
* @see |
108
|
|
|
*/ |
109
|
|
View Code Duplication |
public function updateFaild( $message = null ) { |
|
|
|
|
110
|
|
|
|
111
|
|
|
if ( is_null( $message ) ) |
112
|
|
|
$message = $this->config[ 'fail' ][ 'update' ]; |
113
|
|
|
|
114
|
|
|
return $this->setStatusCode( 449 ) |
115
|
|
|
->setStatusText( 'fail' ) |
116
|
|
|
->setErrorCode( 5449 ) |
117
|
|
|
->respondWithMessage( $message ); |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Insert action is faild |
123
|
|
|
* @author Shima Payro <[email protected]> |
124
|
|
|
* @param String $message |
125
|
|
|
* @since May 2, 2016 9:54:27 AM |
126
|
|
|
* @uses |
127
|
|
|
* @see |
128
|
|
|
*/ |
129
|
|
View Code Duplication |
public function insertFaild( $message = null ) { |
|
|
|
|
130
|
|
|
|
131
|
|
|
if ( is_null( $message ) ) |
132
|
|
|
$message = $this->config[ 'fail' ][ 'insert' ]; |
133
|
|
|
|
134
|
|
|
return $this->setStatusCode( 448 ) |
135
|
|
|
->setStatusText( 'fail' ) |
136
|
|
|
->setErrorCode( 5448 ) |
137
|
|
|
->respondWithMessage( $message ); |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Database connection is refused |
143
|
|
|
* @author Shima Payro <[email protected]> |
144
|
|
|
* @param String $message |
|
|
|
|
145
|
|
|
* @since May 2, 2016 9:54:45 AM |
146
|
|
|
* @uses |
147
|
|
|
* @see |
148
|
|
|
*/ |
149
|
|
|
public function connectionRefused() { |
150
|
|
|
|
151
|
|
|
return $this->setStatusCode( 445 ) |
152
|
|
|
->setStatusText( 'fail' ) |
153
|
|
|
->setErrorCode( 5445 ) |
154
|
|
|
->respondWithMessage(); |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Redis connection is refused |
160
|
|
|
* |
161
|
|
|
* @author Alireza Josheghani <[email protected]> |
162
|
|
|
* @since 21 Jan 2017 |
163
|
|
|
*/ |
164
|
|
|
public function redisConnectionRefused() |
165
|
|
|
{ |
166
|
|
|
return $this->setStatusCode( 445 ) |
167
|
|
|
->setStatusText( 'fail' ) |
168
|
|
|
->setErrorCode( 5445 ) |
169
|
|
|
->respondWithMessage(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/** |
173
|
|
|
* page requested is not found |
174
|
|
|
* @author Shima Payro <[email protected]> |
175
|
|
|
* @param String $message |
|
|
|
|
176
|
|
|
* @since May 2, 2016 9:55:20 AM |
177
|
|
|
* @uses |
178
|
|
|
* @see |
179
|
|
|
*/ |
180
|
|
|
public function notFound() { |
181
|
|
|
|
182
|
|
|
return $this->setStatusCode( 404 ) |
183
|
|
|
->setStatusText( 'fail' ) |
184
|
|
|
->setErrorCode( 5404 ) |
185
|
|
|
->respondWithMessage(); |
186
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* Wrong parameters are entered |
191
|
|
|
* @author Shima Payro <[email protected]> |
192
|
|
|
* @param String $message |
|
|
|
|
193
|
|
|
* @since May 2, 2016 9:55:20 AM |
194
|
|
|
* @uses |
195
|
|
|
* @see |
196
|
|
|
*/ |
197
|
|
|
public function wrongParameters() { |
198
|
|
|
|
199
|
|
|
return $this->setStatusCode( 406 ) |
200
|
|
|
->setStatusText( 'fail' ) |
201
|
|
|
->setErrorCode( 5406 ) |
202
|
|
|
->respondWithMessage(); |
203
|
|
|
|
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* Method is not allowed |
208
|
|
|
* @author Shima Payro <[email protected]> |
209
|
|
|
* @param String $message |
|
|
|
|
210
|
|
|
* @since May 2, 2016 9:55:20 AM |
211
|
|
|
* @uses |
212
|
|
|
* @see |
213
|
|
|
*/ |
214
|
|
|
public function methodNotAllowed() { |
215
|
|
|
|
216
|
|
|
return $this->setStatusCode( 405 ) |
217
|
|
|
->setStatusText( 'fail' ) |
218
|
|
|
->setErrorCode( 5405 ) |
219
|
|
|
->respondWithMessage(); |
220
|
|
|
|
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* There ara validation errors |
225
|
|
|
* @author Shima Payro <[email protected]> |
226
|
|
|
* @param $data array |
227
|
|
|
* @since May 2, 2016 9:55:20 AM |
228
|
|
|
* @uses |
229
|
|
|
* @see |
230
|
|
|
*/ |
231
|
|
|
public function validationErrors( $message = null ) { |
232
|
|
|
|
233
|
|
|
return $this->setStatusCode( 420 ) |
234
|
|
|
->setStatusText( 'fail' ) |
235
|
|
|
->setErrorCode( 5420 ) |
236
|
|
|
->respondWithResult( $message ); |
237
|
|
|
|
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* The request field is not found |
242
|
|
|
* @author Shima Payro <[email protected]> |
243
|
|
|
* @param String $message |
|
|
|
|
244
|
|
|
* @since May 2, 2016 9:55:20 AM |
245
|
|
|
* @uses |
246
|
|
|
* @see |
247
|
|
|
*/ |
248
|
|
|
public function requestFieldNotFound() { |
249
|
|
|
|
250
|
|
|
return $this->setStatusCode( 446 ) |
251
|
|
|
->setStatusText( 'fail' ) |
252
|
|
|
->setErrorCode( 1001 ) |
253
|
|
|
->respondWithMessage(); |
254
|
|
|
|
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* The request field is doublicated |
259
|
|
|
* @author Mehdi Hosseini <[email protected]> |
260
|
|
|
* @since August 24, 2016 |
261
|
|
|
* @return mixed |
262
|
|
|
*/ |
263
|
|
|
public function requestFieldDuplicated() { |
264
|
|
|
|
265
|
|
|
return $this->setStatusCode( 400 ) |
266
|
|
|
->setStatusText( 'fail' ) |
267
|
|
|
->SetErrorCode(1004) |
268
|
|
|
->respondWithMessage(); |
269
|
|
|
|
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
/** |
273
|
|
|
* Custom error message according to error config file |
274
|
|
|
* @author Mehdi Hosseini <[email protected]> |
275
|
|
|
* @since August 24, 2016 |
276
|
|
|
* @param $code integer |
277
|
|
|
* @return mixed |
278
|
|
|
*/ |
279
|
|
|
public function error( $code ) { |
280
|
|
|
|
281
|
|
|
return $this->setStatusCode( 400 ) |
282
|
|
|
->setStatusText( 'fail' ) |
283
|
|
|
->setErrorCode( $code ) |
284
|
|
|
->respondWithMessage(); |
285
|
|
|
|
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
|
289
|
|
|
} |
290
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.