1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the FreshSinchBundle |
4
|
|
|
* |
5
|
|
|
* (c) Artem Genvald <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Fresh\SinchBundle\Service; |
12
|
|
|
|
13
|
|
|
use Fresh\SinchBundle\Exception\BadRequest\SinchInvalidRequestException; |
14
|
|
|
use Fresh\SinchBundle\Exception\BadRequest\SinchMissingParameterException; |
15
|
|
|
use Fresh\SinchBundle\Exception\InternalServerError\SinchForbiddenRequestException; |
16
|
|
|
use Fresh\SinchBundle\Exception\InternalServerError\SinchInternalErrorException; |
17
|
|
|
use Fresh\SinchBundle\Exception\InternalServerError\SinchInvalidAuthorizationSchemeException; |
18
|
|
|
use Fresh\SinchBundle\Exception\InternalServerError\SinchNoVerifiedPhoneNumberException; |
19
|
|
|
use Fresh\SinchBundle\Exception\InternalServerError\SinchParameterValidationException; |
20
|
|
|
use Fresh\SinchBundle\Exception\PaymentRequired\SinchPaymentRequiredException; |
21
|
|
|
use Fresh\SinchBundle\Exception\Unauthorized\SinchIllegalAuthorizationHeaderException; |
22
|
|
|
use Fresh\SinchBundle\Helper\SinchErrorCode; |
23
|
|
|
use GuzzleHttp\Exception\ClientException; |
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SinchExceptionResolver |
28
|
|
|
* |
29
|
|
|
* @author Artem Genvald <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class SinchExceptionResolver |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* Create appropriate Sinch exception |
35
|
|
|
* |
36
|
|
|
* @param ClientException $e Exception |
37
|
|
|
* |
38
|
|
|
* @return \Exception|\Fresh\SinchBundle\Exception\SinchException |
39
|
|
|
*/ |
40
|
|
|
public static function createAppropriateSinchException(ClientException $e) |
41
|
|
|
{ |
42
|
|
|
$response = json_decode($e->getResponse()->getBody()->getContents(), true); |
43
|
|
|
$responseStatusCode = $e->getCode(); |
44
|
|
|
|
45
|
|
|
$errorCode = (int) $response['errorCode']; |
46
|
|
|
$errorMessage = $response['message']; |
47
|
|
|
|
48
|
|
|
$exception = null; |
49
|
|
|
|
50
|
|
|
switch ($responseStatusCode) { |
51
|
|
|
case Response::HTTP_BAD_REQUEST: |
52
|
|
|
$exception = self::getSinchExceptionForBadRequest($errorCode, $errorMessage); |
53
|
|
|
break; |
54
|
|
|
case Response::HTTP_UNAUTHORIZED: |
55
|
|
|
$exception = self::getSinchExceptionForUnauthorized($errorCode, $errorMessage); |
56
|
|
|
break; |
57
|
|
|
case Response::HTTP_PAYMENT_REQUIRED: |
58
|
|
|
$exception = self::getSinchExceptionForPaymentRequired($errorCode, $errorMessage); |
59
|
|
|
break; |
60
|
|
|
case Response::HTTP_FORBIDDEN: |
61
|
|
|
$exception = self::getSinchExceptionForForbidden($errorCode, $errorMessage); |
62
|
|
|
break; |
63
|
|
|
case Response::HTTP_INTERNAL_SERVER_ERROR: |
64
|
|
|
$exception = self::getSinchExceptionForInternalServerError($errorCode, $errorMessage); |
65
|
|
|
break; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
if (null === $exception) { |
69
|
|
|
$exception = new \Exception('Unknown Sinch Error Code'); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $exception; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Get Sinch exception for bad request |
77
|
|
|
* |
78
|
|
|
* @param int $errorCode Sinch error code |
79
|
|
|
* @param string $errorMessage Sinch error message |
80
|
|
|
* |
81
|
|
|
* @return \Fresh\SinchBundle\Exception\SinchException|null |
82
|
|
|
*/ |
83
|
|
View Code Duplication |
private static function getSinchExceptionForBadRequest($errorCode, $errorMessage) |
|
|
|
|
84
|
|
|
{ |
85
|
|
|
$exception = null; |
86
|
|
|
|
87
|
|
|
switch ($errorCode) { |
88
|
|
|
case SinchErrorCode::PARAMETER_VALIDATION: |
89
|
|
|
$exception = new SinchParameterValidationException($errorMessage); |
90
|
|
|
break; |
91
|
|
|
case SinchErrorCode::MISSING_PARAMETER: |
92
|
|
|
$exception = new SinchMissingParameterException($errorMessage); |
93
|
|
|
break; |
94
|
|
|
case SinchErrorCode::INVALID_REQUEST: |
95
|
|
|
$exception = new SinchInvalidRequestException($errorMessage); |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $exception; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get Sinch exception for unauthorized |
104
|
|
|
* |
105
|
|
|
* @param int $errorCode Sinch error code |
106
|
|
|
* @param string $errorMessage Sinch error message |
107
|
|
|
* |
108
|
|
|
* @return \Fresh\SinchBundle\Exception\SinchException|null |
109
|
|
|
*/ |
110
|
|
View Code Duplication |
private static function getSinchExceptionForUnauthorized($errorCode, $errorMessage) |
|
|
|
|
111
|
|
|
{ |
112
|
|
|
$exception = null; |
113
|
|
|
|
114
|
|
|
if (SinchErrorCode::ILLEGAL_AUTHORIZATION_HEADER === $errorCode) { |
115
|
|
|
$exception = new SinchIllegalAuthorizationHeaderException($errorMessage); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $exception; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get Sinch exception for payment required |
123
|
|
|
* |
124
|
|
|
* Sinch returns 402 code when application run out of money |
125
|
|
|
* |
126
|
|
|
* @param int $errorCode Sinch error code |
127
|
|
|
* @param string $errorMessage Sinch error message |
128
|
|
|
* |
129
|
|
|
* @return \Fresh\SinchBundle\Exception\SinchException|null |
130
|
|
|
*/ |
131
|
|
View Code Duplication |
private static function getSinchExceptionForPaymentRequired($errorCode, $errorMessage) |
|
|
|
|
132
|
|
|
{ |
133
|
|
|
$exception = null; |
134
|
|
|
|
135
|
|
|
if (SinchErrorCode::THERE_IS_NOT_ENOUGH_FUNDS_TO_SEND_THE_MESSAGE === $errorCode) { |
136
|
|
|
$exception = new SinchPaymentRequiredException($errorMessage); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
return $exception; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Get Sinch exception for forbidden |
144
|
|
|
* |
145
|
|
|
* @param int $errorCode Sinch error code |
146
|
|
|
* @param string $errorMessage Sinch error message |
147
|
|
|
* |
148
|
|
|
* @return \Fresh\SinchBundle\Exception\SinchException|null |
149
|
|
|
*/ |
150
|
|
View Code Duplication |
private static function getSinchExceptionForForbidden($errorCode, $errorMessage) |
|
|
|
|
151
|
|
|
{ |
152
|
|
|
$exception = null; |
153
|
|
|
|
154
|
|
|
switch ($errorCode) { |
155
|
|
|
case SinchErrorCode::FORBIDDEN_REQUEST: |
156
|
|
|
$exception = new SinchForbiddenRequestException($errorMessage); |
157
|
|
|
break; |
158
|
|
|
case SinchErrorCode::INVALID_AUTHORIZATION_SCHEME_FOR_CALLING_THE_METHOD: |
159
|
|
|
$exception = new SinchInvalidAuthorizationSchemeException($errorMessage); |
160
|
|
|
break; |
161
|
|
|
case SinchErrorCode::NO_VERIFIED_PHONE_NUMBER_ON_YOUR_SINCH_ACCOUNT: |
162
|
|
|
case SinchErrorCode::SANDBOX_SMS_ONLY_ALLOWED_TO_BE_SENT_TO_VERIFIED_NUMBERS: |
163
|
|
|
$exception = new SinchNoVerifiedPhoneNumberException($errorMessage); |
164
|
|
|
break; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $exception; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Get Sinch exception for internal server error |
172
|
|
|
* |
173
|
|
|
* @param int $errorCode Sinch error code |
174
|
|
|
* @param string $errorMessage Sinch error message |
175
|
|
|
* |
176
|
|
|
* @return \Fresh\SinchBundle\Exception\SinchException|null |
177
|
|
|
*/ |
178
|
|
View Code Duplication |
private static function getSinchExceptionForInternalServerError($errorCode, $errorMessage) |
|
|
|
|
179
|
|
|
{ |
180
|
|
|
$exception = null; |
181
|
|
|
|
182
|
|
|
if (SinchErrorCode::INTERNAL_ERROR === $errorCode) { |
183
|
|
|
$exception = new SinchInternalErrorException($errorMessage); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
return $exception; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// endregion |
190
|
|
|
} |
191
|
|
|
|
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.