Completed
Pull Request — master (#553)
by
unknown
02:23
created

VerifyTicket::setCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the overtrue/wechat.
5
 *
6
 * (c) overtrue <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
/**
13
 * VerifyTicket.php.
14
 *
15
 * Part of Overtrue\WeChat.
16
 *
17
 * For the full copyright and license information, please view the LICENSE
18
 * file that was distributed with this source code.
19
 *
20
 * @author    mingyoung <[email protected]>
21
 * @copyright 2016
22
 *
23
 * @see      https://github.com/overtrue
24
 * @see      http://overtrue.me
25
 */
26
27
namespace EasyWeChat\OpenPlatform;
28
29
use Doctrine\Common\Cache\Cache;
30
use Doctrine\Common\Cache\FilesystemCache;
31
use EasyWeChat\Core\Exceptions\RuntimeException;
32
33
class VerifyTicket
34
{
35
36
    /**
37
     * App ID.
38
     *
39
     * @var string
40
     */
41
    private $appId;
42
43
    /**
44
     * Component verify ticket from wechat server.
45
     *
46
     * @var string
47
     */
48
    private $ticket;
49
50
    /**
51
     * Cache.
52
     *
53
     * @var Cache
54
     */
55
    private $cache;
56
57
    /**
58
     * Cache Key.
59
     *
60
     * @var string
61
     */
62
    private $cacheKey;
63
64
    /**
65
     * Cache key prefix.
66
     *
67
     * @var string
68
     */
69
    protected $prefix = 'easywechat.common.component_verify_ticket.';
70
71
    /**
72
     * VerifyTicket constructor.
73
     *
74
     * @param $appId
75
     * @param $ticket
76
     * @param \Doctrine\Common\Cache\Cache $cache
77
     */
78
    public function __construct($appId, $ticket = null, Cache $cache = null)
79
    {
80
        $this->appId = $appId;
81
        $this->ticket = $ticket;
82
        $this->cache = $cache;
83
    }
84
85
    /**
86
     * Save component verify ticket.
87
     *
88
     * @param $appId
89
     * @param $cacheValue
90
     *
91
     * @return bool
92
     */
93
    public static function cache($appId, $cacheValue)
94
    {
95
        $instance = new static($appId, $cacheValue);
96
97
        return $instance->getCache()->save(
98
            $instance->getCacheKey(), $instance->ticket
99
        );
100
    }
101
102
    /**
103
     * Get component verify ticket.
104
     *
105
     * @param $appId
106
     *
107
     * @return string
108
     *
109
     * @throws RuntimeException
110
     */
111
    public static function getTicket($appId)
112
    {
113
        $instance = new static($appId);
114
        $cached = $instance->getCache()->fetch($instance->getCacheKey());
115
116
        if (empty($cached)) {
117
            throw new RuntimeException("Component verify ticket does not exists.");
118
        }
119
120
        return $cached;
121
    }
122
123
124
    /**
125
     * Set cache.
126
     *
127
     * @param \Doctrine\Common\Cache\Cache $cache
128
     *
129
     * @return VerifyTicket
130
     */
131
    public function setCache(Cache $cache)
132
    {
133
        $this->cache = $cache;
134
135
        return $this;
136
    }
137
138
139
    /**
140
     * Return the cache manager.
141
     *
142
     * @return \Doctrine\Common\Cache\Cache
143
     */
144
    public function getCache()
145
    {
146
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
147
    }
148
149
    /**
150
     * Set component verify ticket cache key.
151
     *
152
     * @param string $cacheKey
153
     *
154
     * @return $this
155
     */
156
    public function setCacheKey($cacheKey)
157
    {
158
        $this->cacheKey = $cacheKey;
159
160
        return $this;
161
    }
162
163
    /**
164
     * Get component verify ticket cache key.
165
     *
166
     * @return string $this->cacheKey
167
     */
168
    public function getCacheKey()
169
    {
170
        if (is_null($this->cacheKey)) {
171
            return $this->prefix . $this->appId;
172
        }
173
174
        return $this->cacheKey;
175
    }
176
}
177