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
|
|
|
* @author lixiao <[email protected]> |
22
|
|
|
* @copyright 2016 |
23
|
|
|
* |
24
|
|
|
* @see https://github.com/overtrue |
25
|
|
|
* @see http://overtrue.me |
26
|
|
|
*/ |
27
|
|
|
|
28
|
|
|
namespace EasyWeChat\OpenPlatform; |
29
|
|
|
|
30
|
|
|
use Doctrine\Common\Cache\Cache; |
31
|
|
|
use EasyWeChat\Core\Exceptions\RuntimeException; |
32
|
|
|
use EasyWeChat\OpenPlatform\Traits\Caches; |
33
|
|
|
use EasyWeChat\Support\Collection; |
34
|
|
|
|
35
|
|
|
class VerifyTicket |
36
|
|
|
{ |
37
|
|
|
use Caches; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* App Id. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $appId; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Cache Key. |
48
|
|
|
* |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
private $cacheKey; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Component verify ticket xml structure name. |
55
|
|
|
* |
56
|
|
|
* @var string |
57
|
|
|
*/ |
58
|
|
|
protected $ticketXmlName = 'ComponentVerifyTicket'; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Cache key prefix. |
62
|
|
|
* |
63
|
|
|
* @var string |
64
|
|
|
*/ |
65
|
|
|
protected $prefix = 'easywechat.open_platform.component_verify_ticket.'; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* VerifyTicket constructor. |
69
|
|
|
* |
70
|
|
|
* @param string $appId |
71
|
|
|
* @param Cache $cache |
72
|
|
|
*/ |
73
|
|
|
public function __construct($appId, Cache $cache = null) |
74
|
|
|
{ |
75
|
|
|
$this->appId = $appId; |
76
|
|
|
$this->setCache($cache); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Save component verify ticket. |
81
|
|
|
* |
82
|
|
|
* @param Collection $message |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function cache(Collection $message) |
87
|
|
|
{ |
88
|
|
|
return $this->set( |
89
|
|
|
$this->getCacheKey(), |
90
|
|
|
$message->get($this->ticketXmlName) |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get component verify ticket. |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
* |
99
|
|
|
* @throws RuntimeException |
100
|
|
|
*/ |
101
|
|
|
public function getTicket() |
102
|
|
|
{ |
103
|
|
|
if ($cached = $this->get($this->getCacheKey())) { |
104
|
|
|
return $cached; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
throw new RuntimeException('Component verify ticket does not exists.'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Set component verify ticket cache key. |
112
|
|
|
* |
113
|
|
|
* @param string $cacheKey |
114
|
|
|
* |
115
|
|
|
* @return $this |
116
|
|
|
*/ |
117
|
|
|
public function setCacheKey($cacheKey) |
118
|
|
|
{ |
119
|
|
|
$this->cacheKey = $cacheKey; |
120
|
|
|
|
121
|
|
|
return $this; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Get component verify ticket cache key. |
126
|
|
|
* |
127
|
|
|
* @return string $this->cacheKey |
128
|
|
|
*/ |
129
|
|
|
public function getCacheKey() |
130
|
|
|
{ |
131
|
|
|
if (is_null($this->cacheKey)) { |
132
|
|
|
return $this->prefix . $this->appId; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $this->cacheKey; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
It seems like you allow that null is being passed for a parameter, however the function which is called does not seem to accept null.
We recommend to add an additional type check (or disallow null for the parameter):