Completed
Pull Request — master (#1001)
by mingyoung
01:33
created

Client::getKey()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 16
rs 9.4285
c 0
b 0
f 0
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\Payment\Sandbox;
13
14
use EasyWeChat\Kernel\Traits\InteractsWithCache;
15
use EasyWeChat\Payment\Kernel\BaseClient;
16
use EasyWeChat\Payment\Kernel\Exceptions\SandboxException;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author mingyoung <[email protected]>
22
 */
23
class Client extends BaseClient
24
{
25
    use InteractsWithCache;
26
27
    /**
28
     * @return string
29
     *
30
     * @throws \EasyWeChat\Payment\Kernel\Exceptions\SandboxException
31
     */
32
    public function getKey(): string
33
    {
34
        if ($cache = $this->getCache()->get($this->getCacheKey())) {
35
            return $cache;
36
        }
37
38
        $response = $this->request('sandboxnew/pay/getsignkey');
39
40
        if ($response['return_code'] === 'SUCCESS') {
41
            $this->getCache()->set($this->getCacheKey(), $key = $response['sandbox_signkey'], 24 * 3600);
42
43
            return $key;
44
        }
45
46
        throw new SandboxException($response['return_msg']);
47
    }
48
49
    /**
50
     * @return string
51
     */
52
    protected function getCacheKey(): string
53
    {
54
        return 'easywechat.payment.sandbox.'.md5($this->app['config']->app_id.$this->app['config']['mch_id']);
55
    }
56
}
57