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\Matches\Matches; |
10
|
|
|
use PtrTn\Battlerite\Dto\Player\DetailedPlayer; |
11
|
|
|
use PtrTn\Battlerite\Dto\Players\Players; |
12
|
|
|
use PtrTn\Battlerite\Dto\Status\Status; |
13
|
|
|
use PtrTn\Battlerite\Query\MatchesQuery; |
14
|
|
|
use PtrTn\Battlerite\Query\PlayersQuery; |
15
|
|
|
|
16
|
|
|
class ClientWithCache |
17
|
|
|
{ |
18
|
|
|
private const CACHE_DIR = '/tmp/battlerite'; |
19
|
|
|
|
20
|
|
|
private const CACHE_PREFIX = 'battlerite-'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Client |
24
|
|
|
*/ |
25
|
|
|
private $client; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Cache |
29
|
|
|
*/ |
30
|
|
|
private $cache; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var int |
34
|
|
|
*/ |
35
|
|
|
private $cacheLifetime = 300; |
36
|
|
|
|
37
|
7 |
|
public function __construct(Client $client, Cache $cache, int $cacheLifetime) |
38
|
|
|
{ |
39
|
7 |
|
$this->client = $client; |
40
|
7 |
|
$this->cache = $cache; |
41
|
7 |
|
$this->cacheLifetime = $cacheLifetime; |
42
|
7 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create default API client setup with a filesystem caching layer |
46
|
|
|
*/ |
47
|
1 |
|
public static function create(string $apiKey): self |
48
|
|
|
{ |
49
|
1 |
|
return new self( |
50
|
1 |
|
new Client( |
51
|
1 |
|
new ApiClient( |
52
|
1 |
|
$apiKey, |
53
|
1 |
|
new GuzzleClient() |
54
|
|
|
) |
55
|
|
|
), |
56
|
1 |
|
new FilesystemCache(self::CACHE_DIR, '.cache'), |
57
|
1 |
|
300 |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create default API client setup with a custom caching layer |
63
|
|
|
*/ |
64
|
1 |
|
public static function createWithCache(string $apiKey, Cache $cache, int $cacheLifetime): self |
65
|
|
|
{ |
66
|
1 |
|
return new self( |
67
|
1 |
|
new Client( |
68
|
1 |
|
new ApiClient( |
69
|
1 |
|
$apiKey, |
70
|
1 |
|
new GuzzleClient() |
71
|
|
|
) |
72
|
|
|
), |
73
|
1 |
|
$cache, |
74
|
1 |
|
$cacheLifetime |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
1 |
|
public function getPlayer(string $playerId): DetailedPlayer |
79
|
|
|
{ |
80
|
1 |
|
$cacheKey = self::CACHE_PREFIX . '-player-' . $playerId; |
81
|
1 |
|
if ($this->cache->contains($cacheKey)) { |
82
|
1 |
|
return $this->cache->fetch($cacheKey); |
83
|
|
|
} |
84
|
1 |
|
$data = $this->client->getPlayer($playerId); |
85
|
1 |
|
$this->cache->save($cacheKey, $data, $this->cacheLifetime); |
86
|
1 |
|
return $data; |
87
|
|
|
} |
88
|
|
|
|
89
|
1 |
|
public function getMatch(string $matchId): DetailedMatch |
90
|
|
|
{ |
91
|
1 |
|
$cacheKey = self::CACHE_PREFIX . '-match-' . $matchId; |
92
|
1 |
|
if ($this->cache->contains($cacheKey)) { |
93
|
1 |
|
return $this->cache->fetch($cacheKey); |
94
|
|
|
} |
95
|
1 |
|
$data = $this->client->getMatch($matchId); |
96
|
1 |
|
$this->cache->save($cacheKey, $data, $this->cacheLifetime); |
97
|
1 |
|
return $data; |
98
|
|
|
} |
99
|
|
|
|
100
|
1 |
|
public function getStatus(): Status |
101
|
|
|
{ |
102
|
1 |
|
return $this->client->getStatus(); |
103
|
|
|
} |
104
|
|
|
|
105
|
1 |
|
public function getMatches(MatchesQuery $query = null): Matches |
106
|
|
|
{ |
107
|
1 |
|
return $this->client->getMatches($query); |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
public function getPlayers(PlayersQuery $query = null): Players |
111
|
|
|
{ |
112
|
1 |
|
return $this->client->getPlayers($query); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|