/** * @test */ public function it_returns_cached_coordinates_if_possible() { $address = 'Wetstraat 1, 1000 Brussel, BE'; $expectedCoordinates = new Coordinates(new Latitude(1.07845), new Longitude(2.76412)); $this->decoratee->expects($this->once())->method('getCoordinates')->with($address)->willReturn($expectedCoordinates); $freshCoordinates = $this->service->getCoordinates($address); $cachedCoordinates = $this->service->getCoordinates($address); $this->assertEquals($expectedCoordinates, $freshCoordinates); $this->assertEquals($expectedCoordinates, $cachedCoordinates); }
/** * @param string $address * @return Coordinates */ public function getCoordinates($address) { $encodedCacheData = $this->cache->fetch($address); if ($encodedCacheData) { $cacheData = json_decode($encodedCacheData, true); if (isset($cacheData['lat']) && isset($cacheData['long'])) { return new Coordinates(new Latitude((double) $cacheData['lat']), new Longitude((double) $cacheData['long'])); } } $coordinates = $this->geocodingService->getCoordinates($address); $encodedCacheData = json_encode(['lat' => $coordinates->getLatitude()->toDouble(), 'long' => $coordinates->getLongitude()->toDouble()]); $this->cache->save($address, $encodedCacheData); return $coordinates; }