Skip to content

Commit a12d4e9

Browse files
fix: use Response::toArray instead of decoder (#520)
1 parent c156ecf commit a12d4e9

File tree

3 files changed

+5
-11
lines changed

3 files changed

+5
-11
lines changed

api/src/BookRepository/GutendexBookRepository.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
namespace App\BookRepository;
66

77
use App\Entity\Book;
8-
use Symfony\Component\Serializer\Encoder\DecoderInterface;
98
use Symfony\Contracts\HttpClient\HttpClientInterface;
109

1110
final readonly class GutendexBookRepository implements RestrictedBookRepositoryInterface
1211
{
1312
public function __construct(
1413
private HttpClientInterface $gutendexClient,
15-
private DecoderInterface $decoder,
1614
) {
1715
}
1816

@@ -31,7 +29,7 @@ public function find(string $url): ?Book
3129

3230
$book = new Book();
3331

34-
$data = $this->decoder->decode($response->getContent(), 'json');
32+
$data = $response->toArray();
3533
$book->title = $data['title'];
3634
$book->author = $data['authors'][0]['name'] ?? null;
3735

api/src/BookRepository/OpenLibraryBookRepository.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
namespace App\BookRepository;
66

77
use App\Entity\Book;
8-
use Symfony\Component\Serializer\Encoder\DecoderInterface;
98
use Symfony\Contracts\HttpClient\HttpClientInterface;
109

1110
final readonly class OpenLibraryBookRepository implements RestrictedBookRepositoryInterface
1211
{
1312
public function __construct(
1413
private HttpClientInterface $openLibraryClient,
15-
private DecoderInterface $decoder,
1614
) {
1715
}
1816

@@ -31,13 +29,13 @@ public function find(string $url): ?Book
3129

3230
$book = new Book();
3331

34-
$data = $this->decoder->decode($response->getContent(), 'json');
32+
$data = $response->toArray();
3533
$book->title = $data['title'];
3634

3735
$book->author = null;
3836
if (isset($data['authors'][0]['key'])) {
3937
$authorResponse = $this->openLibraryClient->request('GET', $data['authors'][0]['key'] . '.json', $options);
40-
$author = $this->decoder->decode($authorResponse->getContent(), 'json');
38+
$author = $authorResponse->toArray();
4139
if (isset($author['name'])) {
4240
$book->author = $author['name'];
4341
}

api/src/Command/BooksImportCommand.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use Symfony\Component\Console\Output\OutputInterface;
1313
use Symfony\Component\Console\Style\SymfonyStyle;
1414
use Symfony\Component\HttpFoundation\Request;
15-
use Symfony\Component\Serializer\Encoder\DecoderInterface;
1615
use Symfony\Component\Serializer\Encoder\JsonEncode;
1716
use Symfony\Component\Serializer\SerializerInterface;
1817
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -25,7 +24,6 @@ final class BooksImportCommand extends Command
2524
{
2625
public function __construct(
2726
private readonly SerializerInterface $serializer,
28-
private readonly DecoderInterface $decoder,
2927
private readonly HttpClientInterface $client,
3028
private readonly LoggerInterface $logger,
3129
) {
@@ -91,10 +89,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int
9189

9290
private function getData(string $uri): array
9391
{
94-
return $this->decoder->decode($this->client->request(Request::METHOD_GET, $uri, [
92+
return $this->client->request(Request::METHOD_GET, $uri, [
9593
'headers' => [
9694
'Accept' => 'application/json',
9795
],
98-
])->getContent(), 'json');
96+
])->toArray();
9997
}
10098
}

0 commit comments

Comments
 (0)