ClientWithCache::getPlayers()   A
last analyzed

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