1111
1212namespace App \Service ;
1313
14- use App \Exception \OfficialEndpointNotAvailableExtension ;
15- use GuzzleHttp \Psr7 \Request ;
16- use Http \Client \Exception \NetworkException ;
17- use Http \Client \HttpClient ;
18- use Symfony \Component \Cache \Simple \FilesystemCache ;
19- use Symfony \Component \HttpFoundation \Response ;
14+ use App \Service \Decoder \ResponseDecoder ;
15+ use Nyholm \Psr7 \Request ;
2016
2117/**
2218 * Class OfficialEndpointProxy
@@ -28,32 +24,22 @@ class OfficialEndpointProxy
2824 /** @var string */
2925 private $ endpoint ;
3026
31- /** @var bool */
32- private $ cacheEndpoint ;
33-
34- /** @var HttpClient */
35- private $ client ;
36-
37- /** @var FilesystemCache */
38- private $ cache ;
27+ /**
28+ * @var ResponseDecoder
29+ */
30+ private $ decoder ;
3931
4032 /**
4133 * OfficialEndpointProxy constructor.
4234 * @param string $officialEndpoint
43- * @param bool $cacheEndpoint
44- * @param HttpClient $client
45- * @param Cache $cache
35+ * @param ResponseDecoder $decoder
4636 */
4737 public function __construct (
4838 string $ officialEndpoint ,
49- bool $ cacheEndpoint ,
50- HttpClient $ client ,
51- Cache $ cache
39+ ResponseDecoder $ decoder
5240 ) {
53- $ this ->cacheEndpoint = $ cacheEndpoint ;
54- $ this ->client = $ client ;
5541 $ this ->endpoint = $ officialEndpoint ;
56- $ this ->cache = $ cache () ;
42+ $ this ->decoder = $ decoder ;
5743 }
5844
5945 /**
@@ -62,11 +48,12 @@ public function __construct(
6248 * @return array
6349 * @throws \Exception
6450 * @throws \Http\Client\Exception
51+ * @throws \Psr\SimpleCache\InvalidArgumentException
6552 */
6653 public function getAliases ()
6754 {
6855 $ request = new Request ('GET ' , $ this ->endpoint . 'aliases.json ' );
69- return $ this ->getDecodedResponse ($ request );
56+ return $ this ->decoder -> getDecodedResponse ($ request );
7057 }
7158
7259 /**
@@ -75,11 +62,12 @@ public function getAliases()
7562 * @return array
7663 * @throws \Exception
7764 * @throws \Http\Client\Exception
65+ * @throws \Psr\SimpleCache\InvalidArgumentException
7866 */
7967 public function getVersions ()
8068 {
8169 $ request = new Request ('GET ' , $ this ->endpoint . 'versions.json ' );
82- return $ this ->getDecodedResponse ($ request );
70+ return $ this ->decoder -> getDecodedResponse ($ request );
8371 }
8472
8573 /**
@@ -89,57 +77,11 @@ public function getVersions()
8977 * @return array|string
9078 * @throws \Exception
9179 * @throws \Http\Client\Exception
80+ * @throws \Psr\SimpleCache\InvalidArgumentException
9281 */
9382 public function getPackages (string $ packagesRequestString )
9483 {
9584 $ request = new Request ('GET ' , $ this ->endpoint . 'p/ ' . $ packagesRequestString );
96- return $ this ->getDecodedResponse ($ request );
97- }
98-
99- /**
100- * @param Request $request
101- * @return array|string
102- * @throws \Exception
103- * @throws \Http\Client\Exception
104- */
105- private function getDecodedResponse (Request $ request )
106- {
107- try {
108- $ response = $ this ->client ->sendRequest ($ request );
109- $ decodedResponse = json_decode ($ response ->getBody (), true );
110-
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-
118- if (json_last_error () !== JSON_ERROR_NONE ) {
119- $ decodedResponse = $ response ->getBody ();
120- }
121-
122- if ($ this ->cacheEndpoint ) {
123- $ this ->cache ->set ($ this ->getCacheId ($ request ), $ decodedResponse );
124- }
125-
126- return $ decodedResponse ;
127- } catch (NetworkException $ e ) {
128- if ($ this ->cacheEndpoint && $ this ->cache ->has ($ this ->getCacheId ($ request ))) {
129- return $ this ->cache ->get ($ this ->getCacheId ($ request ));
130- }
131- throw $ e ;
132- }
85+ return $ this ->decoder ->getDecodedResponse ($ request );
13386 }
134-
135- /**
136- * @param Request $request
137- * @return string
138- */
139- private function getCacheId (Request $ request )
140- {
141- $ id = $ request ->getMethod () . $ request ->getUri ();
142- return sha1 (preg_replace ('/[^A-Za-z0-9\.\- ]/ ' , '' , $ id ));
143- }
144-
14587}
0 commit comments