1515use GuzzleHttp \Client ;
1616use GuzzleHttp \ClientInterface ;
1717use GuzzleHttp \Exception \RequestException ;
18- use GuzzleHttp \Message \Response ;
18+ use GuzzleHttp \Message \ResponseInterface ;
1919use GuzzleHttp \Subscriber \Retry \RetrySubscriber ;
2020use Symfony \Component \EventDispatcher \EventDispatcherInterface ;
2121use Tmdb \Common \ParameterBag ;
22+ use Tmdb \Exception \NullResponseException ;
2223use Tmdb \HttpClient \Request ;
24+ use Tmdb \HttpClient \Response ;
2325
2426class GuzzleAdapter extends AbstractAdapter
2527{
@@ -28,6 +30,11 @@ class GuzzleAdapter extends AbstractAdapter
2830 */
2931 private $ client ;
3032
33+ /**
34+ * @var Request
35+ */
36+ protected $ request ;
37+
3138 public function __construct (ClientInterface $ client = null , array $ options = [])
3239 {
3340 if (null === $ client ) {
@@ -63,6 +70,8 @@ public function registerSubscribers(EventDispatcherInterface $eventDispatcher)
6370 */
6471 public function getConfiguration (Request $ request )
6572 {
73+ $ this ->request = $ request ;
74+
6675 return [
6776 'headers ' => $ request ->getHeaders ()->all (),
6877 'query ' => $ request ->getParameters ()->all ()
@@ -72,12 +81,12 @@ public function getConfiguration(Request $request)
7281 /**
7382 * Create the response object
7483 *
75- * @param Response $adapterResponse
84+ * @param ResponseInterface $adapterResponse
7685 * @return \Tmdb\HttpClient\Response
7786 */
78- private function createResponse (Response $ adapterResponse )
87+ private function createResponse (ResponseInterface $ adapterResponse = null )
7988 {
80- $ response = new \ Tmdb \ HttpClient \ Response ();
89+ $ response = new Response ();
8190
8291 $ response ->setCode ($ adapterResponse ->getStatusCode ());
8392 $ response ->setHeaders (new ParameterBag ($ adapterResponse ->getHeaders ()));
@@ -86,18 +95,40 @@ private function createResponse(Response $adapterResponse)
8695 return $ response ;
8796 }
8897
98+ /**
99+ * Create the request exception
100+ *
101+ * @param Request $request
102+ * @param RequestException|null $previousException
103+ * @throws \Tmdb\Exception\TmdbApiException
104+ */
105+ protected function handleRequestException (Request $ request , RequestException $ previousException = null )
106+ {
107+ if (null !== $ previousException && null == $ response = $ previousException ->getResponse ()) {
108+ throw new NullResponseException ($ this ->request , $ previousException );
109+ }
110+
111+ throw $ this ->createApiException (
112+ $ request ,
113+ $ this ->createResponse ($ previousException ->getResponse ()),
114+ $ previousException
115+ );
116+ }
117+
89118 /**
90119 * {@inheritDoc}
91120 */
92121 public function get (Request $ request )
93122 {
123+ $ response = null ;
124+
94125 try {
95126 $ response = $ this ->client ->get (
96127 $ request ->getPath (),
97128 $ this ->getConfiguration ($ request )
98129 );
99130 } catch (RequestException $ e ) {
100- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
131+ $ this ->handleRequestException ($ request , $ e );
101132 }
102133
103134 return $ this ->createResponse ($ response );
@@ -108,6 +139,8 @@ public function get(Request $request)
108139 */
109140 public function post (Request $ request )
110141 {
142+ $ response = null ;
143+
111144 try {
112145 $ response = $ this ->client ->post (
113146 $ request ->getPath (),
@@ -117,7 +150,7 @@ public function post(Request $request)
117150 )
118151 );
119152 } catch (RequestException $ e ) {
120- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
153+ $ this ->handleRequestException ($ request , $ e );
121154 }
122155
123156 return $ this ->createResponse ($ response );
@@ -128,6 +161,8 @@ public function post(Request $request)
128161 */
129162 public function put (Request $ request )
130163 {
164+ $ response = null ;
165+
131166 try {
132167 $ response = $ this ->client ->put (
133168 $ request ->getPath (),
@@ -137,7 +172,7 @@ public function put(Request $request)
137172 )
138173 );
139174 } catch (RequestException $ e ) {
140- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
175+ $ this ->handleRequestException ($ request , $ e );
141176 }
142177
143178 return $ this ->createResponse ($ response );
@@ -148,6 +183,8 @@ public function put(Request $request)
148183 */
149184 public function patch (Request $ request )
150185 {
186+ $ response = null ;
187+
151188 try {
152189 $ response = $ this ->client ->patch (
153190 $ request ->getPath (),
@@ -157,7 +194,7 @@ public function patch(Request $request)
157194 )
158195 );
159196 } catch (RequestException $ e ) {
160- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
197+ $ this ->handleRequestException ($ request , $ e );
161198 }
162199
163200 return $ this ->createResponse ($ response );
@@ -168,6 +205,8 @@ public function patch(Request $request)
168205 */
169206 public function delete (Request $ request )
170207 {
208+ $ response = null ;
209+
171210 try {
172211 $ response = $ this ->client ->delete (
173212 $ request ->getPath (),
@@ -177,7 +216,7 @@ public function delete(Request $request)
177216 )
178217 );
179218 } catch (RequestException $ e ) {
180- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
219+ $ this ->handleRequestException ($ request , $ e );
181220 }
182221
183222 return $ this ->createResponse ($ response );
@@ -188,13 +227,15 @@ public function delete(Request $request)
188227 */
189228 public function head (Request $ request )
190229 {
230+ $ response = null ;
231+
191232 try {
192233 $ response = $ this ->client ->head (
193234 $ request ->getPath (),
194235 $ this ->getConfiguration ($ request )
195236 );
196237 } catch (RequestException $ e ) {
197- throw $ this ->createApiException ($ request , $ this -> createResponse ( $ e -> getResponse ()) );
238+ $ this ->handleRequestException ($ request , $ e );
198239 }
199240
200241 return $ this ->createResponse ($ response );
0 commit comments