Completed
Pull Request — master (#445)
by Carlos
06:52 queued 02:55
created

Staff   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 173
Duplicated Lines 10.4 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 53.85%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 18
loc 173
ccs 21
cts 39
cp 0.5385
rs 10
wmc 12
lcom 2
cbo 5

10 Methods

Rating   Name   Duplication   Size   Complexity  
A lists() 0 4 1
A onlines() 0 4 1
A create() 9 9 1
A update() 9 9 1
A delete() 0 16 1
A invite() 0 9 1
A avatar() 0 4 1
A message() 0 6 1
A send() 0 4 1
A records() 0 11 3

How to fix   Duplicated Code   

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:

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
 * Staff.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Staff;
22
23
use EasyWeChat\Core\AbstractAPI;
24
use EasyWeChat\Support\Collection;
25
26
/**
27
 * Class Staff.
28
 */
29
class Staff extends AbstractAPI
30
{
31
    const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
32
    const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist';
33
    const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del';
34
    const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update';
35
    const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add';
36
    const API_INVITE_BIND = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker';
37
    const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
38
    const API_AVATAR_UPLOAD = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
39
    const API_RECORDS = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord';
40
41
    /**
42
     * List all staffs.
43
     *
44
     * @return \EasyWeChat\Support\Collection
45
     */
46 1
    public function lists()
47
    {
48 1
        return $this->parseJSON('get', [self::API_LISTS]);
49
    }
50
51
    /**
52
     * List all online staffs.
53
     *
54
     * @return \EasyWeChat\Support\Collection
55
     */
56 1
    public function onlines()
57
    {
58 1
        return $this->parseJSON('get', [self::API_ONLINE]);
59
    }
60
61
    /**
62
     * Create a staff.
63
     *
64
     * @param string $account
65
     * @param string $nickname
66
     *
67
     * @return \EasyWeChat\Support\Collection
68
     */
69 1 View Code Duplication
    public function create($account, $nickname)
70
    {
71
        $params = [
72 1
                   'kf_account' => $account,
73 1
                   'nickname' => $nickname,
74 1
                  ];
75
76 1
        return $this->parseJSON('json', [self::API_CREATE, $params]);
77
    }
78
79
    /**
80
     * Update a staff.
81
     *
82
     * @param string $account
83
     * @param string $nickname
84
     *
85
     * @return \EasyWeChat\Support\Collection
86
     */
87 1 View Code Duplication
    public function update($account, $nickname)
88
    {
89
        $params = [
90 1
                   'kf_account' => $account,
91 1
                   'nickname' => $nickname,
92 1
                  ];
93
94 1
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
95
    }
96
97
    /**
98
     * Delete a staff.
99
     *
100
     * @param string $account
101
     *
102
     * @return \EasyWeChat\Support\Collection
103
     */
104
    public function delete($account)
105
    {
106
        // XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode,
107
        // 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。
108
        // 简直是日了...
109
        // #222
110
        // PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。
111
        $accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken());
112
        $url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $account);
113
114
        $contents = $this->getHttp()->parseJSON(file_get_contents($url));
115
116
        $this->checkAndThrow($contents);
117
118
        return new Collection($contents);
119
    }
120
121
    /**
122
     * Invite a staff.
123
     *
124
     * @param string $account
125
     * @param string $wechatId
126
     *
127
     * @return \EasyWeChat\Support\Collection
128
     */
129 1
    public function invite($account, $wechatId)
130
    {
131
        $params = [
132 1
                   'kf_account' => $account,
133 1
                   'invite_wx' => $wechatId,
134 1
                  ];
135
136 1
        return $this->parseJSON('json', [self::API_INVITE_BIND, $params]);
137
    }
138
139
    /**
140
     * Set staff avatar.
141
     *
142
     * @param string $account
143
     * @param string $path
144
     *
145
     * @return \EasyWeChat\Support\Collection
146
     */
147 1
    public function avatar($account, $path)
148
    {
149 1
        return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $account]]);
150
    }
151
152
    /**
153
     * Get message builder.
154
     *
155
     * @param \EasyWeChat\Message\AbstractMessage|string $message
156
     *
157
     * @return \EasyWeChat\Staff\MessageBuilder
158
     *
159
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
160
     */
161
    public function message($message)
162
    {
163
        $messageBuilder = new MessageBuilder($this);
164
165
        return $messageBuilder->message($message);
166
    }
167
168
    /**
169
     * Send a message.
170
     *
171
     * @param string|array $message
172
     *
173
     * @return \EasyWeChat\Support\Collection
174
     */
175
    public function send($message)
176
    {
177
        return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
178
    }
179
180
    /**
181
     * Get staff session history.
182
     *
183
     * @param int $startTime
184
     * @param int $endTime
185
     * @param int $page
186
     * @param int $pageSize
187
     *
188
     * @return \EasyWeChat\Support\Collection
189
     */
190
    public function records($startTime, $endTime, $page = 1, $pageSize = 10)
191
    {
192
        $params = [
193
                   'starttime' => is_numeric($startTime) ? $startTime : strtotime($startTime),
194
                   'endtime' => is_numeric($endTime) ? $endTime : strtotime($endTime),
195
                   'pageindex' => $page,
196
                   'pagesize' => $pageSize,
197
                  ];
198
199
        return $this->parseJSON('json', [self::API_RECORDS, $params]);
200
    }
201
}
202