Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache find place requests #4

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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