Completed
Push — master ( 6c9646...706307 )
by Carlos
02:44
created

Card   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 809
Duplicated Lines 20.89 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 91.56%

Importance

Changes 0
Metric Value
dl 169
loc 809
ccs 206
cts 225
cp 0.9156
rs 5.7142
c 0
b 0
f 0
wmc 53
lcom 1
cbo 7

43 Methods

Rating   Name   Duplication   Size   Complexity  
A getColors() 0 4 1
A create() 0 11 1
A QRCode() 0 4 1
A showQRCode() 0 19 1
A getQRCodeUrl() 0 4 1
A getAPITicket() 16 16 3
A jsConfigForAssign() 0 6 1
B attachExtension() 0 24 1
A getSignature() 0 7 1
A createLandingPage() 0 12 1
A deposit() 9 9 1
A getDepositedCount() 8 8 1
A checkCode() 9 9 1
A getCode() 10 10 1
B consume() 0 16 5
A decryptCode() 0 8 1
A getHtml() 8 8 1
A setTestWhitelist() 0 8 1
A setTestWhitelistByUsername() 0 8 1
A getUserCards() 9 9 1
A getCard() 8 8 1
A lists() 10 10 1
A update() 0 15 2
A setPayCell() 9 9 1
A increaseStock() 0 4 1
A reduceStock() 0 4 1
A updateStock() 0 10 2
A updateCode() 10 10 1
A delete() 8 8 1
A disable() 9 9 1
A activateUserForm() 0 6 1
A getMemberCardUser() 9 9 1
A updateMemberCardUser() 0 4 1
A updateGeneralCardUser() 0 4 1
A createSubMerchant() 18 18 1
A updateSubMerchant() 19 19 1
A getSubMerchant() 0 4 1
A listSubMerchants() 0 10 1
A getCategories() 0 4 1
A setCache() 0 6 1
A getCache() 0 4 2
A setUrl() 0 6 1
A activate() 0 8 2

How to fix   Duplicated Code    Complexity   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like Card often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Card, and based on these observations, apply Extract Interface, too.

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
/**
13
 * Card.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2016 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Card;
23
24
use Doctrine\Common\Cache\Cache;
25
use Doctrine\Common\Cache\FilesystemCache;
26
use EasyWeChat\Core\AbstractAPI;
27
use EasyWeChat\Support\Arr;
28
use Psr\Http\Message\ResponseInterface;
29
30
class Card extends AbstractAPI
31
{
32
    /**
33
     * Cache.
34
     *
35
     * @var Cache
36
     */
37
    protected $cache;
38
39
    /**
40
     * Ticket cache prefix.
41
     */
42
    const TICKET_CACHE_PREFIX = 'overtrue.wechat.card_api_ticket.';
43
44
    const API_GET_COLORS = 'https://api.weixin.qq.com/card/getcolors';
45
    const API_CREATE_CARD = 'https://api.weixin.qq.com/card/create';
46
    const API_CREATE_QRCODE = 'https://api.weixin.qq.com/card/qrcode/create';
47
    const API_SHOW_QRCODE = 'https://mp.weixin.qq.com/cgi-bin/showqrcode';
48
    const API_GET_CARD_TICKET = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket';
49
    const API_CREATE_LANDING_PAGE = 'https://api.weixin.qq.com/card/landingpage/create';
50
    const API_DEPOSIT_CODE = 'https://api.weixin.qq.com/card/code/deposit';
51
    const API_GET_DEPOSIT_COUNT = 'https://api.weixin.qq.com/card/code/getdepositcount';
52
    const API_CHECK_CODE = 'https://api.weixin.qq.com/card/code/checkcode';
53
    const API_GET_HTML = 'https://api.weixin.qq.com/card/mpnews/gethtml';
54
    const API_SET_TEST_WHITE_LIST = 'https://api.weixin.qq.com/card/testwhitelist/set';
55
    const API_GET_CODE = 'https://api.weixin.qq.com/card/code/get';
56
    const API_CONSUME_CARD = 'https://api.weixin.qq.com/card/code/consume';
57
    const API_DECRYPT_CODE = 'https://api.weixin.qq.com/card/code/decrypt';
