setClientHeaders(); $response = $this->makeApiRequest($client, $postcode); $data = $response->toArray(); $this->throwIfNoResponseData($data); return $this->createGeocodeDataObject($data); } private function setClientHeaders(): HttpClientInterface { return $this->client->withOptions([ 'headers' => ["User-Agent" => "Geocode"] ]); } private function makeApiRequest(HttpClientInterface $client, string $postcode): ResponseInterface { return $client->request( method: 'GET', url: self::API_URL, options: [ 'query' => [ 'format' => 'json', 'postalcode' => $postcode ] ] ); } private function throwIfNoResponseData(array $data): void { if (empty($data)) throw new Exception("No data was received from API response! Were the arguments valid?"); } private function createGeocodeDataObject(array $data): LatLonModel { return new LatLonModel((float) $data[0]['lat'], (float) $data[0]['lon']); } }