host.self::SEARCH_URI.'?'.http_build_query(['query' => $movieName]); $searchContext = $this->fetchSerialized('GET', $url, MovieSearchContext::class); if (empty($searchResult = $searchContext->getResults())) { return null; } return reset($searchResult); } /** * @throws GatewayException */ public function getMovieCredits(int $movieId): ?MovieCreditsContext { $url = str_replace('{id}', $movieId, $this->host.self::MOVIE_CREDITS_URI); return $this->fetchSerialized('GET', $url, MovieCreditsContext::class); } /** * @throws GatewayException */ private function fetch(string $method, string $url): ResponseInterface { try { return $this->client->request($method, $url, ['headers' => ['Authorization' => 'Bearer '.$this->apiToken, 'accept' => 'application/json']]); } catch (\Throwable $e) { throw new GatewayException(self::class, $e->getMessage(), $e); } } /** * @throws GatewayException */ private function fetchSerialized(string $method, string $url, string $class, string $type = 'json'): mixed { $result = $this->fetch($method, $url); try { return $this->serializer->deserialize($result->getContent(), $class, $type); } catch (\Throwable $e) { throw new GatewayException(self::class, $e->getMessage(), $e); } } }