Skip to content

Commit fef922c

Browse files
authored
Merge pull request Rentberry#8 from ft-1408/add-maps-timezone
Added maps timezone
2 parents 2efb488 + 6532238 commit fef922c

File tree

2 files changed

+175
-3
lines changed

2 files changed

+175
-3
lines changed

src/MapsPlace.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,16 @@ class MapsPlace implements CacheInterface
5252
private $factory;
5353

5454
/**
55+
* MapsPlace constructor.
56+
*
5557
* @param LoggerInterface $logger
56-
* @param \Memcached $memcached
58+
* @param Cache $cache
5759
* @param string $apiKey
5860
* @param PlaceSimpleFactory $factory
5961
*/
60-
public function __construct(LoggerInterface $logger, \Memcached $memcached, string $apiKey, PlaceSimpleFactory $factory)
62+
public function __construct(LoggerInterface $logger, Cache $cache, string $apiKey, PlaceSimpleFactory $factory)
6163
{
62-
$this->cache = new Cache($memcached);
64+
$this->cache = $cache;
6365
$this->apiKey = $apiKey;
6466
$this->logger = $logger;
6567
$this->factory = $factory;

src/MapsTimezone.php

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Rentberry\MapsUtils;
4+
5+
use GuzzleHttp\Client;
6+
use Psr\Log\LoggerInterface;
7+
use Rentberry\MapsUtils\Cache\Cache;
8+
use Rentberry\MapsUtils\Cache\CacheInterface;
9+
10+
/**
11+
* MapsTimezone
12+
*/
13+
class MapsTimezone implements CacheInterface
14+
{
15+
private const LOCATION_ROUND_LEVEL = 3;
16+
/**
17+
* @var Cache
18+
*/
19+
protected $cache;
20+
21+
/**
22+
* @var LoggerInterface
23+
*/
24+
protected $logger;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $query;
30+
31+
/**
32+
* @var string
33+
*/
34+
private $apiKey;
35+
36+
/**
37+
* @var Client
38+
*/
39+
private $client;
40+
41+
/**
42+
* @var float
43+
*/
44+
private $lat;
45+
46+
/**
47+
* @var float
48+
*/
49+
private $lng;
50+
51+
/**
52+
* MapsTimezone constructor.
53+
*
54+
* @param LoggerInterface $logger
55+
* @param Cache $cache
56+
* @param string $apiKey
57+
*/
58+
public function __construct(LoggerInterface $logger, Cache $cache, string $apiKey)
59+
{
60+
$this->cache = $cache;
61+
$this->apiKey = $apiKey;
62+
$this->logger = $logger;
63+
}
64+
65+
/**
66+
* @param float $lat
67+
* @param float $lng
68+
*
69+
* @return null|string
70+
*/
71+
public function getTimezonePoint(float $lat, float $lng): ?string
72+
{
73+
$this->lat = $lat;
74+
$this->lng = $lng;
75+
76+
return $googleData = $this->getValidatedCacheData();
77+
}
78+
79+
/**
80+
* Return google raw timezone data
81+
*
82+
* @return mixed[]|null
83+
*/
84+
public function getData(): ?array
85+
{
86+
try {
87+
$roundedLattitude = \round($this->lat, self::LOCATION_ROUND_LEVEL);
88+
$roundedLongitude = \round($this->lng, self::LOCATION_ROUND_LEVEL);
89+
$dt = new \DateTime();
90+
91+
$result = $this->getClient()->get($this->getApiUrl($roundedLattitude, $roundedLongitude, $dt), ['timeout' => 10]);
92+
$result = \json_decode($result->getBody()->getContents(), true);
93+
94+
// Going to save data only if it's correct
95+
if ($this->isTimezoneResultCorrect($result)) {
96+
return $result;
97+
}
98+
} catch (\Throwable $e) {
99+
$this->logger->warning('Problem with getting google place data. Error: '.$e->getMessage());
100+
}
101+
102+
return null;
103+
}
104+
105+
/**
106+
* @return string
107+
*/
108+
public function getCacheKey(): string
109+
{
110+
$roundedLattitude = \round($this->lat, self::LOCATION_ROUND_LEVEL);
111+
$roundedLongitude = \round($this->lng, self::LOCATION_ROUND_LEVEL);
112+
113+
return self::class.$roundedLattitude.$roundedLongitude;
114+
}
115+
116+
/**
117+
* @param mixed[] $result
118+
*
119+
* @return bool
120+
*/
121+
private function isTimezoneResultCorrect(array $result): bool
122+
{
123+
return \is_array($result) && \array_key_exists('status', $result) && $result['status'] === 'OK';
124+
}
125+
126+
/**
127+
* @return null|string
128+
*/
129+
private function getValidatedCacheData(): ?string
130+
{
131+
//Get data from the cache, that can be not valid
132+
$googleData = $this->cache->getData($this);
133+
134+
if (!\is_array($googleData) || !$this->isTimezoneResultCorrect($googleData)) {
135+
$googleData = $this->cache->updateData($this);
136+
}
137+
138+
return $googleData['timeZoneId'] ?? null;
139+
}
140+
141+
/**
142+
* @return Client
143+
*/
144+
private function getClient(): Client
145+
{
146+
if (!($this->client instanceof Client)) {
147+
$this->client = new Client();
148+
}
149+
150+
return $this->client;
151+
}
152+
153+
/**
154+
* @param float $lat
155+
* @param float $lng
156+
* @param \DateTime $dateTime
157+
*
158+
* @return string
159+
*/
160+
private function getApiUrl(float $lat, float $lng, \DateTime $dateTime): string
161+
{
162+
return \sprintf(
163+
'https://maps.googleapis.com/maps/api/timezone/json?location=%s,%s&timestamp=%s&key=%s',
164+
$lat,
165+
$lng,
166+
$dateTime->getTimestamp(),
167+
$this->apiKey
168+
);
169+
}
170+
}

0 commit comments

Comments
 (0)