Skip to content

Commit

Permalink
Cache find place requests (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloKowalczyk committed Oct 21, 2018
1 parent 85ec223 commit 60783f5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 9 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
33 changes: 24 additions & 9 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions tests/Unit/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 60783f5

Please sign in to comment.