Completed
Push — master ( 32869d...8a9333 )
by Carlos
04:33 queued 30s
created

JssdkClient   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 57
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
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\InvalidConfigException
32
     * @throws \Psr\SimpleCache\InvalidArgumentException
33
     */
34 1
    public function getTicket(bool $refresh = false, string $type = 'wx_card'): array
35
    {
36 1
        return parent::getTicket($refresh, $type);
37
    }
38
39
    /**
40
     * 微信卡券:JSAPI 卡券发放.
41
     *
42
     * @param array $cards
43
     *
44
     * @return string
45
     */
46
    public function assign(array $cards)
47
    {
48 1
        return json_encode(array_map(function ($card) {
49 1
            return $this->attachExtension($card['card_id'], $card);
50 1
        }, $cards));
51
    }
52
53
    /**
54
     * 生成 js添加到卡包 需要的 card_list 项.
55
     *
56
     * @param string $cardId
57
     * @param array  $extension
58
     *
59
     * @return array
60
     *
61
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
62
     * @throws \Psr\SimpleCache\InvalidArgumentException
63
     */
64 1
    public function attachExtension($cardId, array $extension = [])
65
    {
66 1
        $timestamp = time();
67 1
        $nonce = str_random(6);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 5 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68 1
        $ticket = $this->getTicket()['ticket'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 4 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
69
70 1
        $ext = array_merge(['timestamp' => $timestamp, 'nonce_str' => $nonce], Arr::only(
71 1
            $extension,
72 1
            ['code', 'openid', 'outer_id', 'balance', 'fixed_begintimestamp', 'outer_str']
73
        ));
74
75 1
        $ext['signature'] = $this->dictionaryOrderSignature($ticket, $timestamp, $cardId, $ext['code'] ?? '', $ext['openid'] ?? '', $nonce);
76
77
        return [
78 1
            'cardId' => $cardId,
79 1
            'cardExt' => json_encode($ext),
80
        ];
81
    }
82
}
83