Skip to content
This repository has been archived by the owner on Feb 25, 2021. It is now read-only.

Commit

Permalink
Merge pull request #8 from ft-1408/add-maps-timezone
Browse files Browse the repository at this point in the history
Added maps timezone
  • Loading branch information
Bukashk0zzz authored Feb 1, 2018
2 parents 2efb488 + 6532238 commit fef922c
Show file tree
Hide file tree
Showing 2 changed files with 175 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/MapsPlace.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,16 @@ class MapsPlace implements CacheInterface
private $factory;

/**
* MapsPlace constructor.
*
* @param LoggerInterface $logger
* @param \Memcached $memcached
* @param Cache $cache
* @param string $apiKey
* @param PlaceSimpleFactory $factory
*/
public function __construct(LoggerInterface $logger, \Memcached $memcached, string $apiKey, PlaceSimpleFactory $factory)
public function __construct(LoggerInterface $logger, Cache $cache, string $apiKey, PlaceSimpleFactory $factory)
{
$this->cache = new Cache($memcached);
$this->cache = $cache;
$this->apiKey = $apiKey;
$this->logger = $logger;
$this->factory = $factory;
Expand Down
170 changes: 170 additions & 0 deletions src/MapsTimezone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
<?php declare(strict_types = 1);

namespace Rentberry\MapsUtils;

use GuzzleHttp\Client;
use Psr\Log\LoggerInterface;
use Rentberry\MapsUtils\Cache\Cache;
use Rentberry\MapsUtils\Cache\CacheInterface;

/**
* MapsTimezone
*/
class MapsTimezone implements CacheInterface
{
private const LOCATION_ROUND_LEVEL = 3;
/**
* @var Cache
*/
protected $cache;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var string
*/
protected $query;

/**
* @var string
*/
private $apiKey;

/**
* @var Client
*/
private $client;

/**
* @var float
*/
private $lat;

/**
* @var float
*/
private $lng;

/**
* MapsTimezone constructor.
*
* @param LoggerInterface $logger
* @param Cache $cache
* @param string $apiKey
*/
public function __construct(LoggerInterface $logger, Cache $cache, string $apiKey)
{
$this->cache = $cache;
$this->apiKey = $apiKey;
$this->logger = $logger;
}

/**
* @param float $lat
* @param float $lng
*
* @return null|string
*/
public function getTimezonePoint(float $lat, float $lng): ?string
{
$this->lat = $lat;
$this->lng = $lng;

return $googleData = $this->getValidatedCacheData();
}

/**
* Return google raw timezone data
*
* @return mixed[]|null
*/
public function getData(): ?array
{
try {
$roundedLattitude = \round($this->lat, self::LOCATION_ROUND_LEVEL);
$roundedLongitude = \round($this->lng, self::LOCATION_ROUND_LEVEL);
$dt = new \DateTime();

$result = $this->getClient()->get($this->getApiUrl($roundedLattitude, $roundedLongitude, $dt), ['timeout' => 10]);
$result = \json_decode($result->getBody()->getContents(), true);

// Going to save data only if it's correct
if ($this->isTimezoneResultCorrect($result)) {
return $result;
}
} catch (\Throwable $e) {
$this->logger->warning('Problem with getting google place data. Error: '.$e->getMessage());
}

return null;
}

/**
* @return string
*/
public function getCacheKey(): string
{
$roundedLattitude = \round($this->lat, self::LOCATION_ROUND_LEVEL);
$roundedLongitude = \round($this->lng, self::LOCATION_ROUND_LEVEL);

return self::class.$roundedLattitude.$roundedLongitude;
}

/**
* @param mixed[] $result
*
* @return bool
*/
private function isTimezoneResultCorrect(array $result): bool
{
return \is_array($result) && \array_key_exists('status', $result) && $result['status'] === 'OK';
}

/**
* @return null|string
*/
private function getValidatedCacheData(): ?string
{
//Get data from the cache, that can be not valid
$googleData = $this->cache->getData($this);

if (!\is_array($googleData) || !$this->isTimezoneResultCorrect($googleData)) {
$googleData = $this->cache->updateData($this);
}

return $googleData['timeZoneId'] ?? null;
}

/**
* @return Client
*/
private function getClient(): Client
{
if (!($this->client instanceof Client)) {
$this->client = new Client();
}

return $this->client;
}

/**
* @param float $lat
* @param float $lng
* @param \DateTime $dateTime
*
* @return string
*/
private function getApiUrl(float $lat, float $lng, \DateTime $dateTime): string
{
return \sprintf(
'https://maps.googleapis.com/maps/api/timezone/json?location=%s,%s&timestamp=%s&key=%s',
$lat,
$lng,
$dateTime->getTimestamp(),
$this->apiKey
);
}
}

0 comments on commit fef922c

Please sign in to comment.