diff --git a/src/Controller/BookController.php b/src/Controller/BookController.php index af9492b4..383dc2fc 100644 --- a/src/Controller/BookController.php +++ b/src/Controller/BookController.php @@ -31,7 +31,7 @@ public function index(Request $request, Book $book, string $slug, BookSuggestion $openLibraries = (bool) $request->get('openlib', false); if (!$book->isVerified() && $google === true) { $suggestions = $bookSuggestions->getGoogleSuggestions($book); - }elseif (!$book->isVerified() && $openLibraries === true) { + } elseif (!$book->isVerified() && $openLibraries === true) { $suggestions = $bookSuggestions->getCategorySuggestions($book); } @@ -82,7 +82,7 @@ public function index(Request $request, Book $book, string $slug, BookSuggestion #[Route('/download-image/{id}/{image}', name: 'app_book_downloadImage')] public function downloadImage(Book $book, string $image, BookSuggestions $bookSuggestions, EntityManagerInterface $entityManager, BookFileSystemManager $fileSystemManager): Response { - $suggestions = $bookSuggestions->getSuggestions($book); + $suggestions = $bookSuggestions->getGoogleSuggestions($book); $url = $suggestions['image'][$image] ?? null; diff --git a/src/Service/BookSuggestions.php b/src/Service/BookSuggestions.php index fc5b07d7..274c82c6 100644 --- a/src/Service/BookSuggestions.php +++ b/src/Service/BookSuggestions.php @@ -40,22 +40,23 @@ public function getCategorySuggestions(Book $book): array $mainAuthor = current($book->getAuthors()); - $query = ['q'=>'title:'.$book->getTitle().' author:'.$mainAuthor, 'fields'=>'title,author_name,key,cover_i,subject']; + $query = ['q' => 'title:'.$book->getTitle().' author:'.$mainAuthor, 'fields' => 'title,author_name,key,cover_i,subject']; $client = new \GuzzleHttp\Client(); - - $results = $client->request('GET','https://openlibrary.org/search.json', ['query'=>$query])->getBody()->getContents(); + $results = $client->request('GET', 'https://openlibrary.org/search.json', ['query' => $query])->getBody()->getContents(); $results = json_decode($results, true); + if (!is_array($results) || !array_key_exists('docs', $results)) { + return $suggestions; + } foreach ($results['docs'] as $result) { - foreach ($result['subject'] as $category) { $suggestions['tags'][$category] = $category; } - } + return $suggestions; }