Completed
Push — master ( 8f5b55...c52b56 )
by frey
04:17 queued 01:50
created

Ticket::getCacheKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EntWeChat\Suite;
4
5
use Doctrine\Common\Cache\Cache;
6
use EntWeChat\Core\Exceptions\RuntimeException;
7
8
class Ticket
9
{
10
    /**
11
     * Cache manager.
12
     *
13
     * @var \Doctrine\Common\Cache\Cache
14
     */
15
    protected $cache;
16
17
    /**
18
     * Suite Id.
19
     *
20
     * @var string
21
     */
22
    protected $suiteId;
23
    /**
24
     * Cache key prefix.
25
     *
26
     * @var string
27
     */
28
    protected $prefix = 'EntWeChat.suite.suite_ticket.';
29
    /**
30
     * Cache Key.
31
     *
32
     * @var string
33
     */
34
    private $cacheKey;
35
36
    /**
37
     * Ticket constructor.
38
     *
39
     * @param string                       $suiteId
40
     * @param \Doctrine\Common\Cache\Cache $cache
41
     */
42
    public function __construct($suiteId, Cache $cache)
43
    {
44
        $this->suiteId = $suiteId;
45
        $this->cache = $cache;
46
    }
47
48
    /**
49
     * Set component verify ticket to the cache.
50
     *
51
     * @param string $ticket
52
     *
53
     * @return bool
54
     */
55
    public function setTicket($ticket)
56
    {
57
        return $this->cache->save($this->getCacheKey(), $ticket);
58
    }
59
60
    /**
61
     * Get component verify ticket cache key.
62
     *
63
     * @return string $this->cacheKey
64
     */
65
    public function getCacheKey()
66
    {
67
        if (is_null($this->cacheKey)) {
68
            return $this->prefix.$this->suiteId;
69
        }
70
71
        return $this->cacheKey;
72
    }
73
74
    /**
75
     * Set component verify ticket cache key.
76
     *
77
     * @param string $cacheKey
78
     *
79
     * @return $this
80
     */
81
    public function setCacheKey($cacheKey)
82
    {
83
        $this->cacheKey = $cacheKey;
84
85
        return $this;
86
    }
87
88
    /**
89
     * Get component verify ticket.
90
     *
91
     * @throws \EntWeChat\Core\Exceptions\RuntimeException
92
     *
93
     * @return string
94
     */
95
    public function getTicket()
96
    {
97
        if ($cached = $this->cache->fetch($this->getCacheKey())) {
98
            return $cached;
99
        }
100
101
        throw new RuntimeException('Suite ticket does not exists.');
102
    }
103
}
104