58
    const API_GET_CARD_LIST = 'https://api.weixin.qq.com/card/user/getcardlist';
59
    const API_GET_CARD = 'https://api.weixin.qq.com/card/get';
60
    const API_LIST_CARD = 'https://api.weixin.qq.com/card/batchget';
61
    const API_UPDATE_CARD = 'https://api.weixin.qq.com/card/update';
62
    const API_SET_PAY_CELL = 'https://api.weixin.qq.com/card/paycell/set';
63
    const API_MODIFY_STOCK = 'https://api.weixin.qq.com/card/modifystock';
64
    const API_UPDATE_CODE = 'https://api.weixin.qq.com/card/code/update';
65
    const API_DELETE_CARD = 'https://api.weixin.qq.com/card/delete';
66
    const API_DISABLE_CARD = 'https://api.weixin.qq.com/card/code/unavailable';
67
    const API_ACTIVATE_MEMBER_CARD = 'https://api.weixin.qq.com/card/membercard/activate';
68
    const API_ACTIVATE_MEMBER_USER_FORM = 'https://api.weixin.qq.com/card/membercard/activateuserform/set';
69
    const API_GET_MEMBER_USER_INFO = 'https://api.weixin.qq.com/card/membercard/userinfo/get';
70
    const API_UPDATE_MEMBER_CARD_USER = 'https://api.weixin.qq.com/card/membercard/updateuser';
71
    const API_CREATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/submit';
72
    const API_UPDATE_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/update';
73
    const API_GET_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/get';
74
    const API_LIST_SUB_MERCHANT = 'https://api.weixin.qq.com/card/submerchant/batchget';
75
    const API_GET_CATEGORIES = 'https://api.weixin.qq.com/card/getapplyprotocol';
76
    const API_ACTIVATE_GENERAL_CARD = 'https://api.weixin.qq.com/card/generalcard/activate';
77
    const API_UPDATE_GENERAL_CARD_USER = 'https://api.weixin.qq.com/card/generalcard/updateuser';
78
79
    /**
80
     * 获取卡券颜色.
81
     *
82 1
     * @return \EasyWeChat\Support\Collection
83
     */
84 1
    public function getColors()
85
    {
86
        return $this->parseJSON('get', [self::API_GET_COLORS]);
87
    }
88
89
    /**
90
     * 创建卡券.
91
     *
92
     * @param string $cardType
93
     * @param array  $baseInfo
94
     * @param array  $especial
95
     * @param array  $advancedInfo
96
     *
97 1
     * @return \EasyWeChat\Support\Collection
98
     */
99
    public function create($cardType = 'member_card', array $baseInfo = [], array $especial = [], array $advancedInfo = [])
100
    {
101 1
        $params = [
102 1
            'card' => [
103 1
                'card_type' => strtoupper($cardType),
104 1
                strtolower($cardType) => array_merge(['base_info' => $baseInfo], $especial, $advancedInfo),
105
            ],
106 1
        ];
107
108
        return $this->parseJSON('json', [self::API_CREATE_CARD, $params]);
109
    }
110
111
    /**
112
     * 创建二维码.
113
     *
114
     * @param array $cards
115
     *
116 1
     * @return \EasyWeChat\Support\Collection
117
     */
118 1
    public function QRCode(array $cards = [])
119
    {
120
        return $this->parseJSON('json', [self::API_CREATE_QRCODE, $cards]);
121
    }
122
123
    /**
124
     * ticket 换取二维码图片.
125
     *
126
     * @param string $ticket
127
     *
128
     * @return array
129
     */
130
    public function showQRCode($ticket = null)
131
    {
132
        $params = [
133
            'ticket' => $ticket,
134
        ];
135
136
        $http = $this->getHttp();
137
138
        /** @var ResponseInterface $response */
139
        $response = $http->get(self::API_SHOW_QRCODE, $params);
140
141
        return [
142
            'status' => $response->getStatusCode(),
143
            'reason' => $response->getReasonPhrase(),
144
            'headers' => $response->getHeaders(),
145
            'body' => strval($response->getBody()),
146
            'url' => self::API_SHOW_QRCODE.'?'.http_build_query($params),
147
        ];
148
    }
