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

SuiteTicket   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getCacheKey() 0 3 1
A setTicket() 0 5 1
A getTicket() 0 7 2
A __construct() 0 3 1
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
}