1111
1212namespace App \Service ;
1313
14+ use App \Exception \OfficialEndpointNotAvailableExtension ;
1415use GuzzleHttp \Psr7 \Request ;
1516use Http \Client \Exception \NetworkException ;
1617use Http \Client \HttpClient ;
1718use Symfony \Component \Cache \Simple \FilesystemCache ;
19+ use Symfony \Component \HttpFoundation \Response ;
1820
1921/**
2022 * Class OfficialEndpointProxy
@@ -106,6 +108,13 @@ private function getDecodedResponse(Request $request)
106108 $ response = $ this ->client ->sendRequest ($ request );
107109 $ decodedResponse = json_decode ($ response ->getBody (), true );
108110
111+ if (!in_array ($ response ->getStatusCode (), range (200 , 299 ))) {
112+ if ($ this ->cacheEndpoint && $ this ->cache ->has ($ this ->getCacheId ($ request ))) {
113+ return $ this ->cache ->get ($ this ->getCacheId ($ request ));
114+ }
115+ return [];
116+ }
117+
109118 if (json_last_error () !== JSON_ERROR_NONE ) {
110119 $ decodedResponse = $ response ->getBody ();
111120 }
@@ -116,10 +125,8 @@ private function getDecodedResponse(Request $request)
116125
117126 return $ decodedResponse ;
118127 } catch (NetworkException $ e ) {
119- if ($ this ->cacheEndpoint ) {
120- if ($ this ->cache ->has ($ this ->getCacheId ($ request ))) {
121- return $ this ->cache ->get ($ this ->getCacheId ($ request ));
122- }
128+ if ($ this ->cacheEndpoint && $ this ->cache ->has ($ this ->getCacheId ($ request ))) {
129+ return $ this ->cache ->get ($ this ->getCacheId ($ request ));
123130 }
124131 throw $ e ;
125132 }
0 commit comments