@@ -24,49 +24,47 @@ class HttpExceptionHandler
2424 /**
2525 * Transforms response to an exception if possible.
2626 *
27- * @param RequestInterface $request Request of the call
28- * @param ResponseInterface $response Response of the call
29- *
30- * @throws BadRequestHttpException If response status code is a 400
3127 * @throws UnauthorizedHttpException If response status code is a 401
3228 * @throws NotFoundHttpException If response status code is a 404
3329 * @throws UnprocessableEntityHttpException If response status code is a 422
3430 * @throws ClientErrorHttpException If response status code is a 4xx
3531 * @throws ServerErrorHttpException If response status code is a 5xx
3632 *
37- * @return ResponseInterface
33+ * @throws BadRequestHttpException If response status code is a 400
3834 */
39- public function transformResponseToException (RequestInterface $ request , ResponseInterface $ response ): ResponseInterface
40- {
41- if ($ response ->getStatusCode () >= 300 && $ response ->getStatusCode () < 400 ) {
35+ public function transformResponseToException (
36+ RequestInterface $ request ,
37+ ResponseInterface $ response
38+ ): ResponseInterface {
39+ if ($ this ->isSuccessStatusCode ($ response ->getStatusCode ())) {
40+ return $ response ;
41+ }
42+
43+ if ($ this ->isRedirectionStatusCode ($ response ->getStatusCode ())) {
4244 throw new RedirectionHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
4345 }
4446
45- if (400 === $ response ->getStatusCode ()) {
47+ if (HttpClient:: HTTP_BAD_REQUEST === $ response ->getStatusCode ()) {
4648 throw new BadRequestHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
4749 }
4850
49- if (401 === $ response ->getStatusCode ()) {
51+ if (HttpClient:: HTTP_UNAUTHORIZED === $ response ->getStatusCode ()) {
5052 throw new UnauthorizedHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
5153 }
5254
53- if (404 === $ response ->getStatusCode ()) {
55+ if (HttpClient:: HTTP_NOT_FOUND === $ response ->getStatusCode ()) {
5456 throw new NotFoundHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
5557 }
5658
57- if (422 === $ response ->getStatusCode ()) {
59+ if (HttpClient:: HTTP_UNPROCESSABLE_ENTITY === $ response ->getStatusCode ()) {
5860 throw new UnprocessableEntityHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
5961 }
6062
61- if ($ response -> getStatusCode () >= 400 && $ response ->getStatusCode () < 500 ) {
63+ if ($ this -> isApiClientErrorStatusCode ( $ response ->getStatusCode ()) ) {
6264 throw new ClientErrorHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
6365 }
6466
65- if ($ response ->getStatusCode () >= 500 && $ response ->getStatusCode () < 600 ) {
66- throw new ServerErrorHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
67- }
68-
69- return $ response ;
67+ throw new ServerErrorHttpException ($ this ->getResponseMessage ($ response ), $ request , $ response );
7068 }
7169
7270 /**
@@ -86,4 +84,23 @@ protected function getResponseMessage(ResponseInterface $response): string
8684
8785 return isset ($ decodedBody ['message ' ]) ? $ decodedBody ['message ' ] : $ response ->getReasonPhrase ();
8886 }
87+
88+ private function isSuccessStatusCode (int $ statusCode ): bool
89+ {
90+ return in_array ($ statusCode , [
91+ HttpClient::HTTP_OK ,
92+ HttpClient::HTTP_CREATED ,
93+ HttpClient::HTTP_NO_CONTENT ,
94+ ]);
95+ }
96+
97+ private function isApiClientErrorStatusCode (int $ statusCode ): bool
98+ {
99+ return $ statusCode >= 400 && $ statusCode < 500 ;
100+ }
101+
102+ private function isRedirectionStatusCode (int $ statusCode ): bool
103+ {
104+ return $ statusCode >= 300 && $ statusCode < 400 ;
105+ }
89106}
0 commit comments