Completed
Push — master ( cdafa1...c7d4dc )
by Carlos
09:26
created

Staff   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 123
Duplicated Lines 16.26 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 80%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 8
c 3
b 0
f 0
lcom 1
cbo 2
dl 20
loc 123
ccs 20
cts 25
cp 0.8
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 delete() 0 4 1
A avatar() 0 4 1
A message() 0 6 1
A send() 0 4 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
25
/**
26
 * Class Staff.
27
 */
28
class Staff extends AbstractAPI
29
{
30
    const API_LISTS = 'https://api.weixin.qq.com/cgi-bin/customservice/getkflist';
31
    const API_ONLINE = 'https://api.weixin.qq.com/cgi-bin/customservice/getonlinekflist';
32
    const API_DELETE = 'https://api.weixin.qq.com/customservice/kfaccount/del';
33
    const API_UPDATE = 'https://api.weixin.qq.com/customservice/kfaccount/update';
34
    const API_CREATE = 'https://api.weixin.qq.com/customservice/kfaccount/add';
35
    const API_MESSAGE_SEND = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
36
    const API_AVATAR_UPLOAD = 'http://api.weixin.qq.com/customservice/kfaccount/uploadheadimg';
37
38
    /**
39
     * List all staffs.
40
     *
41
     * @return array
42
     */
43 1
    public function lists()
44
    {
45 1
        return $this->parseJSON('get', [self::API_LISTS]);
46
    }
47
48
    /**
49
     * List all online staffs.
50
     *
51
     * @return array
52
     */
53 1
    public function onlines()
54
    {
55 1
        return $this->parseJSON('get', [self::API_ONLINE]);
56
    }
57
58
    /**
59
     * Create a staff.
60
     *
61
     * @param string $email
62
     * @param string $nickname
63
     * @param string $password
64
     *
65
     * @return bool
66
     */
67 1 View Code Duplication
    public function create($email, $nickname, $password)
68
    {
69
        $params = [
70 1
                   'kf_account' => $email,
71 1
                   'nickname' => $nickname,
72 1
                   'password' => $password,
73 1
                  ];
74
75 1
        return $this->parseJSON('json', [self::API_CREATE, $params]);
76
    }
77
78
    /**
79
     * Update a staff.
80
     *
81
     * @param string $email
82
     * @param string $nickname
83
     * @param string $password
84
     *
85
     * @return bool
86
     */
87 1 View Code Duplication
    public function update($email, $nickname, $password)
88
    {
89
        $params = [
90 1
                   'kf_account' => $email,
91 1
                   'nickname' => $nickname,
92 1
                   'password' => $password,
93 1
                  ];
94
95 1
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
96
    }
97
98
    /**
99
     * Delete a staff.
100
     *
101
     * @param string $email
102
     *
103
     * @return bool
104
     */
105 1
    public function delete($email)
106
    {
107 1
        return $this->parseJSON('get', [sprintf(self::API_DELETE.'?kf_account=%s', $email)]);
108
    }
109
110
    /**
111
     * Set staff avatar.
112
     *
113
     * @param string $email
114
     * @param string $path
115
     *
116
     * @return bool
117
     */
118 1
    public function avatar($email, $path)
119
    {
120 1
        return $this->parseJSON('upload', [self::API_AVATAR_UPLOAD, ['media' => $path], [], ['kf_account' => $email]]);
121
    }
122
123
    /**
124
     * Get message builder.
125
     *
126
     * @param \EasyWeChat\Message\AbstractMessage|string $message
127
     *
128
     * @return \EasyWeChat\Staff\MessageBuilder
129
     *
130
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
131
     */
132
    public function message($message)
133
    {
134
        $messageBuilder = new MessageBuilder($this);
135
136
        return $messageBuilder->message($message);
0 ignored issues
show
Bug introduced by
It seems like $message defined by parameter $message on line 132 can also be of type string; however, EasyWeChat\Staff\MessageBuilder::message() does only seem to accept object<EasyWeChat\Message\AbstractMessage>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
137
    }
138
139
    /**
140
     *  Send a message.
141
     *
142
     * @param string|array $message
143
     *
144
     * @return mixed
145
     */
146
    public function send($message)
147
    {
148
        return $this->parseJSON('json', [self::API_MESSAGE_SEND, $message]);
149
    }
150
}
151