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

Proxy::getPost()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
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