From 60783f5b316a0a1b654d056ef946ef9b28053c45 Mon Sep 17 00:00:00 2001 From: PabloKowalczyk <11366345+PabloKowalczyk@users.noreply.github.com> Date: Sun, 21 Oct 2018 19:43:20 +0200 Subject: [PATCH] Cache find place requests (#4) --- README.md | 6 ++++++ src/Client.php | 33 ++++++++++++++++++++++++--------- tests/Unit/ClientTest.php | 23 +++++++++++++++++++++++ 3 files changed, 53 insertions(+), 9 deletions(-) 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 {