Completed
Push — master ( 2e778b...f213b5 )
by Takashi
02:31
created

Proxy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 61.11%

Importance

Changes 0
Metric Value
dl 0
loc 58
ccs 11
cts 18
cp 0.6111
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getEmojis() 0 12 2
A getPost() 0 12 3
1
<?php
2
3
namespace Ttskch\Esa;
4
5
use Doctrine\Common\Cache\Cache;
6
use Polidog\Esa\Client;
7
8
/**
9
 * @see https://docs.esa.io/posts/102
10
 */
11
class Proxy
12
{
13
    /**
14
     * @var Client
15
     */
16
    private $client;
17
18
    /**
19
     * @var Cache
20
     */
21
    private $cache;
22
23
    const CACHE_KEY_PREFIX = 'ttskch.esa.proxy';
24
25
    /**
26
     * @param Client $client
27
     * @param Cache $cache
28
     */
29 4
    public function __construct(Client $client, Cache $cache)
30
    {
31 4
        $this->client = $client;
32 4
        $this->cache = $cache;
33 4
    }
34
35
    /**
36
     * @param int $postId
37
     * @param bool $force
38
     * @return array
39
     */
40 4
    public function getPost($postId, $force = false)
41
    {
42 4
        $cacheKey = sprintf('%s.post.%d', self::CACHE_KEY_PREFIX, $postId);
43
44 4
        if (!$force && $post = $this->cache->fetch($cacheKey)) {
45 1
            return $post;
46
        }
47
48 3
        $post = $this->client->post($postId);
49 3
        $this->cache->save($cacheKey, $post);
50
51 3
        return $post;
52
    }
53
54
    /**
55
     * @return array
56
     */
57
    public function getEmojis()
58
    {
59
        $cacheKey = sprintf('%s.emojis', self::CACHE_KEY_PREFIX);
60
61
        if ($emojis = $this->cache->fetch($cacheKey)) {
62
            return $emojis;
63
        }
64
65
        $emojis = $this->client->emojis(['include' => 'all'])['emojis'];
66
        $this->cache->save($cacheKey, $emojis);
67
68
        return $emojis;
69
    }
70
}
71