Passed
Pull Request — master (#1278)
by
unknown
03:33
created

SuiteTicket::getTicket()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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