Completed
Pull Request — master (#4)
by Peter
02:41 queued 32s
created

ClientWithCache::getStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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