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
|
|
|
namespace EasyWeChat\OpenWork\SuiteAuth; |
13
|
|
|
|
14
|
|
|
use EasyWeChat\Kernel\Exceptions\RuntimeException; |
15
|
|
|
use EasyWeChat\Kernel\Traits\InteractsWithCache; |
16
|
|
|
use EasyWeChat\OpenWork\Application; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* SuiteTicket. |
20
|
|
|
* |
21
|
|
|
* @author xiaomin <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class SuiteTicket |
24
|
|
|
{ |
25
|
|
|
use InteractsWithCache; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var Application |
29
|
|
|
*/ |
30
|
|
|
protected $app; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* SuiteTicket constructor. |
34
|
|
|
* |
35
|
|
|
* @param Application $app |
36
|
|
|
*/ |
37
|
5 |
|
public function __construct(Application $app) |
38
|
|
|
{ |
39
|
5 |
|
$this->app = $app; |
40
|
5 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param string $ticket |
44
|
|
|
* |
45
|
|
|
* @return $this |
46
|
|
|
* |
47
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
48
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
49
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
50
|
|
|
*/ |
51
|
3 |
|
public function setTicket(string $ticket) |
52
|
|
|
{ |
53
|
3 |
|
$this->getCache()->set($this->getCacheKey(), $ticket, 1800); |
54
|
|
|
|
55
|
3 |
|
if (!$this->getCache()->has($this->getCacheKey())) { |
56
|
1 |
|
throw new RuntimeException('Failed to cache suite ticket.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
2 |
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
* |
65
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException |
66
|
|
|
* @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
67
|
|
|
* @throws \Psr\SimpleCache\InvalidArgumentException |
68
|
|
|
*/ |
69
|
3 |
|
public function getTicket(): string |
70
|
|
|
{ |
71
|
3 |
|
if ($cached = $this->getCache()->get($this->getCacheKey())) { |
72
|
2 |
|
return $cached; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
throw new RuntimeException('Credential "suite_ticket" does not exist in cache.'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return string |
80
|
|
|
*/ |
81
|
5 |
|
protected function getCacheKey(): string |
82
|
|
|
{ |
83
|
5 |
|
return 'easywechat.open_work.suite_ticket.'.$this->app['config']['suite_id']; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|