Chat   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 152
Duplicated Lines 11.84 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 18
loc 152
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 2

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get() 0 8 1
A create() 0 11 1
A update() 9 9 1
A quit() 9 9 1
A clear() 0 9 1
A send() 0 14 1
A mute() 0 8 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
namespace EntWeChat\Chat;
4
5
use EntWeChat\Core\AbstractAPI;
6
7
/**
8
 * Class Chat.
9
 */
10
class Chat extends AbstractAPI
11
{
12
    const API_GET = 'https://qyapi.weixin.qq.com/cgi-bin/chat/get';
13
    const API_CREATE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/create';
14
    const API_UPDATE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/update';
15
    const API_QUIT = 'https://qyapi.weixin.qq.com/cgi-bin/chat/quit';
16
    const API_CLEAR_NOTIFY = 'https://qyapi.weixin.qq.com/cgi-bin/chat/clearnotify';
17
    const API_SEND = 'https://qyapi.weixin.qq.com/cgi-bin/chat/send';
18
    const API_SET_MUTE = 'https://qyapi.weixin.qq.com/cgi-bin/chat/setmute';
19
20
    const CHAT_TYPE_SINGLE = 'single';  // 单聊
21
    const CHAT_TYPE_GROUP = 'group';   // 群聊
22
23
    const MSG_TYPE_TEXT = 'text';   // 文本
24
    const MSG_TYPE_VOICE = 'voice';  // 语音
25
    const MSG_TYPE_IMAGE = 'image';  // 图片
26
    const MSG_TYPE_FILE = 'file';   // 文件
27
    const MSG_TYPE_LINK = 'link';   // 文件
28
29
    /**
30
     * Fetch a chat by chat id.
31
     *
32
     * @param string $chatId
33
     *
34
     * @return \EntWeChat\Support\Collection
35
     */
36
    public function get($chatId)
37
    {
38
        $params = [
39
            'chatid' => $chatId,
40
        ];
41
42
        return $this->parseJSON('get', [self::API_GET, $params]);
43
    }
44
45
    /**
46
     * Create chat.
47
     *
48
     * @param string $chatId
49
     * @param string $name
50
     * @param string $owner
51
     * @param array  $userList
52
     *
53
     * @return \EntWeChat\Support\Collection
54
     */
55
    public function create($chatId, $name, $owner, array $userList)
56
    {
57
        $params = [
58
            'chatid'   => $chatId,
59
            'name'     => $name,
60
            'owner'    => $owner,
61
            'userlist' => $userList,
62
        ];
63
64
        return $this->parseJSON('json', [self::API_CREATE, $params]);
65
    }
66
67
    /**
68
     * Update chat.
69
     *
70
     * @param string $chatId
71
     * @param string $opUser
72
     * @param array  $chatInfo
73
     *
74
     * @return \EntWeChat\Support\Collection
75
     */
76 View Code Duplication
    public function update($chatId, $opUser, array $chatInfo = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        $params = array_merge($chatInfo, [
79
            'chatid'  => $chatId,
80
            'op_user' => $opUser,
81
        ]);
82
83
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
84
    }
85
86
    /**
87
     * Quit chat.
88
     *
89
     * @param string $chatId
90
     * @param string $opUser
91
     *
92
     * @return \EntWeChat\Support\Collection
93
     */
94 View Code Duplication
    public function quit($chatId, $opUser)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        $params = [
97
            'chatid'  => $chatId,
98
            'op_user' => $opUser,
99
        ];
100
101
        return $this->parseJSON('json', [self::API_QUIT, $params]);
102
    }
103
104
    /**
105
     * Clear chat.
106
     *
107
     * @param string $chatId
108
     * @param array  $chat
109
     *
110
     * @return \EntWeChat\Support\Collection
111
     */
112
    public function clear($chatId, array $chat)
113
    {
114
        $params = [
115
            'chatid' => $chatId,
116
            'chat'   => $chat,
117
        ];
118
119
        return $this->parseJSON('json', [self::API_CLEAR_NOTIFY, $params]);
120
    }
121
122
    /**
123
     * Send chat.
124
     *
125
     * @param string $type
126
     * @param string $id
127
     * @param string $sender
128
     * @param string $msgType
129
     * @param mixed  $message
130
     *
131
     * @return \EntWeChat\Support\Collection
132
     */
133
    public function send($type, $id, $sender, $msgType, $message)
134
    {
135
        $content = (new Transformer($msgType, $message))->transform();
136
137
        $params = array_merge([
138
            'receiver' => [
139
                'type' => $type,
140
                'id'   => $id,
141
            ],
142
            'sender' => $sender,
143
        ], $content);
144
145
        return $this->parseJSON('json', [self::API_SEND, $params]);
146
    }
147
148
    /**
149
     * Set chat.
150
     *
151
     * @return \EntWeChat\Support\Collection
152
     */
153
    public function mute(array $userMuteList)
154
    {
155
        $params = [
156
            'user_mute_list' => $userMuteList,
157
        ];
158
159
        return $this->parseJSON('json', [self::API_SET_MUTE, $params]);
160
    }
161
}
162