Completed
Push — master ( 158640...6228b6 )
by Carlos
02:38 queued 01:15
created

Client::key()   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
    const ENDPOINT = 'pay/getsignkey';
28
29
    /**
30
     * @return string
31
     *
32
     * @throws \EasyWeChat\Payment\Kernel\Exceptions\SandboxException
33
     */
34
    public function key(): string
35
    {
36
        if ($cache = $this->getCache()->get($this->getCacheKey())) {
37
            return $cache;
38
        }
39
40
        $response = $this->resolveResponse($this->requestRaw(self::ENDPOINT), 'array');
41
42
        if ($response['return_code'] === 'SUCCESS') {
43
            $this->getCache()->set($this->getCacheKey(), $key = $response['sandbox_signkey'], 24 * 3600);
44
45
            return $key;
46
        }
47
48
        throw new SandboxException($response['return_msg']);
49
    }
50
51
    /**
52
     * @param string $endpoint
53
     *
54
     * @return bool
55
     */
56
    public function except(string $endpoint): bool
57
    {
58
        $excepts = [
59
            self::ENDPOINT,
60
        ];
61
62
        return in_array($endpoint, $excepts);
63
    }
64
65
    /**
66
     * @return string
67
     */
68
    protected function getCacheKey()
69
    {
70
        return 'easywechat.payment.sandbox.'.$this->app['config']->app_id;
71
    }
72
}
73