Skip to content

Commit 75b0fee

Browse files
authored
Merge branch 'master' into master
2 parents 4ec2bba + d3bf7b6 commit 75b0fee

9 files changed

+240
-48
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"escapestudios/symfony2-coding-standard": "^3.0",
2727
"slevomat/coding-standard": "^4.0",
2828
"friendsofphp/php-cs-fixer": "^2.3",
29-
"monolog/monolog": "^1.23"
29+
"monolog/monolog": "^1.23",
30+
"psr/simple-cache": "^1.0"
3031
},
3132
"autoload": {
3233
"psr-4": {

ruleset.xml

+4
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@
2121
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation.NonFullyQualifiedClassName"/>
2222
<exclude name="SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions"/>
2323
<exclude name="SlevomatCodingStandard.Files.TypeNameMatchesFileName"/>
24+
<exclude name="SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment.MultiLinePropertyComment"/>
25+
<exclude name="SlevomatCodingStandard.Commenting.DocCommentSpacing.IncorrectLinesCountBetweenDifferentAnnotationsTypes"/>
26+
<exclude name="SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming.SuperfluousSuffix"/>
27+
<exclude name="SlevomatCodingStandard.ControlStructures.EarlyExit"/>
2428
</rule>
2529

2630
<rule ref="SlevomatCodingStandard.Types.EmptyLinesAroundTypeBraces">

src/Cache/Cache.php

+11-9
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,37 @@
22

33
namespace Rentberry\MapsUtils\Cache;
44

5+
use Psr\SimpleCache\CacheInterface;
6+
57
/**
68
* Cache service
79
*/
810
class Cache
911
{
1012
/**
11-
* @var \Memcached
13+
* @var CacheInterface
1214
*/
1315
protected $cache;
1416

1517
/**
16-
* @param \Memcached $cache
18+
* @param CacheInterface $cache
1719
*/
18-
public function __construct(\Memcached $cache)
20+
public function __construct(CacheInterface $cache)
1921
{
2022
$this->cache = $cache;
2123
}
2224

2325
/**
24-
* @param CacheInterface $object
26+
* @param CacheableInterface $object
2527
*
2628
* @return false|mixed
2729
*/
28-
public function getData(CacheInterface $object)
30+
public function getData(CacheableInterface $object)
2931
{
3032
$cacheKey = $object->getCacheKey();
31-
$data = $this->cache->get($cacheKey);
33+
$data = $this->cache->get($cacheKey, null);
3234

33-
if ($data !== false) {
35+
if ($data !== null) {
3436
return $data;
3537
}
3638

@@ -46,11 +48,11 @@ public function getData(CacheInterface $object)
4648
/**
4749
* Force update data in cache
4850
*
49-
* @param CacheInterface $object
51+
* @param CacheableInterface $object
5052
*
5153
* @return false|mixed
5254
*/
53-
public function updateData(CacheInterface $object)
55+
public function updateData(CacheableInterface $object)
5456
{
5557
$cacheKey = $object->getCacheKey();
5658
$data = $object->getData();

src/Cache/CacheInterface.php src/Cache/CacheableInterface.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
namespace Rentberry\MapsUtils\Cache;
44

55
/**
6-
* Interface Cache
6+
* Interface Cacheable
77
*/
8-
interface CacheInterface
8+
interface CacheableInterface
99
{
1010
/**
1111
* @return mixed

src/Cache/CacheObject.php src/Cache/CacheableObject.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* Cache object
77
*/
8-
class CacheObject implements CacheInterface
8+
class CacheableObject implements CacheableInterface
99
{
1010
/**
1111
* @var string
@@ -36,9 +36,9 @@ public function getData()
3636
/**
3737
* @param string $cacheKey
3838
*
39-
* @return CacheObject
39+
* @return CacheableObject
4040
*/
41-
public function setCacheKey(string $cacheKey): CacheObject
41+
public function setCacheKey(string $cacheKey): CacheableObject
4242
{
4343
$this->cacheKey = $cacheKey;
4444

@@ -48,9 +48,9 @@ public function setCacheKey(string $cacheKey): CacheObject
4848
/**
4949
* @param mixed $data
5050
*
51-
* @return CacheObject
51+
* @return CacheableObject
5252
*/
53-
public function setData($data): CacheObject
53+
public function setData($data): CacheableObject
5454
{
5555
$this->data = $data;
5656

src/MapsPlace.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
use GuzzleHttp\Client;
66
use Psr\Log\LoggerInterface;
77
use Rentberry\MapsUtils\Cache\Cache;
8-
use Rentberry\MapsUtils\Cache\CacheInterface;
8+
use Rentberry\MapsUtils\Cache\CacheableInterface;
99
use Rentberry\MapsUtils\Objects\Place;
1010

1111
/**
1212
* MapsPlace
1313
*/
14-
class MapsPlace implements CacheInterface
14+
class MapsPlace implements CacheableInterface
1515
{
1616
public const QUERY_TYPE_ADDRESS = 'address';
1717
public const QUERY_TYPE_PLACE_ID = 'place_id';
@@ -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\CacheableInterface;
9+
10+
/**
11+
* MapsTimezone
12+
*/
13+
class MapsTimezone implements CacheableInterface
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 \sprintf('%s_%s', self::class, sha1($roundedLattitude.$roundedLongitude, false));
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)