1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Torralbodavid\DuckFunkCore\Exceptions\Welcome; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Contracts\Validation\Validator; |
7
|
|
|
use Torralbodavid\DuckFunkCore\Models\Arcturus\User; |
8
|
|
|
|
9
|
|
|
class UserCheck extends Exception |
10
|
|
|
{ |
11
|
|
|
protected $validator; |
12
|
|
|
|
13
|
|
|
protected $code = 422; |
14
|
|
|
|
15
|
|
|
public function __construct(Validator $validator) |
16
|
|
|
{ |
17
|
|
|
$this->validator = $validator; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function render() |
21
|
|
|
{ |
22
|
|
View Code Duplication |
if ($this->validator->errors()->first() == 'validation.min.string') { |
|
|
|
|
23
|
|
|
return response()->json([ |
|
|
|
|
24
|
|
|
'code' => 'INVALID_NAME', |
25
|
|
|
'validationResult' => [ |
26
|
|
|
'resultType' => 'VALIDATION_ERROR_NAME_TOO_SHORT', |
27
|
|
|
'additionalInfo' => 2, |
28
|
|
|
'valid' => false, |
29
|
|
|
], |
30
|
|
|
'suggestions' => [User::randomNickname()], |
31
|
|
|
]); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
View Code Duplication |
if ($this->validator->errors()->first() == 'validation.max.string') { |
|
|
|
|
35
|
|
|
return response()->json([ |
36
|
|
|
'code' => 'INVALID_NAME', |
37
|
|
|
'validationResult' => [ |
38
|
|
|
'resultType' => 'VALIDATION_ERROR_NAME_TOO_LONG', |
39
|
|
|
'additionalInfo' => 15, |
40
|
|
|
'valid' => false, |
41
|
|
|
], |
42
|
|
|
'suggestions' => [User::randomNickname()], |
43
|
|
|
]); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
View Code Duplication |
if ($this->validator->errors()->first() == 'validation.regex') { |
|
|
|
|
47
|
|
|
return response()->json([ |
48
|
|
|
'code' => 'INVALID_NAME', |
49
|
|
|
'validationResult' => [ |
50
|
|
|
'resultType' => 'VALIDATION_ERROR_ILLEGAL_CHARS', |
51
|
|
|
'additionalInfo' => null, |
52
|
|
|
'valid' => false, |
53
|
|
|
], |
54
|
|
|
'suggestions' => [], |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($this->validator->errors()->first() == 'validation.user.taken') { |
59
|
|
|
return response()->json([ |
60
|
|
|
'code' => 'NAME_IN_USE', |
61
|
|
|
'validationResult' => null, |
62
|
|
|
'suggestions' => [User::randomNickname()], |
63
|
|
|
]); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
View Code Duplication |
if ($this->validator->errors()->first() == 'validation.user.illegal.words') { |
|
|
|
|
67
|
|
|
return response()->json([ |
68
|
|
|
'code' => 'INVALID_NAME', |
69
|
|
|
'validationResult' => [ |
70
|
|
|
'resultType' => 'VALIDATION_ERROR_ILLEGAL_WORDS', |
71
|
|
|
'additionalInfo' => '', |
72
|
|
|
'valid' => false, |
73
|
|
|
], |
74
|
|
|
'suggestions' => [User::randomNickname()], |
75
|
|
|
]); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return response()->json([ |
79
|
|
|
'code' => 'INVALID_NAME', |
80
|
|
|
'validationResult' => [ |
81
|
|
|
'resultType' => 'VALIDATION_ERROR_UNKNOWN', |
82
|
|
|
'additionalInfo' => null, |
83
|
|
|
'valid' => false, |
84
|
|
|
], |
85
|
|
|
'suggestions' => [], |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
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.