Skip to content

Commit 40e9bd1

Browse files
committed
refractor: optimize code
1 parent fff02bf commit 40e9bd1

1 file changed

Lines changed: 53 additions & 24 deletions

File tree

src/Http/Client.php

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use GuzzleHttp\Exception\GuzzleException;
1818
use OpenLRW\Exception\ExpiredJwtException;
1919
use OpenLRW\Exception\GenericException;
20+
use OpenLRW\Exception\InternalServerErrorException;
2021
use OpenLRW\Exception\NotFoundException;
2122

2223

@@ -42,12 +43,20 @@ public function get(string $route, array $header = null)
4243
try {
4344
return json_decode(self::$http->request('GET', $route, ['headers' => $header])->getBody()->getContents());
4445
} catch (GuzzleException $e) {
45-
if ($e->getCode() === 401) {
46-
throw new ExpiredJwtException($e->getMessage());
47-
} else if ($e->getCode() === 404) {
48-
throw new NotFoundException($e->getMessage());
46+
switch ($e->getCode()) {
47+
case 401:
48+
throw new ExpiredJwtException($e->getMessage());
49+
break;
50+
case 404:
51+
throw new NotFoundException($e->getMessage());
52+
break;
53+
case 500:
54+
throw new InternalServerErrorException($e->getMessage());
55+
break;
56+
default:
57+
throw new GenericException($e->getMessage());
58+
break;
4959
}
50-
throw new GenericException($e->getMessage());
5160
}
5261

5362
}
@@ -66,15 +75,20 @@ public function patch(string $route, array $header, $json)
6675
try {
6776
return self::$http->patch($route, ['headers' => $header, 'body' => $json])->getStatusCode();
6877
} catch (GuzzleException $e) {
69-
if ($e->getCode() === 401) {
70-
throw new ExpiredJwtException($e->getMessage());
78+
switch ($e->getCode()) {
79+
case 401:
80+
throw new ExpiredJwtException($e->getMessage());
81+
break;
82+
case 404:
83+
throw new NotFoundException($e->getMessage());
84+
break;
85+
case 500:
86+
throw new InternalServerErrorException($e->getMessage());
87+
break;
88+
default:
89+
throw new GenericException($e->getMessage());
90+
break;
7191
}
72-
73-
if ($e->getCode() === 404) {
74-
return null;
75-
}
76-
77-
throw new GenericException($e->getMessage());
7892
}
7993
}
8094

@@ -92,11 +106,20 @@ public function post(string $route, array $json, array $header = ['X-Requested-W
92106
try {
93107
return json_decode(self::$http->request('POST', $route, ['headers' => $header, 'json' => $json])->getBody()->getContents());
94108
} catch (GuzzleException $e) {
95-
if ($e->getCode() === 401) {
96-
throw new ExpiredJwtException($e->getMessage());
109+
switch ($e->getCode()) {
110+
case 401:
111+
throw new ExpiredJwtException($e->getMessage());
112+
break;
113+
case 404:
114+
throw new NotFoundException($e->getMessage());
115+
break;
116+
case 500:
117+
throw new InternalServerErrorException($e->getMessage());
118+
break;
119+
default:
120+
throw new GenericException($e->getMessage());
121+
break;
97122
}
98-
99-
throw new GenericException($e->getMessage());
100123
}
101124
}
102125

@@ -113,14 +136,20 @@ public function delete(String $route, array $header)
113136
try {
114137
return self::$http->delete($route, ['headers' => $header])->getStatusCode();
115138
} catch (GuzzleException $e) {
116-
if ($e->getCode() === 401) {
117-
throw new ExpiredJwtException($e->getMessage());
118-
}
119-
120-
if ($e->getCode() === 404) {
121-
return null;
139+
switch ($e->getCode()) {
140+
case 401:
141+
throw new ExpiredJwtException($e->getMessage());
142+
break;
143+
case 404:
144+
throw new NotFoundException($e->getMessage());
145+
break;
146+
case 500:
147+
throw new InternalServerErrorException($e->getMessage());
148+
break;
149+
default:
150+
throw new GenericException($e->getMessage());
151+
break;
122152
}
123-
throw new GenericException($e->getMessage());
124153
}
125154
}
126155

0 commit comments

Comments
 (0)