149
150
    /**
151
     * 通过ticket换取二维码 链接.
152
     *
153
     * @param string $ticket
154
     *
155 1
     * @return string
156
     */
157 1
    public function getQRCodeUrl($ticket)
158
    {
159
        return self::API_SHOW_QRCODE.'?ticket='.$ticket;
160
    }
161
162
    /**
163
     * 获取 卡券 Api_ticket.
164
     *
165
     * @param bool $refresh 是否强制刷新
166
     *
167 2
     * @return string $apiTicket
168
     */
169 2 View Code Duplication
    public function getAPITicket($refresh = false)
170
    {
171 2
        $key = self::TICKET_CACHE_PREFIX.$this->getAccessToken()->getAppId();
172
173 2
        $ticket = $this->getCache()->fetch($key);
174 1
175
        if (!$ticket || $refresh) {
176 1
            $result = $this->parseJSON('get', [self::API_GET_CARD_TICKET, ['type' => 'wx_card']]);
177
178 1
            $this->getCache()->save($key, $result['ticket'], $result['expires_in'] - 500);
179
180
            return $result['ticket'];
181 1
        }
182
183
        return $ticket;
184
    }
185
186
    /**
187
     * 微信卡券:JSAPI 卡券发放.
188
     *
189
     * @param array $cards
190
     *
191
     * @return string
192
     */
193 1
    public function jsConfigForAssign(array $cards)
194 1
    {
195 1
        return json_encode(array_map(function ($card) {
196
            return $this->attachExtension($card['card_id'], $card);
197
        }, $cards));
198
    }
199
200
    /**
201
     * 生成 js添加到卡包 需要的 card_list 项.
202
     *
203
     * @param string $cardId
204
     * @param array  $extension
205
     *
206 1
     * @return string
207
     */
208 1
    public function attachExtension($cardId, array $extension = [])
209
    {
210 1
        $timestamp = time();
211 1
        $ext = [
212 1
            'code' => Arr::get($extension, 'code'),
213 1
            'openid' => Arr::get($extension, 'openid', Arr::get($extension, 'open_id')),
214 1
            'timestamp' => $timestamp,
215 1
            'outer_id' => Arr::get($extension, 'outer_id'),
216 1
            'balance' => Arr::get($extension, 'balance'),
217 1
        ];
218 1
        $ext['signature'] = $this->getSignature(
219 1
            $this->getAPITicket(),
220 1
            $timestamp,
221 1
            $cardId,
222 1
            $ext['code'],
223 1
            $ext['openid'],
224
            $ext['balance']
225
        );
226 1
227 1
        return [
228 1
            'cardId' => $cardId,
229
            'cardExt' => json_encode($ext),
230
        ];
231
    }
232
233
    /**
234
     * 生成签名.
235
     *
236 1
     * @return string
237
     */
238 1
    public function getSignature()
239 1
    {
240
        $params = func_get_args();
241 1
        sort($params, SORT_STRING);
242
243
        return sha1(implode($params));
244
    }
245
246
    /**
247
     * 创建货架接口.
248
     *
249
     * @param string $banner
250
     * @param string $pageTitle
251
     * @param bool   $canShare
252
     * @param string $scene     [SCENE_NEAR_BY 附近,SCENE_MENU 自定义菜单,SCENE_QRCODE 二维码,SCENE_ARTICLE 公众号文章,
253
     *                          SCENE_H5 h5页面,SCENE_IVR 自动回复,SCENE_CARD_CUSTOM_CELL 卡券自定义cell]
254
     * @param array  $cardList
255
     *
256 1
     * @return \EasyWeChat\Support\Collection
257
     */
258
    public function createLandingPage($banner, $pageTitle, $canShare, $scene, $cardList)
259 1
    {
260 1
        $params = [
261 1
            'banner' => $banner,
262 1
            'page_title' => $pageTitle,
263 1
            'can_share' => $canShare,
264 1
            'scene' => $scene,
265
            'card_list' => $cardList,
266 1
        ];
267
268
        return $this->parseJSON('json', [self::API_CREATE_LANDING_PAGE, $params]);
269
    }
270
271
    /**
272
     * 导入code接口.
273
     *
274
     * @param string $cardId
275
     * @param array  $code
276
     *
277 1
     * @return \EasyWeChat\Support\Collection
278
     */
