Completed
Pull Request — master (#1275)
by
unknown
03:05
created

SuiteTicket::getCacheKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace EasyWeChat\OpenWork\SuiteAuth;
4
5
use EasyWeChat\Kernel\Traits\InteractsWithCache;
6
use EasyWeChat\OpenWork\Application;
7
use EasyWeChat\Kernel\Exceptions\RuntimeException;
8
9
class SuiteTicket
10
{
11
    use InteractsWithCache;
12
13
    /**
14
     * @var Application
15
     */
16
    protected $app;
17
18
    /**
19
     * SuiteTicket constructor.
20
     * @param Application $app
21
     */
22
    public function __construct(Application $app)
23
    {
24
        $this->app = $app;
25
    }
26
27
    /**
28
     * @param string $ticket
29
     * @return $this
30
     * @throws \Psr\SimpleCache\InvalidArgumentException
31
     */
32
    public function setTicket(string $ticket)
33
    {
34
        $this->getCache()->set($this->getCacheKey(), $ticket, 600);
35
36
        return $this;
37
    }
38
39
    /**
40
     * @return string
41
     * @throws RuntimeException
42
     * @throws \Psr\SimpleCache\InvalidArgumentException
43
     */
44
    public function getTicket(): string
45
    {
46
        if ($cached = $this->getCache()->get($this->getCacheKey())) {
47
            return $cached;
48
        }
49
50
        throw new RuntimeException('Credential "suite_ticket" does not exist in cache.');
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    protected function getCacheKey(): string
57
    {
58
        return 'easywechat.open_work.suite_ticket.' . $this->app['config']['corp_id'];
59
    }
60
61
}