Skip to content

Commit 2b1d9ee

Browse files
authored
Merge pull request #27 from respinoza/issue-26-fix-wrong-request-object-for-official-endpoint
Fix wrong class use for Request object (closes #26)
2 parents 90ba3bf + 800273b commit 2b1d9ee

File tree

4 files changed

+350
-73
lines changed

4 files changed

+350
-73
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Service\Decoder;
5+
6+
use App\Service\Cache;
7+
use Http\Client\Exception\NetworkException;
8+
use Http\Client\HttpClient;
9+
use Nyholm\Psr7\Request;
10+
use Symfony\Component\Cache\Simple\FilesystemCache;
11+
12+
class ResponseDecoder
13+
{
14+
/**
15+
* @var bool
16+
*/
17+
private $cacheEndpoint;
18+
19+
/**
20+
* @var HttpClient
21+
*/
22+
private $client;
23+
24+
/**
25+
* @var FilesystemCache
26+
*/
27+
private $cache;
28+
29+
public function __construct(bool $cacheEndpoint, HttpClient $client, Cache $cache)
30+
{
31+
$this->cacheEndpoint = $cacheEndpoint;
32+
$this->client = $client;
33+
$this->cache = $cache();
34+
}
35+
36+
/**
37+
* @param Request $request
38+
* @return array|mixed|\Psr\Http\Message\StreamInterface
39+
* @throws \Http\Client\Exception
40+
* @throws \Psr\SimpleCache\InvalidArgumentException
41+
*/
42+
public function getDecodedResponse(Request $request)
43+
{
44+
try {
45+
$response = $this->client->sendRequest($request);
46+
$decodedResponse = json_decode($response->getBody(), true);
47+
48+
if(!in_array($response->getStatusCode(), range(200, 299))) {
49+
if ($this->cacheEndpoint && $this->cache->has($this->getCacheId($request))) {
50+
return $this->cache->get($this->getCacheId($request));
51+
}
52+
return [];
53+
}
54+
55+
if (json_last_error() !== JSON_ERROR_NONE) {
56+
$decodedResponse = $response->getBody();
57+
}
58+
59+
if ($this->cacheEndpoint) {
60+
$this->cache->set($this->getCacheId($request), $decodedResponse);
61+
}
62+
63+
return $decodedResponse;
64+
} catch (NetworkException $e) {
65+
if ($this->cacheEndpoint && $this->cache->has($this->getCacheId($request))) {
66+
return $this->cache->get($this->getCacheId($request));
67+
}
68+
throw $e;
69+
}
70+
}
71+
72+
/**
73+
* @param Request $request
74+
* @return string
75+
*/
76+
private function getCacheId(Request $request)
77+
{
78+
return sha1(
79+
preg_replace(
80+
'/[^A-Za-z0-9\.\- ]/',
81+
'',
82+
$request->getMethod().$request->getUri()
83+
)
84+
);
85+
}
86+
}

src/Service/OfficialEndpointProxy.php

Lines changed: 15 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,8 @@
1111

1212
namespace 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

Comments
 (0)