|
1
|
|
|
<?php |
|
2
|
|
|
namespace Germania\IpstackClient; |
|
3
|
|
|
|
|
4
|
|
|
use Germania\IpstackClient\IpstackClientInterface; |
|
5
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
6
|
|
|
use Psr\Log\LoggerInterface; |
|
7
|
|
|
use Psr\Log\NullLogger; |
|
8
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
9
|
|
|
|
|
10
|
|
|
class IpstackClientPsr6CacheDecorator implements IpstackClientInterface |
|
11
|
|
|
{ |
|
12
|
|
|
use LoggerAwareTrait; |
|
13
|
|
|
|
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @var CacheItemPoolInterface |
|
17
|
|
|
*/ |
|
18
|
|
|
public $cache; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var int|null |
|
22
|
|
|
*/ |
|
23
|
|
|
public $cache_lifetime; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
public $loglevel_name; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param IpstackClientInterface $ipstack_client |
|
33
|
|
|
* @param CacheItemPoolInterface $cache PSR-6 Cache ItemPool |
|
34
|
|
|
*/ |
|
35
|
24 |
|
public function __construct( IpstackClientInterface $ipstack_client, CacheItemPoolInterface $cache, LoggerInterface $logger = null, string $loglevel_name = "info" ) |
|
36
|
|
|
{ |
|
37
|
24 |
|
$this->ipstack_client = $ipstack_client; |
|
|
|
|
|
|
38
|
24 |
|
$this->loglevel_name = $loglevel_name; |
|
39
|
24 |
|
$this->cache = $cache; |
|
40
|
24 |
|
$this->setLogger( $logger ?: new NullLogger ); |
|
41
|
24 |
|
} |
|
42
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param int|null $lifetime Cache Lifetime in seconds (or NULL) |
|
46
|
|
|
* @return self |
|
47
|
|
|
*/ |
|
48
|
8 |
|
public function setCacheLifeTime( $lifetime ) |
|
49
|
|
|
{ |
|
50
|
8 |
|
$this->cache_lifetime = $lifetime; |
|
51
|
8 |
|
return $this; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return int|null |
|
56
|
|
|
*/ |
|
57
|
24 |
|
public function getCacheLifeTime() |
|
58
|
|
|
{ |
|
59
|
24 |
|
return $this->cache_lifetime; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|
|
63
|
16 |
|
public function get( string $client_ip, array $custom_query = array() ) : array |
|
64
|
|
|
{ |
|
65
|
|
|
|
|
66
|
16 |
|
$item = $this->cache->getItem( sha1($client_ip) ); |
|
67
|
|
|
|
|
68
|
16 |
|
$ipstack = $item->get(); |
|
69
|
|
|
|
|
70
|
|
|
// No valid item? |
|
71
|
16 |
|
if(!$item->isHit()): |
|
72
|
16 |
|
$this->logger->debug("No ipstack info in cache"); |
|
73
|
|
|
|
|
74
|
|
|
// Run intensive code: Ask ipstack directly |
|
75
|
16 |
|
$ipstack = $this->ipstack_client->get( $client_ip, $custom_query ); |
|
76
|
|
|
|
|
77
|
|
|
// Store data for future use. |
|
78
|
16 |
|
$this->logger->log( $this->loglevel_name, "Write ipstack info to cache", $ipstack); |
|
79
|
16 |
|
$item->set($ipstack); |
|
80
|
|
|
|
|
81
|
16 |
|
$lifetime = $this->getCacheLifeTime(); |
|
82
|
16 |
|
$item->expiresAfter($lifetime); |
|
83
|
|
|
|
|
84
|
16 |
|
$this->cache->save($item); |
|
85
|
|
|
|
|
86
|
|
|
// Valid item found |
|
87
|
|
|
else: |
|
88
|
|
|
$this->logger->log($this->loglevel_name, "Found ipstack info in cache", $ipstack); |
|
89
|
|
|
endif; |
|
90
|
|
|
|
|
91
|
16 |
|
return $ipstack; |
|
92
|
|
|
|
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: