Passed
Branch master (4f08c0)
by Takashi
07:18 queued 05:07
created

Proxy   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 58
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
class Proxy
9
{
10
    /**
11
     * @var Client
12
     */
13
    private $client;
14
15
    /**
16
     * @var Cache
17
     */
18
    private $cache;
19
20
    const CACHE_KEY_PREFIX = 'ttskch.esa.proxy.';
21
22
    /**
23
     * @param Client $client
24
     * @param Cache $cache
25
     */
26
    public function __construct(Client $client, Cache $cache)
27
    {
28
        $this->client = $client;
29
        $this->cache = $cache;
30
    }
31
32
    /**
33
     * @param int $postId
34
     * @param bool $force
35
     * @return array
36
     */
37
    public function getPost($postId, $force = false)
38
    {
39
        $cacheKey = sprintf('%s.post.%d', self::CACHE_KEY_PREFIX, $postId);
40
41
        if (!$force && $post = $this->cache->fetch($cacheKey)) {
42
            return $post;
43
        }
44
45
        $post = $this->client->post($postId);
46
        $this->cache->save($cacheKey, $post);
47
48
        return $post;
49
    }
50
51
    /**
52
     * @return array
53
     */
54
    public function getEmojis()
55
    {
56
        $cacheKey = sprintf('%s.emojis', self::CACHE_KEY_PREFIX);
57
58
        if ($emojis = $this->cache->fetch($cacheKey)) {
59
            return $emojis;
60
        }
61
62
        $emojis = $this->client->emojis(['include' => 'all'])['emojis'];
63
        $this->cache->save($cacheKey, $emojis);
64
65
        return $emojis;
66
    }
67
}
68