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

Proxy::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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
/**
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