apiKey) !== ''; } /** * @return string|null the image data, or null when there is no cover or it could not be fetched */ public function download(string $isbn): ?string { if (!$this->isEnabled() || preg_match('/^[\dX]{10,13}$/', $isbn) !== 1) { return null; } try { $response = $this->libraryThingClient->request( 'GET', \sprintf('/devkey/%s/large/isbn/%s', rawurlencode(trim((string)$this->apiKey)), $isbn), ); if ($response->getStatusCode() !== 200) { $this->logger->warning('LibraryThing returned HTTP {status} for ISBN {isbn}.', [ 'status' => $response->getStatusCode(), 'isbn' => $isbn, ]); return null; } $data = $response->getContent(); } catch (ExceptionInterface $exception) { // The message would contain the URL, and with it the key. $this->logger->warning('The LibraryThing cover for ISBN {isbn} could not be downloaded.', ['isbn' => $isbn]); return null; } if (\strlen($data) > self::MAX_BYTES) { return null; } $size = @getimagesizefromstring($data); return $size === false || $size[0] <= 1 || $size[1] <= 1 ? null : $data; } }