279 View Code Duplication
    public function deposit($cardId, $code)
280 1
    {
281 1
        $params = [
282 1
            'card_id' => $cardId,
283
            'code' => $code,
284 1
        ];
285
286
        return $this->parseJSON('json', [self::API_DEPOSIT_CODE, $params]);
287
    }
288
289
    /**
290
     * 查询导入code数目.
291
     *
292
     * @param string $cardId
293
     *
294 1
     * @return \EasyWeChat\Support\Collection
295
     */
296 View Code Duplication
    public function getDepositedCount($cardId)
297 1
    {
298 1
        $params = [
299
            'card_id' => $cardId,
300 1
        ];
301
302
        return $this->parseJSON('json', [self::API_GET_DEPOSIT_COUNT, $params]);
303
    }
304
305
    /**
306
     * 核查code接口.
307
     *
308
     * @param string $cardId
309
     * @param array  $code
310
     *
311 1
     * @return \EasyWeChat\Support\Collection
312
     */
313 View Code Duplication
    public function checkCode($cardId, $code)
314 1
    {
315 1
        $params = [
316 1
            'card_id' => $cardId,
317
            'code' => $code,
318 1
        ];
319
320
        return $this->parseJSON('json', [self::API_CHECK_CODE, $params]);
321
    }
322
323
    /**
324
     * 查询Code接口.
325
     *
326
     * @param string $code
327
     * @param bool   $checkConsume
328
     * @param string $cardId
329
     *
330 1
     * @return \EasyWeChat\Support\Collection
331
     */
332 View Code Duplication
    public function getCode($code, $checkConsume, $cardId)
333 1
    {
334 1
        $params = [
335 1
            'code' => $code,
336 1
            'check_consume' => $checkConsume,
337
            'card_id' => $cardId,
338 1
        ];
339
340
        return $this->parseJSON('json', [self::API_GET_CODE, $params]);
341
    }
342
343
    /**
344
     * 核销Code接口.
345
     *
346
     * @param string $code
347
     * @param string $cardId
348
     *
349 1
     * @return \EasyWeChat\Support\Collection
350
     */
351 1
    public function consume($code, $cardId = null)
352 1
    {
353 1
        if (strlen($code) === 28 && $cardId && strlen($cardId) !== 28) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cardId of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
354
            list($code, $cardId) = [$cardId, $code];
355
        }
356 1
357 1
        $params = [
358
            'code' => $code,
359 1
        ];
360 1
361 1
        if ($cardId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $cardId of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
362
            $params['card_id'] = $cardId;
363 1
        }
364
365
        return $this->parseJSON('json', [self::API_CONSUME_CARD, $params]);
366
    }
367
368
    /**
369
     * Code解码接口.
370
     *
371
     * @param string $encryptedCode
372
     *
373 1
     * @return \EasyWeChat\Support\Collection
374
     */
375
    public function decryptCode($encryptedCode)
376 1
    {
377 1
        $params = [
378
            'encrypt_code' => $encryptedCode,
379 1
        ];
380
381
        return $this->parseJSON('json', [self::API_DECRYPT_CODE, $params]);
382
    }
383
384
    /**
385
     * 图文消息群发卡券.
386
     *
387
     * @param string $cardId
388
     *
389 1
     * @return \EasyWeChat\Support\Collection
390
     */
391 View Code Duplication
    public function getHtml($cardId)
392 1
    {
393 1
        $params = [
394
            'card_id' => $cardId,
395 1
        ];
396
397
        return $this->parseJSON('json', [self::API_GET_HTML, $params]);
398
    }
399
400
    /**
401
     * 设置测试白名单.
402
     *
403
     * @param array $openids
404
     *
405 1
     * @return \EasyWeChat\Support\Collection
406
     */
407
    public function setTestWhitelist($openids)
408 1
    {
409 1
        $params = [
410
            'openid' => $openids,
411 1
        ];
412
413
        return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]);
414
    }
415
416
    /**
417
     * 设置测试白名单(by username).
418
     *
419
     * @param array $usernames
420
     *
421 1
     * @return \EasyWeChat\Support\Collection
422
     */
423
    public function setTestWhitelistByUsername($usernames)
