Skip to content

Commit c53f5f2

Browse files
committed
Refactoring and improved exception output
1 parent 7270987 commit c53f5f2

File tree

5 files changed

+33
-32
lines changed

5 files changed

+33
-32
lines changed

src/Service/Decoder/ResponseDecoder.php renamed to src/Service/Decoder/JsonResponseDecoder.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@
77
use Nyholm\Psr7\Request;
88
use Symfony\Component\Cache\Simple\FilesystemCache;
99

10-
class ResponseDecoder
10+
class JsonResponseDecoder
1111
{
12-
/**
13-
* @var bool
14-
*/
12+
/** @var bool */
1513
private $cacheEndpoint;
1614

17-
/**
18-
* @var HttpClient
19-
*/
15+
/** @var HttpClient */
2016
private $client;
2117

22-
/**
23-
* @var FilesystemCache
24-
*/
18+
/** @var FilesystemCache */
2519
private $cache;
2620

21+
/**
22+
* @param bool $cacheEndpoint
23+
* @param HttpClient $client
24+
* @param Cache $cache
25+
*/
2726
public function __construct(bool $cacheEndpoint, HttpClient $client, Cache $cache)
2827
{
2928
$this->cacheEndpoint = $cacheEndpoint;
@@ -33,7 +32,9 @@ public function __construct(bool $cacheEndpoint, HttpClient $client, Cache $cach
3332

3433
/**
3534
* @param Request $request
35+
*
3636
* @return array|mixed|\Psr\Http\Message\StreamInterface
37+
*
3738
* @throws \Http\Client\Exception
3839
* @throws \Psr\SimpleCache\InvalidArgumentException
3940
*/
@@ -69,6 +70,7 @@ public function getDecodedResponse(Request $request)
6970

7071
/**
7172
* @param Request $request
73+
*
7274
* @return string
7375
*/
7476
private function getCacheId(Request $request)

src/Service/OfficialEndpointProxy.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
namespace App\Service;
1313

14-
use App\Service\Decoder\ResponseDecoder;
14+
use App\Service\Decoder\JsonResponseDecoder;
1515
use Nyholm\Psr7\Request;
1616

1717
/**
@@ -24,19 +24,17 @@ class OfficialEndpointProxy
2424
/** @var string */
2525
private $endpoint;
2626

27-
/**
28-
* @var ResponseDecoder
29-
*/
27+
/** @var JsonResponseDecoder */
3028
private $decoder;
3129

3230
/**
3331
* OfficialEndpointProxy constructor.
3432
* @param string $officialEndpoint
35-
* @param ResponseDecoder $decoder
33+
* @param JsonResponseDecoder $decoder
3634
*/
3735
public function __construct(
3836
string $officialEndpoint,
39-
ResponseDecoder $decoder
37+
JsonResponseDecoder $decoder
4038
) {
4139
$this->endpoint = $officialEndpoint;
4240
$this->decoder = $decoder;

src/Service/Provider/PackagesProvider.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use App\Service\Compiler\LocalRecipeCompiler;
1616
use App\Service\Compiler\PackagesCompiler;
1717
use App\Service\OfficialEndpointProxy;
18+
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
1819

1920
/**
2021
* Class AliasesProvider
@@ -141,8 +142,10 @@ private function getLocalRecipe(array $package)
141142
* Parses the request string and provides an array of requested packages
142143
*
143144
* @param string $packagesRequestString
145+
*
144146
* @return array
145-
* @throws \HttpRequestException
147+
*
148+
* @throws
146149
*/
147150
private function parseRequestedPackages(string $packagesRequestString)
148151
{
@@ -151,7 +154,7 @@ private function parseRequestedPackages(string $packagesRequestString)
151154
$packageDetails = explode(',', $requestedPackage);
152155

153156
if (count($packageDetails) < 3) {
154-
throw new \HttpRequestException('Invalid package string provided', 500);
157+
throw new BadRequestHttpException('Invalid package string provided');
155158
}
156159

157160
$packages[] = [

tests/Service/Decoder/ResponseDecoderTest.php renamed to tests/Service/Decoder/JsonResponseDecoderTest.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
<?php
2-
declare(strict_types=1);
3-
42
namespace App\Tests\Service\Decoder;
53

64
use App\Service\Cache;
7-
use App\Service\Decoder\ResponseDecoder;
5+
use App\Service\Decoder\JsonResponseDecoder;
86
use Http\Client\Exception\NetworkException;
97
use Http\Client\HttpClient;
108
use Nyholm\Psr7\Request;
119
use PHPUnit\Framework\TestCase;
1210
use Psr\Http\Message\ResponseInterface;
1311
use Symfony\Component\Cache\Simple\FilesystemCache;
1412

15-
class ResponseDecoderTest extends TestCase
13+
class JsonResponseDecoderTest extends TestCase
1614
{
1715
/**
1816
* @var HttpClient|\Prophecy\Prophecy\ObjectProphecy
@@ -39,7 +37,7 @@ protected function setUp()
3937

4038
public function testGetDecodedResponse()
4139
{
42-
$decoder = new ResponseDecoder(false, $this->client->reveal(), $this->cache->reveal());
40+
$decoder = new JsonResponseDecoder(false, $this->client->reveal(), $this->cache->reveal());
4341

4442
$request = new Request('GET', 'endpoint/data.json');
4543
$response = $this->prophesize(ResponseInterface::class);
@@ -56,7 +54,7 @@ public function testGetDecodedResponse()
5654

5755
public function testGetDecodedResponseEmptyOnRequestError()
5856
{
59-
$decoder = new ResponseDecoder(false, $this->client->reveal(), $this->cache->reveal());
57+
$decoder = new JsonResponseDecoder(false, $this->client->reveal(), $this->cache->reveal());
6058

6159
$request = new Request('GET', 'endpoint/data.json');
6260
$response = $this->prophesize(ResponseInterface::class);
@@ -73,7 +71,7 @@ public function testGetDecodedResponseEmptyOnRequestError()
7371

7472
public function testGetDecodedResponseFromCacheOnRequestError()
7573
{
76-
$decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
74+
$decoder = new JsonResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
7775

7876
$request = new Request('GET', 'endpoint/data.json');
7977
$response = $this->prophesize(ResponseInterface::class);
@@ -93,7 +91,7 @@ public function testGetDecodedResponseFromCacheOnRequestError()
9391

9492
public function testGetDecodedResponseCachesDataIfEnabled()
9593
{
96-
$decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
94+
$decoder = new JsonResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
9795

9896
$request = new Request('GET', 'endpoint/data.json');
9997
$response = $this->prophesize(ResponseInterface::class);
@@ -113,7 +111,7 @@ public function testGetDecodedResponseCachesDataIfEnabled()
113111

114112
public function testGetDecodedResponseFromCacheWhenNetworkFails()
115113
{
116-
$decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
114+
$decoder = new JsonResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
117115

118116
$request = new Request('GET', 'endpoint/data.json');
119117

@@ -135,7 +133,7 @@ public function testGetDecodedResponseFromCacheWhenNetworkFails()
135133
*/
136134
public function testGetDecodedResponseThrowsNetworkExceptionWhenClientFailsAndNoCachedVersion()
137135
{
138-
$decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
136+
$decoder = new JsonResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
139137
$request = new Request('GET', 'endpoint/data.json');
140138

141139
$this->simpleCache->has('4429b090fd82239e188859ae626162e5e790b4db')->willReturn(false);
@@ -151,7 +149,7 @@ public function testGetDecodedResponseThrowsNetworkExceptionWhenClientFailsAndNo
151149
*/
152150
public function testGetDecodedResponseReturnsBodyWhenJsonDecodingFails()
153151
{
154-
$decoder = new ResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
152+
$decoder = new JsonResponseDecoder(true, $this->client->reveal(), $this->cache->reveal());
155153

156154
$request = new Request('GET', 'endpoint/data.json');
157155
$response = $this->prophesize(ResponseInterface::class);

tests/Service/OfficialEndpointProxyTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace App\Tests\Service;
55

6-
use App\Service\Decoder\ResponseDecoder;
6+
use App\Service\Decoder\JsonResponseDecoder;
77
use App\Service\OfficialEndpointProxy;
88
use PHPUnit\Framework\TestCase;
99
use Prophecy\Argument;
@@ -17,13 +17,13 @@ class OfficialEndpointProxyTest extends TestCase
1717
private $proxy;
1818

1919
/**
20-
* @var ResponseDecoder|ObjectProphecy
20+
* @var JsonResponseDecoder|ObjectProphecy
2121
*/
2222
private $decoder;
2323

2424
protected function setUp()
2525
{
26-
$this->decoder = $this->prophesize(ResponseDecoder::class);
26+
$this->decoder = $this->prophesize(JsonResponseDecoder::class);
2727

2828
$this->proxy = new OfficialEndpointProxy(
2929
'official_endpoint/',

0 commit comments

Comments
 (0)