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 |
||
30 | class Staff extends AbstractAPI |
||
31 | { |
||
32 | const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist'; |
||
33 | const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist'; |
||
34 | const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del'; |
||
35 | const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update'; |
||
36 | const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add'; |
||
37 | const API_INVITE_BIND = 'https://api.weixin.qq.com/customservice/kfaccount/inviteworker'; |
||
38 | const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send'; |
||
39 | const API_AVATAR_UPLOAD = 'https://api.weixin.qq.com/customservice/kfaccount/uploadheadimg'; |
||
40 | const API_RECORDS = 'https://api.weixin.qq.com/customservice/msgrecord/getrecord'; |
||
41 | const API_MSG_LIST = 'https://api.weixin.qq.com/customservice/msgrecord/getmsglist'; |
||
42 | |||
43 | /** |
||
44 | * List all staffs. |
||
45 | * |
||
46 | * @return \EasyWeChat\Support\Collection |
||
47 | */ |
||
48 | 1 | public function lists() |
|
49 | { |
||
50 | 1 | return $this->parseJSON('get', [self::API_LISTS]); |
|
51 | } |
||
52 | |||
53 | /** |
||
54 | * List all online staffs. |
||
55 | * |
||
56 | * @return \EasyWeChat\Support\Collection |
||
57 | */ |
||
58 | 1 | public function onlines() |
|
59 | { |
||
60 | 1 | return $this->parseJSON('get', [self::API_ONLINE]); |
|
61 | } |
||
62 | |||
63 | /** |
||
64 | * Create a staff. |
||
65 | * |
||
66 | * @param string $account |
||
67 | * @param string $nickname |
||
68 | * |
||
69 | * @return \EasyWeChat\Support\Collection |
||
70 | */ |
||
71 | 1 | View Code Duplication | public function create($account, $nickname) |
72 | { |
||
73 | $params = [ |
||
74 | 1 | 'kf_account' => $account, |
|
75 | 1 | 'nickname' => $nickname, |
|
76 | 1 | ]; |
|
77 | |||
78 | 1 | return $this->parseJSON('json', [self::API_CREATE, $params]); |
|
79 | } |
||
80 | |||
81 | /** |
||
82 | * Update a staff. |
||
83 | * |
||
84 | * @param string $account |
||
85 | * @param string $nickname |
||
86 | * |
||
87 | * @return \EasyWeChat\Support\Collection |
||
88 | */ |
||
89 | 1 | View Code Duplication | public function update($account, $nickname) |
90 | { |
||
91 | $params = [ |
||
92 | 1 | 'kf_account' => $account, |
|
93 | 1 | 'nickname' => $nickname, |
|
94 | 1 | ]; |
|
95 | |||
96 | 1 | return $this->parseJSON('json', [self::API_UPDATE, $params]); |
|
97 | } |
||
98 | |||
99 | /** |
||
100 | * Delete a staff. |
||
101 | * |
||
102 | * @param string $account |
||
103 | * |
||
104 | * @return \EasyWeChat\Support\Collection |
||
105 | */ |
||
106 | public function delete($account) |
||
122 | |||
123 | /** |
||
124 | * Invite a staff. |
||
125 | * |
||
126 | * @param string $account |
||
127 | * @param string $wechatId |
||
128 | * |
||
129 | * @return \EasyWeChat\Support\Collection |
||
130 | */ |
||
131 | 1 | public function invite($account, $wechatId) |
|
140 | |||
141 | /** |
||
142 | * Set staff avatar. |
||
143 | * |
||
144 | * @param string $account |
||
145 | * @param string $path |
||
146 | * |
||
147 | * @return \EasyWeChat\Support\Collection |
||
148 | */ |
||
149 | 1 | public function avatar($account, $path) |
|
153 | |||
154 | /** |
||
155 | * Get message builder. |
||
156 | * |
||
157 | * @param \EasyWeChat\Message\AbstractMessage|string $message |
||
158 | * |
||
159 | * @return \EasyWeChat\Staff\MessageBuilder |
||
160 | * |
||
161 | * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException |
||
162 | */ |
||
163 | public function message($message) |
||
169 | |||
170 | /** |
||
171 | * Send a message. |
||
172 | * |
||
173 | * @param string|array $message |
||
174 | * |
||
175 | * @return \EasyWeChat\Support\Collection |
||
176 | */ |
||
177 | public function send($message) |
||
181 | |||
182 | /** |
||
183 | * Get staff session history. |
||
184 | * |
||
185 | * @param int $startTime |
||
186 | * @param int $endTime |
||
187 | * @param int $page |
||
188 | * @param int $pageSize |
||
189 | * |
||
190 | * @return \EasyWeChat\Support\Collection |
||
191 | */ |
||
192 | View Code Duplication | public function records($startTime, $endTime, $page = 1, $pageSize = 10) |
|
203 | |||
204 | /** |
||
205 | * 获取聊天记录. |
||
206 | * |
||
207 | * @param int $startTime |
||
208 | * @param int $endTime |
||
209 | * @param int $msgId |
||
210 | * @param int $number |
||
211 | * |
||
212 | * @return \EasyWeChat\Support\Collection |
||
213 | */ |
||
214 | View Code Duplication | public function messages($startTime, $endTime, $msgId = 1, $number = 10000) |
|
225 | } |
||
226 |