@@ -20,7 +20,7 @@ abstract class Version {
2020 /**
2121 * @var string $version
2222 */
23- protected $ version ;
23+ public $ version ;
2424
2525 /**
2626 * @param Domain $domain
@@ -82,7 +82,7 @@ protected function exception(Response $response, string $header): TwilioExceptio
8282 $ content = $ response ->getContent ();
8383 if (\is_array ($ content )) {
8484 $ message .= isset ($ content ['message ' ]) ? ': ' . $ content ['message ' ] : '' ;
85- $ code = isset ( $ content ['code ' ]) ? $ content [ ' code ' ] : $ response ->getStatusCode ();
85+ $ code = $ content ['code ' ] ?? $ response ->getStatusCode ();
8686 $ moreInfo = $ content ['more_info ' ] ?? '' ;
8787 $ details = $ content ['details ' ] ?? [];
8888 return new RestException ($ message , $ code , $ response ->getStatusCode (), $ moreInfo , $ details );
@@ -98,22 +98,17 @@ public function fetch(string $method, string $uri,
9898 array $ params = [], array $ data = [], array $ headers = [],
9999 ?string $ username = null , ?string $ password = null ,
100100 ?int $ timeout = null ) {
101- $ response = $ this ->request (
101+ $ response = $ this ->handleException (
102102 $ method ,
103103 $ uri ,
104104 $ params ,
105105 $ data ,
106106 $ headers ,
107107 $ username ,
108108 $ password ,
109- $ timeout
109+ $ timeout ,
110+ "fetch "
110111 );
111-
112- // 3XX response codes are allowed here to allow for 307 redirect from Deactivations API.
113- if ($ response ->getStatusCode () < 200 || $ response ->getStatusCode () >= 400 ) {
114- throw $ this ->exception ($ response , 'Unable to fetch record ' );
115- }
116-
117112 return $ response ->getContent ();
118113 }
119114
@@ -134,21 +129,17 @@ public function update(string $method, string $uri,
134129 array $ params = [], array $ data = [], array $ headers = [],
135130 ?string $ username = null , ?string $ password = null ,
136131 ?int $ timeout = null ) {
137- $ response = $ this ->request (
132+ $ response = $ this ->handleException (
138133 $ method ,
139134 $ uri ,
140135 $ params ,
141136 $ data ,
142137 $ headers ,
143138 $ username ,
144139 $ password ,
145- $ timeout
140+ $ timeout ,
141+ "update "
146142 );
147-
148- if ($ response ->getStatusCode () < 200 || $ response ->getStatusCode () >= 300 ) {
149- throw $ this ->exception ($ response , 'Unable to update record ' );
150- }
151-
152143 return $ response ->getContent ();
153144 }
154145
@@ -159,23 +150,18 @@ public function delete(string $method, string $uri,
159150 array $ params = [], array $ data = [], array $ headers = [],
160151 ?string $ username = null , ?string $ password = null ,
161152 ?int $ timeout = null ): bool {
162- $ response = $ this ->request (
153+ $ this ->handleException (
163154 $ method ,
164155 $ uri ,
165156 $ params ,
166157 $ data ,
167158 $ headers ,
168159 $ username ,
169160 $ password ,
170- $ timeout
161+ $ timeout ,
162+ "delete "
171163 );
172-
173- // check for 2xx status code is already present here
174- if ($ response ->getStatusCode () < 200 || $ response ->getStatusCode () >= 300 ) {
175- throw $ this ->exception ($ response , 'Unable to delete record ' );
176- }
177-
178- return true ; // if response code is 2XX, deletion was successful
164+ return true ;
179165 }
180166
181167 public function readLimits (?int $ limit = null , ?int $ pageSize = null ): array {
@@ -219,6 +205,28 @@ public function create(string $method, string $uri,
219205 array $ params = [], array $ data = [], array $ headers = [],
220206 ?string $ username = null , ?string $ password = null ,
221207 ?int $ timeout = null ) {
208+ $ response = $ this ->handleException (
209+ $ method ,
210+ $ uri ,
211+ $ params ,
212+ $ data ,
213+ $ headers ,
214+ $ username ,
215+ $ password ,
216+ $ timeout ,
217+ "create "
218+ );
219+ return $ response ->getContent ();
220+ }
221+
222+ /**
223+ * @throws TwilioException
224+ */
225+ public function handleException (string $ method , string $ uri ,
226+ array $ params = [], array $ data = [], array $ headers = [],
227+ ?string $ username = null , ?string $ password = null ,
228+ ?int $ timeout = null , ?string $ operation = "" ): Response
229+ {
222230 $ response = $ this ->request (
223231 $ method ,
224232 $ uri ,
@@ -230,11 +238,12 @@ public function create(string $method, string $uri,
230238 $ timeout
231239 );
232240
233- if ($ response ->getStatusCode () < 200 || $ response ->getStatusCode () >= 300 ) {
234- throw $ this ->exception ($ response , 'Unable to create record ' );
241+ if ($ response ->getStatusCode () < 200 || $ response ->getStatusCode () >= 400 ) {
242+ $ exceptionHeader = 'Unable to ' . $ operation . ' record ' ;
243+ throw $ this ->exception ($ response , $ exceptionHeader );
235244 }
236245
237- return $ response-> getContent () ;
246+ return $ response ;
238247 }
239248
240249 public function getDomain (): Domain {
0 commit comments