JssdkClient   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 60
ccs 16
cts 16
cp 1
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getTicket() 0 3 1
A attachExtension() 0 16 1
A assign() 0 5 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\OfficialAccount\Card;
13
14
use EasyWeChat\BasicService\Jssdk\Client as Jssdk;
15
use EasyWeChat\Kernel\Support\Arr;
16
use function EasyWeChat\Kernel\Support\str_random;
17
18
/**
19
 * Class Jssdk.
20
 *
21
 * @author overtrue <[email protected]>
22
 */
23
class JssdkClient extends Jssdk
24
{
25
    /**
26
     * @param bool   $refresh
27
     * @param string $type
28
     *
29
     * @return array
30
     *
31
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
32
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
33
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
34
     * @throws \Psr\SimpleCache\InvalidArgumentException
35
     */
36 1
    public function getTicket(bool $refresh = false, string $type = 'wx_card'): array
37
    {
38 1
        return parent::getTicket($refresh, $type);
39
    }
40
41
    /**
42
     * 微信卡券:JSAPI 卡券发放.
43
     *
44
     * @param array $cards
45
     *
46
     * @return string
47
     */
48 1
    public function assign(array $cards)
49
    {
50 1
        return json_encode(array_map(function ($card) {
51 1
            return $this->attachExtension($card['card_id'], $card);
52 1
        }, $cards));
53
    }
54
55
    /**
56
     * 生成 js添加到卡包 需要的 card_list 项.
57
     *
58
     * @param string $cardId
59
     * @param array  $extension
60
     *
61
     * @return array
62
     *
63
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
64
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
65
     * @throws \Psr\SimpleCache\InvalidArgumentException
66
     */
67 1
    public function attachExtension($cardId, array $extension = [])
68
    {
69 1
        $timestamp = time();
70 1
        $nonce = str_random(6);
71 1
        $ticket = $this->getTicket()['ticket'];
72
73 1
        $ext = array_merge(['timestamp' => $timestamp, 'nonce_str' => $nonce], Arr::only(
74 1
            $extension,
75 1
            ['code', 'openid', 'outer_id', 'balance', 'fixed_begintimestamp', 'outer_str']
76
        ));
77
78 1
        $ext['signature'] = $this->dictionaryOrderSignature($ticket, $timestamp, $cardId, $ext['code'] ?? '', $ext['openid'] ?? '', $nonce);
79
80
        return [
81 1
            'cardId' => $cardId,
82 1
            'cardExt' => json_encode($ext),
83
        ];
84
    }
85
}
86