|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PtrTn\Battlerite; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
use Doctrine\Common\Cache\FilesystemCache; |
|
7
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
|
8
|
|
|
use PtrTn\Battlerite\Dto\Match\DetailedMatch; |
|
9
|
|
|
use PtrTn\Battlerite\Dto\Player\DetailedPlayer; |
|
10
|
|
|
|
|
11
|
|
|
class ClientWithCache |
|
12
|
|
|
{ |
|
13
|
|
|
private const CACHE_DIR = '/tmp/battlerite'; |
|
14
|
|
|
|
|
15
|
|
|
private const CACHE_PREFIX = 'battlerite-'; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Client |
|
19
|
|
|
*/ |
|
20
|
|
|
private $client; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Cache |
|
24
|
|
|
*/ |
|
25
|
|
|
private $cache; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private $cacheLifetime = 300; |
|
31
|
|
|
|
|
32
|
4 |
|
public function __construct(Client $client, Cache $cache, int $cacheLifetime) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$this->client = $client; |
|
35
|
4 |
|
$this->cache = $cache; |
|
36
|
4 |
|
$this->cacheLifetime = $cacheLifetime; |
|
37
|
4 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Create default API client setup with a filesystem caching layer |
|
41
|
|
|
*/ |
|
42
|
1 |
|
public static function create(string $apiKey): self |
|
43
|
|
|
{ |
|
44
|
1 |
|
return new self( |
|
45
|
1 |
|
new Client( |
|
46
|
1 |
|
new ApiClient( |
|
47
|
1 |
|
$apiKey, |
|
48
|
1 |
|
new GuzzleClient() |
|
49
|
|
|
) |
|
50
|
|
|
), |
|
51
|
1 |
|
new FilesystemCache(self::CACHE_DIR, '.cache'), |
|
52
|
1 |
|
300 |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Create default API client setup with a custom caching layer |
|
58
|
|
|
*/ |
|
59
|
1 |
|
public static function createWithCache(string $apiKey, Cache $cache, int $cacheLifetime): self |
|
60
|
|
|
{ |
|
61
|
1 |
|
return new self( |
|
62
|
1 |
|
new Client( |
|
63
|
1 |
|
new ApiClient( |
|
64
|
1 |
|
$apiKey, |
|
65
|
1 |
|
new GuzzleClient() |
|
66
|
|
|
) |
|
67
|
|
|
), |
|
68
|
1 |
|
$cache, |
|
69
|
1 |
|
$cacheLifetime |
|
70
|
|
|
); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
1 |
|
public function getPlayer(string $playerId): DetailedPlayer |
|
74
|
|
|
{ |
|
75
|
1 |
|
$cacheKey = self::CACHE_PREFIX . '-player-' . $playerId; |
|
76
|
1 |
|
if ($this->cache->contains($cacheKey)) { |
|
77
|
1 |
|
return $this->cache->fetch($cacheKey); |
|
78
|
|
|
} |
|
79
|
1 |
|
$data = $this->client->getPlayer($playerId); |
|
80
|
1 |
|
$this->cache->save($cacheKey, $data, $this->cacheLifetime); |
|
81
|
1 |
|
return $data; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
1 |
|
public function getMatch(string $matchId): DetailedMatch |
|
85
|
|
|
{ |
|
86
|
1 |
|
$cacheKey = self::CACHE_PREFIX . '-match-' . $matchId; |
|
87
|
1 |
|
if ($this->cache->contains($cacheKey)) { |
|
88
|
1 |
|
return $this->cache->fetch($cacheKey); |
|
89
|
|
|
} |
|
90
|
1 |
|
$data = $this->client->getMatch($matchId); |
|
91
|
1 |
|
$this->cache->save($cacheKey, $data, $this->cacheLifetime); |
|
92
|
1 |
|
return $data; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|