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
|
|
|
* App ID. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $appId; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Component verify ticket from wechat server. |
44
|
|
|
* |
45
|
|
|
* @var string |
46
|
|
|
*/ |
47
|
|
|
private $ticket; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Cache. |
51
|
|
|
* |
52
|
|
|
* @var Cache |
53
|
|
|
*/ |
54
|
|
|
private $cache; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Cache Key. |
58
|
|
|
* |
59
|
|
|
* @var string |
60
|
|
|
*/ |
61
|
|
|
private $cacheKey; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Cache key prefix. |
65
|
|
|
* |
66
|
|
|
* @var string |
67
|
|
|
*/ |
68
|
|
|
protected $prefix = 'easywechat.common.component_verify_ticket.'; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* VerifyTicket constructor. |
72
|
|
|
* |
73
|
|
|
* @param $appId |
74
|
|
|
* @param $ticket |
75
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
76
|
|
|
*/ |
77
|
|
|
public function __construct($appId, $ticket = null, Cache $cache = null) |
78
|
|
|
{ |
79
|
|
|
$this->appId = $appId; |
80
|
|
|
$this->ticket = $ticket; |
81
|
|
|
$this->cache = $cache; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Save component verify ticket. |
86
|
|
|
* |
87
|
|
|
* @param $appId |
88
|
|
|
* @param $cacheValue |
89
|
|
|
* |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
|
|
public static function cache($appId, $cacheValue) |
93
|
|
|
{ |
94
|
|
|
$instance = new static($appId, $cacheValue); |
95
|
|
|
|
96
|
|
|
return $instance->getCache()->save( |
97
|
|
|
$instance->getCacheKey(), $instance->ticket |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get component verify ticket. |
103
|
|
|
* |
104
|
|
|
* @param $appId |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
* |
108
|
|
|
* @throws RuntimeException |
109
|
|
|
*/ |
110
|
|
|
public static function getTicket($appId) |
111
|
|
|
{ |
112
|
|
|
$instance = new static($appId); |
113
|
|
|
$cached = $instance->getCache()->fetch($instance->getCacheKey()); |
114
|
|
|
|
115
|
|
|
if (empty($cached)) { |
116
|
|
|
throw new RuntimeException('Component verify ticket does not exists.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return $cached; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set cache. |
124
|
|
|
* |
125
|
|
|
* @param \Doctrine\Common\Cache\Cache $cache |
126
|
|
|
* |
127
|
|
|
* @return VerifyTicket |
128
|
|
|
*/ |
129
|
|
|
public function setCache(Cache $cache) |
130
|
|
|
{ |
131
|
|
|
$this->cache = $cache; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Return the cache manager. |
138
|
|
|
* |
139
|
|
|
* @return \Doctrine\Common\Cache\Cache |
140
|
|
|
*/ |
141
|
|
|
public function getCache() |
142
|
|
|
{ |
143
|
|
|
return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir()); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set component verify ticket cache key. |
148
|
|
|
* |
149
|
|
|
* @param string $cacheKey |
150
|
|
|
* |
151
|
|
|
* @return $this |
152
|
|
|
*/ |
153
|
|
|
public function setCacheKey($cacheKey) |
154
|
|
|
{ |
155
|
|
|
$this->cacheKey = $cacheKey; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get component verify ticket cache key. |
162
|
|
|
* |
163
|
|
|
* @return string $this->cacheKey |
164
|
|
|
*/ |
165
|
|
|
public function getCacheKey() |
166
|
|
|
{ |
167
|
|
|
if (is_null($this->cacheKey)) { |
168
|
|
|
return $this->prefix.$this->appId; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return $this->cacheKey; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|