424 1
    {
425 1
        $params = [
426
            'username' => $usernames,
427 1
        ];
428
429
        return $this->parseJSON('json', [self::API_SET_TEST_WHITE_LIST, $params]);
430
    }
431
432
    /**
433
     * 获取用户已领取卡券接口.
434
     *
435
     * @param string $openid
436
     * @param string $cardId
437
     *
438 1
     * @return \EasyWeChat\Support\Collection
439
     */
440 View Code Duplication
    public function getUserCards($openid, $cardId = '')
441 1
    {
442 1
        $params = [
443 1
            'openid' => $openid,
444
            'card_id' => $cardId,
445 1
        ];
446
447
        return $this->parseJSON('json', [self::API_GET_CARD_LIST, $params]);
448
    }
449
450
    /**
451
     * 查看卡券详情.
452
     *
453
     * @param string $cardId
454
     *
455 1
     * @return \EasyWeChat\Support\Collection
456
     */
457 View Code Duplication
    public function getCard($cardId)
458 1
    {
459 1
        $params = [
460
            'card_id' => $cardId,
461 1
        ];
462
463
        return $this->parseJSON('json', [self::API_GET_CARD, $params]);
464
    }
465
466
    /**
467
     * 批量查询卡列表.
468
     *
469
     * @param int    $offset
470
     * @param int    $count
471
     * @param string $statusList
472
     *
473 1
     * @return \EasyWeChat\Support\Collection
474
     */
475 View Code Duplication
    public function lists($offset = 0, $count = 10, $statusList = 'CARD_STATUS_VERIFY_OK')
476 1
    {
477 1
        $params = [
478 1
            'offset' => $offset,
479 1
            'count' => $count,
480
            'status_list' => $statusList,
481 1
        ];
482
483
        return $this->parseJSON('json', [self::API_LIST_CARD, $params]);
484
    }
485
486
    /**
487
     * 更改卡券信息接口 and 设置跟随推荐接口.
488
     *
489
     * @param string $cardId
490
     * @param string $type
491
     * @param array  $baseInfo
492
     * @param array  $especial
493
     *
494 1
     * @return \EasyWeChat\Support\Collection
495
     */
496 1
    public function update($cardId, $type, $baseInfo = [], $especial = [])
497 1
    {
498 1
        $card = [];
499
        $card['card_id'] = $cardId;
500 1
        $card[$type] = [];
501 1
502 1
        $cardInfo = [];
503 1
        if ($baseInfo) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $baseInfo of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
504
            $cardInfo['base_info'] = $baseInfo;
505 1
        }
506
507 1
        $card[$type] = array_merge($cardInfo, $especial);
508
509
        return $this->parseJSON('json', [self::API_UPDATE_CARD, $card]);
510
    }
511
512
    /**
513
     * 设置微信买单接口.
514
     * 设置买单的 card_id 必须已经配置了门店,否则会报错.
515
     *
516
     * @param string $cardId
517
     * @param bool   $isOpen
518
     *
519 1
     * @return \EasyWeChat\Support\Collection
520
     */
521 View Code Duplication
    public function setPayCell($cardId, $isOpen = true)
522 1
    {
523 1
        $params = [
524 1
            'card_id' => $cardId,
525
            'is_open' => $isOpen,
526 1
        ];
527
528
        return $this->parseJSON('json', [self::API_SET_PAY_CELL, $params]);
529
    }
530
531
    /**
532
     * 增加库存.
533
     *
534
     * @param string $cardId
535
     * @param int    $amount
536
     *
537 1
     * @return \EasyWeChat\Support\Collection
538
     */
539 1
    public function increaseStock($cardId, $amount)
540
    {
541
        return $this->updateStock($cardId, $amount, 'increase');
542
    }
543
544
    /**
545
     * 减少库存.
546
     *
547
     * @param string $cardId
548
     * @param int    $amount
549
     *
550 1
     * @return \EasyWeChat\Support\Collection
551
     */
552 1
    public function reduceStock($cardId, $amount)
553
    {
554
        return $this->updateStock($cardId, $amount, 'reduce');
555
    }
