diff --git a/README.md b/README.md index 6bfff07..9900d92 100644 --- a/README.md +++ b/README.md @@ -95,7 +95,13 @@ Things to do before stable `v1` release: - [x] Place Details - [x] Caching Place Details - [ ] Places Search + - [x] Find Place + - [ ] Nearby Search + - [ ] Text Search - [ ] Caching Places Search + - [x] Find Place + - [ ] Nearby Search + - [ ] Text Search - [ ] Place Photos - [ ] Caching Place Photos - [ ] Use `PSR-18` instead of `HTTPlug` diff --git a/src/Client.php b/src/Client.php index 727c728..a2f39df 100644 --- a/src/Client.php +++ b/src/Client.php @@ -104,18 +104,33 @@ public function findPlace( $inputType, $optionalParameters ?? OptionalParameters::fromArray([]) ); - $request = $this->requestFactory - ->createRequest('GET', $url->toString()); - $response = $this->httpClient - ->sendRequest($request); - $responseData = $this->parseJsonResponse($response->getBody()); - $responseStatus = $responseData['status'] ?? ''; - if ('OK' !== $responseStatus) { - throw ApiException::create($responseStatus, $responseData['error_message'] ?? ''); + $isCached = $this->cache + ->has($url->toHash()); + $findPlaceData = null; + + if (!$isCached) { + $request = $this->requestFactory + ->createRequest('GET', $url->toString()); + $response = $this->httpClient + ->sendRequest($request); + $responseData = $this->parseJsonResponse($response->getBody()); + $responseStatus = $responseData['status'] ?? ''; + + if ('OK' !== $responseStatus) { + throw ApiException::create($responseStatus, $responseData['error_message'] ?? ''); + } + + $findPlaceData = $responseData['candidates'] ?? []; + + $this->cache + ->set($url->toHash(), $findPlaceData); + } else { + $findPlaceData = $this->cache + ->get($url->toHash()); } - return FindPlace::fromArray($responseData['candidates'] ?? []); + return FindPlace::fromArray($findPlaceData); } private function parseJsonResponse(StreamInterface $stream): array diff --git a/tests/Unit/ClientTest.php b/tests/Unit/ClientTest.php index ad3d105..0c0ee9e 100644 --- a/tests/Unit/ClientTest.php +++ b/tests/Unit/ClientTest.php @@ -73,6 +73,29 @@ public function find_place_fetch_data_by_http(): void $this->assertSame('140 George St, The Rocks NSW 2000, Australia', $firstCandidate->formattedAddress()); } + /** @test */ + public function find_place_fetched_form_cache_if_cached(): void + { + $fixtureLoader = new FixtureLoader(); + $candidatesData = $fixtureLoader->getJson('find-place')['candidates'] ?? []; + $cacheMock = new CacheSpy($candidatesData); + $response = ResponseMock::withContent('[]'); + + $client = Client::create( + 'some-api-key', + $this->createDummyClient($response), + $this->createDummyRequestFactory(), + $cacheMock + ); + $findPlace = $client->findPlace('some-place-id'); + $candidates = $findPlace->candidates(); + + $this->assertCount(1, $candidates); + $firstCandidate = \array_values($candidates)[0]; + $this->assertSame('Museum of Contemporary Art Australia', $firstCandidate->name()); + $this->assertSame('140 George St, The Rocks NSW 2000, Australia', $firstCandidate->formattedAddress()); + } + private function createDummyClient(ResponseInterface $response): HttpClient { return new class($response) implements HttpClient {