Skip to content

Commit a13153a

Browse files
authored
Refactor ApiResponseFormatter (#235)
1 parent 1702b79 commit a13153a

8 files changed

Lines changed: 123 additions & 273 deletions

File tree

config/web/di/data-response.php

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,10 @@
33
declare(strict_types=1);
44

55
use App\Http\ApiResponseFormatter;
6-
use Yiisoft\DataResponse\DataResponseFactory;
7-
use Yiisoft\DataResponse\DataResponseFactoryInterface;
86
use Yiisoft\DataResponse\DataResponseFormatterInterface;
9-
use Yiisoft\DataResponse\Formatter\HtmlDataResponseFormatter;
10-
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
11-
use Yiisoft\DataResponse\Formatter\XmlDataResponseFormatter;
12-
use Yiisoft\DataResponse\Middleware\ContentNegotiator;
137

148
/* @var $params array */
159

1610
return [
1711
DataResponseFormatterInterface::class => ApiResponseFormatter::class,
18-
DataResponseFactoryInterface::class => DataResponseFactory::class,
19-
ContentNegotiator::class => [
20-
'__construct()' => [
21-
'contentFormatters' => [
22-
'text/html' => new HtmlDataResponseFormatter(),
23-
'application/xml' => new XmlDataResponseFormatter(),
24-
'application/json' => new JsonDataResponseFormatter(),
25-
],
26-
],
27-
],
2812
];

src/Http/ApiResponseData.php

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/Http/ApiResponseDataFactory.php

Lines changed: 0 additions & 61 deletions
This file was deleted.

src/Http/ApiResponseFormatter.php

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,57 @@
44

55
namespace App\Http;
66

7+
use LogicException;
78
use Psr\Http\Message\ResponseInterface;
89
use Yiisoft\DataResponse\DataResponse;
910
use Yiisoft\DataResponse\DataResponseFormatterInterface;
1011
use Yiisoft\DataResponse\Formatter\JsonDataResponseFormatter;
12+
use Yiisoft\Http\Status;
1113

12-
final class ApiResponseFormatter implements DataResponseFormatterInterface
14+
use function sprintf;
15+
16+
final readonly class ApiResponseFormatter implements DataResponseFormatterInterface
1317
{
14-
public function __construct(private ApiResponseDataFactory $apiResponseDataFactory, private JsonDataResponseFormatter $jsonDataResponseFormatter) {}
18+
public function __construct(
19+
private JsonDataResponseFormatter $jsonDataResponseFormatter,
20+
) {}
1521

1622
public function format(DataResponse $dataResponse): ResponseInterface
1723
{
18-
$response = $dataResponse->withData(
19-
$this->apiResponseDataFactory
20-
->createFromResponse($dataResponse)
21-
->toArray(),
24+
$data = $dataResponse->getStatusCode() === Status::OK
25+
? $this->createSuccessData($dataResponse)
26+
: $this->createFailedData($dataResponse);
27+
28+
return $this->jsonDataResponseFormatter->format(
29+
$dataResponse->withData($data),
2230
);
31+
}
32+
33+
private function createSuccessData(DataResponse $response): array
34+
{
35+
$data = $response->getData();
36+
if ($data !== null && !is_array($data)) {
37+
throw new LogicException(
38+
sprintf(
39+
'The response data must be either null or an array. Got %s.',
40+
get_debug_type($data),
41+
),
42+
);
43+
}
44+
45+
return [
46+
'status' => 'success',
47+
'data' => $data,
48+
];
49+
}
2350

24-
return $this->jsonDataResponseFormatter->format($response);
51+
private function createFailedData(DataResponse $response): array
52+
{
53+
$data = $response->getData();
54+
return [
55+
'status' => 'failed',
56+
'error_message' => is_string($data) && $data !== '' ? $data : 'Unknown error',
57+
'error_code' => $response->getStatusCode(),
58+
];
2559
}
2660
}

tests/Acceptance/SiteCest.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ public function getHome(AcceptanceTester $I): void
1717
$I->seeResponseContainsJson(
1818
[
1919
'status' => 'success',
20-
'error_message' => '',
21-
'error_code' => null,
2220
'data' => [
2321
'name' => 'My Project',
2422
'version' => '1.0',
@@ -37,7 +35,6 @@ public function testNotFoundPage(AcceptanceTester $I): void
3735
'status' => 'failed',
3836
'error_message' => 'Not found.',
3937
'error_code' => 404,
40-
'data' => null,
4138
],
4239
);
4340
}

tests/Functional/InfoControllerTest.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ public function testGetIndex()
2727
assertSame(
2828
[
2929
'status' => 'success',
30-
'error_message' => '',
31-
'error_code' => null,
3230
'data' => ['name' => 'My Project', 'version' => '1.0'],
3331
],
3432
json_decode($output, true),

tests/Unit/Http/ApiResponseDataFactoryTest.php

Lines changed: 0 additions & 117 deletions
This file was deleted.

0 commit comments

Comments
 (0)