556
557
    /**
558
     * 修改库存接口.
559
     *
560
     * @param string $cardId
561
     * @param int    $amount
562
     * @param string $action
563
     *
564 1
     * @return \EasyWeChat\Support\Collection
565
     */
566 1
    protected function updateStock($cardId, $amount, $action = 'increase')
567
    {
568 1
        $key = $action === 'increase' ? 'increase_stock_value' : 'reduce_stock_value';
569 1
        $params = [
570 1
            'card_id' => $cardId,
571
            $key => abs($amount),
572 1
        ];
573
574
        return $this->parseJSON('json', [self::API_MODIFY_STOCK, $params]);
575
    }
576
577
    /**
578
     * 更改Code接口.
579
     *
580
     * @param string $code
581
     * @param string $newCode
582
     * @param array  $cardId
583
     *
584 1
     * @return \EasyWeChat\Support\Collection
585
     */
586 View Code Duplication
    public function updateCode($code, $newCode, $cardId = [])
587 1
    {
588 1
        $params = [
589 1
            'code' => $code,
590 1
            'new_code' => $newCode,
591
            'card_id' => $cardId,
592 1
        ];
593
594
        return $this->parseJSON('json', [self::API_UPDATE_CODE, $params]);
595
    }
596
597
    /**
598
     * 删除卡券接口.
599
     *
600
     * @param string $cardId
601
     *
602 1
     * @return \EasyWeChat\Support\Collection
603
     */
604 View Code Duplication
    public function delete($cardId)
605 1
    {
606 1
        $params = [
607
            'card_id' => $cardId,
608 1
        ];
609
610
        return $this->parseJSON('json', [self::API_DELETE_CARD, $params]);
611
    }
612
613
    /**
614
     * 设置卡券失效.
615
     *
616
     * @param string $code
617
     * @param string $cardId
618
     *
619 1
     * @return \EasyWeChat\Support\Collection
620
     */
621 View Code Duplication
    public function disable($code, $cardId = '')
622 1
    {
623 1
        $params = [
624 1
            'code' => $code,
625
            'card_id' => $cardId,
626 1
        ];
627
628
        return $this->parseJSON('json', [self::API_DISABLE_CARD, $params]);
629
    }
630
631
    /**
632
     * 会员卡接口激活.
633
     *
634
     * @param array $info
635
     *
636 1
     * @return \EasyWeChat\Support\Collection
637
     */
638 1
    public function activate($info = [], $cardType = 'member_card')
639
    {
640
        if ($cardType === 'general_card') {
641
            return $this->parseJSON('json', [self::API_ACTIVATE_GENERAL_CARD, $info]);
642
        }
643
644
        return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_CARD, $info]);
645
    }
646
647
    /**
648
     * 设置开卡字段接口.
649
     *
650 1
     * @param string $cardId
651
     * @param array  $requiredForm
652 1
     * @param array  $optionalForm
653
     *
654 1
     * @return \EasyWeChat\Support\Collection
655
     */
656
    public function activateUserForm($cardId, array $requiredForm = [], array $optionalForm = [])
657
    {
658
        $params = array_merge(['card_id' => $cardId], $requiredForm, $optionalForm);
659
660
        return $this->parseJSON('json', [self::API_ACTIVATE_MEMBER_USER_FORM, $params]);
661
    }
662
663
    /**
664
     * 拉取会员信息接口.
665 1
     *
666
     * @param string $cardId
667
     * @param string $code
668 1
     *
669 1
     * @return \EasyWeChat\Support\Collection
670 1
     */
671 View Code Duplication
    public function getMemberCardUser($cardId, $code)
672 1
    {
673
        $params = [
674
            'card_id' => $cardId,
675
            'code' => $code,
676
        ];
677
678
        return $this->parseJSON('json', [self::API_GET_MEMBER_USER_INFO, $params]);
679
    }
680
681
    /**
682 1
     * 更新会员信息.
683
     *
684 1
     * @param array $params
685
     *
686
     * @return \EasyWeChat\Support\Collection
687
     */
688
    public function updateMemberCardUser(array $params = [])
689
    {
690
        return $this->parseJSON('json', [self::API_UPDATE_MEMBER_CARD_USER, $params]);
691
    }
692
693
    /**
694 1
     * 更新通用员信息.
695
     *
696
     * @param array $params
697 1
     *
698 1
     * @return \EasyWeChat\Support\Collection
699 1
     */
