| Total Complexity | 6 |
| Total Lines | 59 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 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 \Psr\SimpleCache\InvalidArgumentException |
||
| 48 | * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException |
||
| 49 | */ |
||
| 50 | 3 | public function setTicket(string $ticket) |
|
| 51 | { |
||
| 52 | 3 | $ok = $this->getCache()->set($this->getCacheKey(), $ticket, 1800); |
|
| 53 | |||
| 54 | 3 | if (!$ok) { |
|
| 55 | 1 | throw new RuntimeException('Failed to cache suite ticket.'); |
|
| 56 | } |
||
| 57 | |||
| 58 | 2 | return $this; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return string |
||
| 63 | * |
||
| 64 | * @throws RuntimeException |
||
| 65 | * @throws \Psr\SimpleCache\InvalidArgumentException |
||
| 66 | */ |
||
| 67 | 3 | public function getTicket(): string |
|
| 68 | { |
||
| 69 | 3 | if ($cached = $this->getCache()->get($this->getCacheKey())) { |
|
| 70 | 2 | return $cached; |
|
| 71 | } |
||
| 72 | |||
| 73 | 1 | throw new RuntimeException('Credential "suite_ticket" does not exist in cache.'); |
|
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @return string |
||
| 78 | */ |
||
| 79 | 5 | protected function getCacheKey(): string |
|
| 82 | } |
||
| 83 | } |
||
| 84 |