Passed
Push — master ( c685e5...ffe417 )
by Carlos
07:17 queued 04:23
created

Client::buildAgentConfig()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 5
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 2
rs 10
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\Work\Jssdk;
13
14
use EasyWeChat\BasicService\Jssdk\Client as BaseClient;
15
use EasyWeChat\Kernel\Exceptions\RuntimeException;
16
17
/**
18
 * Class Client.
19
 *
20
 * @author mingyoung <[email protected]>
21
 */
22
class Client extends BaseClient
23
{
24
    protected $ticketEndpoint = 'https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket';
25
26
    /**
27
     * @return string
28
     */
29 3
    protected function getAppId()
30
    {
31 3
        return $this->app['config']->get('corp_id');
32
    }
33
34
    /**
35
     * Return jsapi agent config as a PHP array.
36
     *
37
     * @param array $apis
38
     * @param bool  $debug
39
     * @param bool  $beta
40
     * @param array $openTagList
41
     *
42
     * @return array|string
43
     *
44
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
45
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
46
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
47
     * @throws \GuzzleHttp\Exception\GuzzleException
48
     * @throws \Psr\SimpleCache\InvalidArgumentException
49
     */
50 1
    public function getAgentConfigArray(array $apis, bool $debug = false, bool $beta = false, array $openTagList = [])
51
    {
52 1
        return $this->buildAgentConfig($apis, $debug, $beta, false, $openTagList);
53
    }
54
55
    /**
56
     * Get agent config json for jsapi.
57
     *
58
     * @param array $jsApiList
59
     * @param bool  $debug
60
     * @param bool  $beta
61
     * @param bool  $json
62
     * @param array $openTagList
63
     *
64
     * @return array|string
65
     *
66
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
67
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
68
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
69
     * @throws \GuzzleHttp\Exception\GuzzleException
70
     * @throws \Psr\SimpleCache\InvalidArgumentException
71
     */
72 1
    public function buildAgentConfig(array $jsApiList, bool $debug = false, bool $beta = false, bool $json = true, array $openTagList = [])
73
    {
74 1
        $config = array_merge(compact('debug', 'beta', 'jsApiList', 'openTagList'), $this->agentConfigSignature());
75
76 1
        return $json ? json_encode($config) : $config;
77
    }
78
79
    /**
80
     * @param string|null $url
81
     * @param string|null $nonce
82
     * @param null        $timestamp
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $timestamp is correct as it would always require null to be passed?
Loading history...
83
     *
84
     * @return array
85
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
86
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
87
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
88
     * @throws \GuzzleHttp\Exception\GuzzleException
89
     * @throws \Psr\SimpleCache\InvalidArgumentException
90
     */
91
    protected function agentConfigSignature(string $url = null, string $nonce = null, $timestamp = null): array
92
    {
93
        $url = $url ?: $this->getUrl();
94
        $nonce = $nonce ?: Support\Str::quickRandom(10);
0 ignored issues
show
Bug introduced by
The type EasyWeChat\Work\Jssdk\Support\Str was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
95
        $timestamp = $timestamp ?: time();
0 ignored issues
show
introduced by
$timestamp is of type null, thus it always evaluated to false.
Loading history...
96
97
        return [
98
            'corpid' => $this->getAppId(),
99
            'agentid' => $this->getAgentId(),
100
            'nonceStr' => $nonce,
101
            'timestamp' => $timestamp,
102
            'url' => $url,
103
            'signature' => $this->getTicketSignature($this->getAgentTicket()['ticket'], $nonce, $timestamp, $url),
104
        ];
105
    }
106
107
    /**
108
     * Get js ticket.
109
     *
110
     * @param bool   $refresh
111
     * @param string $type
112
     *
113
     * @return array
114
     *
115
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
116
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
117
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
118
     * @throws \GuzzleHttp\Exception\GuzzleException
119
     * @throws \Psr\SimpleCache\InvalidArgumentException
120
     */
121 1
    public function getTicket(bool $refresh = false, string $type = 'config'): array
122
    {
123 1
        $cacheKey = sprintf('easywechat.work.jssdk.ticket.%s.%s', $type, $this->getAppId());
124
125 1
        if (!$refresh && $this->getCache()->has($cacheKey)) {
126 1
            return $this->getCache()->get($cacheKey);
127
        }
128
129
        /** @var array<string, mixed> $result */
130 1
        $result = $this->castResponseToType(
131 1
            $this->requestRaw($this->ticketEndpoint, 'GET'),
132 1
            'array'
133
        );
134
135 1
        $this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500);
136
137 1
        if (!$this->getCache()->has($cacheKey)) {
138
            throw new RuntimeException('Failed to cache jssdk ticket.');
139
        }
140
141 1
        return $result;
142
    }
143
144
    /**
145
     * @param bool   $refresh
146
     * @param string $type
147
     *
148
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
149
     *
150
     * @throws RuntimeException
151
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
152
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
153
     * @throws \GuzzleHttp\Exception\GuzzleException
154
     * @throws \Psr\SimpleCache\InvalidArgumentException
155
     */
156 1
    public function getAgentTicket(bool $refresh = false, string $type = 'agent_config')
157
    {
158 1
        $cacheKey = sprintf('easywechat.work.jssdk.ticket.%s.%s', $type, $this->getAppId());
159
160 1
        if (!$refresh && $this->getCache()->has($cacheKey)) {
161 1
            return $this->getCache()->get($cacheKey);
162
        }
163
164
        /** @var array<string, mixed> $result */
165 1
        $result = $this->castResponseToType(
166 1
            $this->requestRaw('cgi-bin/ticket/get', 'GET', ['query' => ['type' => $type]]),
167 1
            'array'
168
        );
169
170 1
        $this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500);
171
172 1
        if (!$this->getCache()->has($cacheKey)) {
173
            throw new RuntimeException('Failed to cache jssdk ticket.');
174
        }
175
176 1
        return $result;
177
    }
178
}
179