Completed
Pull Request — master (#1413)
by milkmeowo
04:18
created

Client::formatKVLists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 7
cts 7
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\MiniProgram\OpenData;
13
14
use EasyWeChat\Kernel\BaseClient;
15
16
/**
17
 * Class Client.
18
 *
19
 * @author tianyong90 <[email protected]>
20
 */
21
class Client extends BaseClient
22
{
23
    /**
24
     * @var string
25
     */
26
    protected $baseUri = 'https://api.weixin.qq.com/wxa/';
27
28
    /**
29
     * removeUserStorage.
30
     *
31
     * @param string $openid
32
     * @param string $sessionKey
33
     * @param array  $key
34
     *
35
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
36
     *
37
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
38
     */
39 1
    public function removeUserStorage(string $openid, string $sessionKey, array $key)
40
    {
41 1
        $data = ['key' => $key];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
42
        $query = [
43 1
            'openid' => $openid,
44 1
            'sig_method' => 'hmac_sha256',
45 1
            'signature' => hash_hmac('sha256', json_encode($data), $sessionKey),
46
        ];
47
48 1
        return $this->httpPostJson('remove_user_storage', $data, $query);
49
    }
50
51
    /**
52
     * setUserStorage.
53
     *
54
     * @param string $openid
55
     * @param string $sessionKey
56
     * @param array  $kvList
57
     *
58
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
59
     *
60
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
61
     */
62 1
    public function setUserStorage(string $openid, string $sessionKey, array $kvList)
63
    {
64 1
        $kvList = $this->formatKVLists($kvList);
65
66 1
        $data = ['kv_list' => $kvList];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 2 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...
67
        $query = [
68 1
            'openid' => $openid,
69 1
            'sig_method' => 'hmac_sha256',
70 1
            'signature' => hash_hmac('sha256', json_encode($data), $sessionKey),
71
        ];
72
73 1
        return $this->httpPostJson('set_user_storage', $data, $query);
74
    }
75
76
    /**
77
     * @param array $params
78
     * @return array
79
     */
80 1
    protected function formatKVLists(array $params)
81
    {
82 1
        $formatted = [];
83
84 1
        foreach ($params as $name => $value) {
85 1
            $formatted[] = [
86 1
                'key' => $name,
87 1
                'value' => strval($value),
88
            ];
89
        }
90
91 1
        return $formatted;
92
    }
93
}
94