Completed
Push — master ( 5ddcc0...9e0ead )
by Carlos
42s
created

Staff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 135
Duplicated Lines 14.81 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 62.07%

Importance

Changes 5
Bugs 2 Features 0
Metric Value
wmc 8
c 5
b 2
f 0
lcom 2
cbo 5
dl 20
loc 135
ccs 18
cts 29
cp 0.6207
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A lists() 0 4 1
A onlines() 0 4 1
A create() 10 10 1
A update() 10 10 1
A avatar() 0 4 1
A message() 0 6 1
A send() 0 4 1
A delete() 0 16 1

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_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
37
    const API_AVATAR_UPLOAD = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
38
39
    /**
40
     * List all staffs.
41
     *
42
     * @return array
43
     */
44 1
    public function lists()
45
    {
46 1
        return $this->parseJSON('get', [self::API_LISTS]);
47
    }
48
49
    /**
50
     * List all online staffs.
51
     *
52
     * @return array
53
     */
54 1
    public function onlines()
55
    {
56 1
        return $this->parseJSON('get', [self::API_ONLINE]);
57
    }
58
59
    /**
60
     * Create a staff.
61
     *
62
     * @param string $email
63
     * @param string $nickname
64
     * @param string $password
65
     *
66
     * @return bool
67
     */
68 1 View Code Duplication
    public function create($email, $nickname, $password)
69
    {
70
        $params = [
71 1
                   'kf_account' => $email,
72 1
                   'nickname' => $nickname,
73 1
                   'password' => $password,
74 1
                  ];
75
76 1
        return $this->parseJSON('json', [self::API_CREATE, $params]);
77
    }
78
79
    /**
80
     * Update a staff.
81
     *
82
     * @param string $email
83
     * @param string $nickname
84
     * @param string $password
85
     *
86
     * @return bool
87
     */
88 1 View Code Duplication
    public function update($email, $nickname, $password)
89
    {
90
        $params = [
91 1
                   'kf_account' => $email,
92 1
                   'nickname' => $nickname,
93 1
                   'password' => $password,
94 1
                  ];
95
96 1
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
97
    }
98
99
    /**
100
     * Delete a staff.
101
     *
102
     * @param string $email
103
     *
104
     * @return bool
105
     */
106
    public function delete($email)
107
    {
108
        // XXX: 微信那帮搞技术的都 TM 是 SB,url上的文本居然不 TM urlencode,
109
        // 这里客服账号因为有 @ 符,而微信不接收urlencode的账号。。
110
        // 简直是日了...
111
        // #222
112
        // PS: 如果你是微信做接口的,奉劝你们,尊重技术,不会别乱搞,笨不是你们的错,你们出来坑人就是大错特错。
113
        $accessTokenField = sprintf('%s=%s', $this->accessToken->getQueryName(), $this->accessToken->getToken());
114
        $url = sprintf(self::API_DELETE.'?%s&kf_account=%s', $accessTokenField, $email);
115
116
        $contents = $this->getHttp()->parseJSON(file_get_contents($url));
117
118
        $this->checkAndThrow($contents);
119
120
        return new Collection($contents);
121
    }
122
123
    /**
124
     * Set staff avatar.
125
     *
126
     * @param string $email
127
     * @param string $path
128
     *
129
     * @return bool
130
     */
131 1
    public function avatar($email, $path)
132
    {
133 1
        return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $email]]);
134
    }
135
136
    /**
137
     * Get message builder.
138
     *
139
     * @param \EasyWeChat\Message\AbstractMessage|string $message
140
     *
141
     * @return \EasyWeChat\Staff\MessageBuilder
142
     *
143
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
144
     */
145
    public function message($message)
146
    {
147
        $messageBuilder = new MessageBuilder($this);
148
149
        return $messageBuilder->message($message);
150
    }
151
152
    /**
153
     *  Send a message.
154
     *
155
     * @param string|array $message
156
     *
157
     * @return mixed
158
     */
159
    public function send($message)
160
    {
161
        return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
162
    }
163
}
164