Client::setFollowConfig()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 3
eloc 10
c 3
b 2
f 0
nc 2
nop 4
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 12
rs 9.9332
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\MicroMerchant\MerchantConfig;
13
14
use EasyWeChat\MicroMerchant\Kernel\BaseClient;
15
16
/**
17
 * Class Client.
18
 *
19
 * @author   liuml  <[email protected]>
20
 * @DateTime 2019-05-30  14:19
21
 */
22
class Client extends BaseClient
23
{
24
    /**
25
     * Service providers configure recommendation functions for small and micro businesses.
26
     *
27
     * @param string $subAppId
28
     * @param string $subscribeAppId
29
     * @param string $receiptAppId
30
     * @param string $subMchId
31
     *
32
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
33
     *
34
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
35
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
36
     * @throws \GuzzleHttp\Exception\GuzzleException
37
     */
38
    public function setFollowConfig(string $subAppId, string $subscribeAppId, string $receiptAppId = '', string $subMchId = '')
39
    {
40
        $params = [
41
            'sub_appid' => $subAppId,
42
            'sub_mch_id' => $subMchId ?: $this->app['config']->sub_mch_id,
43
        ];
44
45
        if (!empty($subscribeAppId)) {
46
            $params['subscribe_appid'] = $subscribeAppId;
47
        } else {
48
            $params['receipt_appid'] = $receiptAppId;
49
        }
50
51
        return $this->safeRequest('secapi/mkt/addrecommendconf', array_merge($params, [
52
            'sign_type' => 'HMAC-SHA256',
53
            'nonce_str' => uniqid('micro'),
54
        ]));
55
    }
56
57
    /**
58
     * Configure the new payment directory.
59
     *
60
     * @param string $jsapiPath
61
     * @param string $appId
62
     * @param string $subMchId
63
     *
64
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
65
     *
66
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
67
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
68
     */
69 1
    public function addPath(string $jsapiPath, string $appId = '', string $subMchId = '')
70
    {
71 1
        return $this->addConfig([
72 1
            'appid' => $appId ?: $this->app['config']->appid,
73 1
            'sub_mch_id' => $subMchId ?: $this->app['config']->sub_mch_id,
74 1
            'jsapi_path' => $jsapiPath,
75
        ]);
76
    }
77
78
    /**
79
     * bind appid.
80
     *
81
     * @param string $subAppId
82
     * @param string $appId
83
     * @param string $subMchId
84
     *
85
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
86
     *
87
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
88
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
89
     */
90 1
    public function bindAppId(string $subAppId, string $appId = '', string $subMchId = '')
91
    {
92 1
        return $this->addConfig([
93 1
            'appid' => $appId ?: $this->app['config']->appid,
94 1
            'sub_mch_id' => $subMchId ?: $this->app['config']->sub_mch_id,
95 1
            'sub_appid' => $subAppId,
96
        ]);
97
    }
98
99
    /**
100
     * add sub dev config.
101
     *
102
     * @param array $params
103
     *
104
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
105
     *
106
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
107
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
108
     * @throws \GuzzleHttp\Exception\GuzzleException
109
     */
110 2
    private function addConfig(array $params)
111
    {
112 2
        return $this->safeRequest('secapi/mch/addsubdevconfig', $params);
113
    }
114
115
    /**
116
     * query Sub Dev Config.
117
     *
118
     * @param string $subMchId
119
     * @param string $appId
120
     *
121
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
122
     *
123
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
124
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
125
     * @throws \GuzzleHttp\Exception\GuzzleException
126
     */
127 1
    public function getConfig(string $subMchId = '', string $appId = '')
128
    {
129 1
        return $this->safeRequest('secapi/mch/querysubdevconfig', [
130 1
            'sub_mch_id' => $subMchId ?: $this->app['config']->sub_mch_id,
131 1
            'appid' => $appId ?: $this->app['config']->appid,
132
        ]);
133
    }
134
}
135