Passed
Push — master ( 511f0f...0c65ca )
by Carlos
13:06
created

Client::getAppId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
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
     * @param bool   $refresh
36
     * @param string $type
37
     *
38
     * @return array|\EasyWeChat\Kernel\Support\Collection|mixed|object|\Psr\Http\Message\ResponseInterface|string
39
     *
40
     * @throws RuntimeException
41
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
42
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
43
     * @throws \GuzzleHttp\Exception\GuzzleException
44
     * @throws \Psr\SimpleCache\InvalidArgumentException
45
     */
46 1
    public function getAgentTicket(bool $refresh = false, string $type = 'agent_config')
47
    {
48 1
        $cacheKey = sprintf('easywechat.work.jssdk.ticket.%s.%s', $type, $this->getAppId());
49
50 1
        if (!$refresh && $this->getCache()->has($cacheKey)) {
51 1
            return $this->getCache()->get($cacheKey);
52
        }
53
54
        /** @var array<string, mixed> $result */
55 1
        $result = $this->castResponseToType(
56 1
            $this->requestRaw('cgi-bin/ticket/get', 'GET', ['query' => ['type' => $type]]),
57 1
            'array'
58
        );
59
60 1
        $this->getCache()->set($cacheKey, $result, $result['expires_in'] - 500);
61
62 1
        if (!$this->getCache()->has($cacheKey)) {
63
            throw new RuntimeException('Failed to cache jssdk ticket.');
64
        }
65
66 1
        return $result;
67
    }
68
}
69