700 1
    public function updateGeneralCardUser(array $params = [])
701 1
    {
702 1
        return $this->parseJSON('json', [self::API_UPDATE_GENERAL_CARD_USER, $params]);
703 1
    }
704 1
705 1
    /**
706 1
     * 添加子商户.
707 1
     *
708 1
     * @param array $info
709
     *
710 1
     * @return \EasyWeChat\Support\Collection
711
     */
712 View Code Duplication
    public function createSubMerchant(array $info = [])
713
    {
714
        $params = [
715
            'info' => Arr::only($info, [
716
                'brand_name',
717
                'logo_url',
718
                'protocol',
719
                'end_time',
720
                'primary_category_id',
721 1
                'secondary_category_id',
722
                'agreement_media_id',
723
                'operator_media_id',
724 1
                'app_id',
725 1
            ]),
726 1
        ];
727 1
728 1
        return $this->parseJSON('json', [self::API_CREATE_SUB_MERCHANT, $params]);
729 1
    }
730 1
731 1
    /**
732 1
     * 更新子商户.
733 1
     *
734 1
     * @param int   $merchantId
735 1
     * @param array $info
736 1
     *
737
     * @return \EasyWeChat\Support\Collection
738 1
     */
739 View Code Duplication
    public function updateSubMerchant($merchantId, array $info = [])
740
    {
741
        $params = [
742
            'info' => array_merge(['merchant_id' => $merchantId],
743
                Arr::only($info, [
744
                    'brand_name',
745
                    'logo_url',
746
                    'protocol',
747
                    'end_time',
748
                    'primary_category_id',
749
                    'secondary_category_id',
750
                    'agreement_media_id',
751
                    'operator_media_id',
752
                    'app_id',
753
                ])),
754
        ];
755
756
        return $this->parseJSON('json', [self::API_UPDATE_SUB_MERCHANT, $params]);
757
    }
758
759
    /**
760
     * 获取子商户信息.
761
     *
762
     * @param int $merchantId
763
     *
764
     * @return \EasyWeChat\Support\Collection
765
     */
766
    public function getSubMerchant($merchantId)
767
    {
768
        return $this->parseJSON('json', [self::API_GET_SUB_MERCHANT, ['merchant_id' => $merchantId]]);
769
    }
770
771
    /**
772
     * 批量获取子商户信息.
773
     *
774
     * @param int    $beginId
775
     * @param int    $limit
776
     * @param string $status
777
     *
778 1
     * @return \EasyWeChat\Support\Collection
779
     */
780 1
    public function listSubMerchants($beginId = 0, $limit = 50, $status = 'CHECKING')
781
    {
782
        $params = [
783
            'begin_id' => $beginId,
784
            'limit' => $limit,
785
            'status' => $status,
786
        ];
787
788
        return $this->parseJSON('json', [self::API_LIST_SUB_MERCHANT, $params]);
789
    }
790 2
791
    /**
792 2
     * 卡券开放类目查询接口.
793
     *
794 2
     * @return \EasyWeChat\Support\Collection
795
     */
796
    public function getCategories()
797
    {
798
        return $this->parseJSON('get', [self::API_GET_CATEGORIES]);
799
    }
800
801
    /**
802 2
     * Set cache manager.
803
     *
804 2
     * @param \Doctrine\Common\Cache\Cache $cache
805
     *
806
     * @return $this
807
     */
808
    public function setCache(Cache $cache)
809
    {
810
        $this->cache = $cache;
811
812
        return $this;
813
    }
814 1
815
    /**
816 1
     * Return cache manager.
817
     *
818 1
     * @return \Doctrine\Common\Cache\Cache
819
     */
820
    public function getCache()
821
    {
822
        return $this->cache ?: $this->cache = new FilesystemCache(sys_get_temp_dir());
823
    }
824
825
    /**
826
     * Set current url.
827
     *
828
     * @param string $url
829
     *
830
     * @return Card
831
     */
832
    public function setUrl($url)
833
    {
834
        $this->url = $url;
0 ignored issues
show
Bug introduced by
The property url does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
835
836
        return $this;
837
    }
838
}
839