Passed
Push — master ( c74e83...4330b5 )
by test
02:39
created

Client   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 167
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 1
Metric Value
eloc 41
c 5
b 1
f 1
dl 0
loc 167
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A batchSendMsg() 0 18 2
A withdrawMsg() 0 9 1
A queryMsg() 0 19 1
A sendMsg() 0 24 2
A importMsg() 0 16 1
1
<?php
2
3
namespace EasyIM\TencentIM\SingleChat;
4
5
use EasyIM\Kernel\BaseClient;
6
use EasyIM\Kernel\Contracts\MessageInterface;
7
use EasyIM\Kernel\Support\Arr;
8
use EasyIM\TencentIM\Kernel\Constant\SingleChatConstant;
9
use EasyIM\TencentIM\Kernel\OfflinePushInfo\OfflinePushElem;
10
11
/**
12
 * Class Client
13
 *
14
 * @package EasyIM\TencentIM\SingleChat
15
 * @author  longing <[email protected]>
16
 */
17
class Client extends BaseClient
18
{
19
    /**
20
     * Send single message.
21
     *
22
     * @param string               $toAccount
23
     * @param MessageInterface     $message
24
     * @param string|null          $fromAccount
25
     * @param int                  $msgLifeTime
26
     * @param int                  $syncOtherMachine
27
     * @param OfflinePushElem|null $offlinePushInfo
28
     * @param array                $forbidCallbackControl
29
     *
30
     * @return array|\EasyIM\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
31
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
32
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
33
     * @throws \GuzzleHttp\Exception\GuzzleException
34
     */
35
    public function sendMsg(
36
        string $toAccount,
37
        MessageInterface $message,
38
        string $fromAccount = null,
39
        int $msgLifeTime = 604800,
40
        int $syncOtherMachine = 2,
41
        OfflinePushElem $offlinePushInfo = null,
42
        array $forbidCallbackControl = []
43
    ) {
44
        $params = [
45
            'To_Account' => $toAccount,
46
            'MsgRandom' => msgRandom(),
47
            'MsgBody' => $message->transformToArray(),
48
            'MsgTimeStamp' => time(),
49
            'SyncOtherMachine' => $syncOtherMachine,
50
            'MsgLifeTime' => $msgLifeTime,
51
            'ForbidCallbackControl' => $forbidCallbackControl
52
        ];
53
54
55
        Arr::setNotNullValue($params, 'From_Account', $fromAccount);
56
        Arr::setNotNullValue($params, 'OfflinePushInfo', $offlinePushInfo && $offlinePushInfo->transformToArray());
57
58
        return $this->httpPostJson('openim/sendmsg', $params);
59
    }
60
61
62
    /**
63
     * Batch send single chat.
64
     *
65
     * @param array                $toAccount
66
     * @param MessageInterface     $message
67
     * @param string|null          $fromAccount
68
     * @param int                  $syncOtherMachine
69
     * @param OfflinePushElem|null $offlinePushInfo
70
     *
71
     * @return array|\EasyIM\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
72
     * @throws \EasyIM\Kernel\Exceptions\InvalidArgumentException
73
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
74
     * @throws \GuzzleHttp\Exception\GuzzleException
75
     */
76
    public function batchSendMsg(
77
        array $toAccount,
78
        MessageInterface $message,
79
        string $fromAccount = null,
80
        int $syncOtherMachine = SingleChatConstant::UN_SYNC_OTHER_MACHINE,
81
        OfflinePushElem $offlinePushInfo = null
82
    ) {
83
        $params = [
84
            'To_Account' => $toAccount,
85
            'MsgRandom' => msgRandom(),
86
            'MsgBody' => $message->transformToArray(),
87
            'SyncOtherMachine' => $syncOtherMachine,
88
        ];
89
90
        Arr::setNotNullValue($params, 'From_Account', $fromAccount);
91
        Arr::setNotNullValue($params, 'OfflinePushInfo', $offlinePushInfo && $offlinePushInfo->transformToArray());
92
93
        return $this->httpPostJson('openim/batchsendmsg', $params);
94
    }
95
96
97
    /**
98
     *
99
     * Import single chat message.
100
     *
101
     * @param string           $toAccount
102
     * @param string           $fromAccount
103
     * @param MessageInterface $message
104
     * @param int              $syncFromOldSystem
105
     *
106
     * @return array|\EasyIM\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
107
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
108
     * @throws \GuzzleHttp\Exception\GuzzleException
109
     */
110
    public function importMsg(
111
        string $toAccount,
112
        string $fromAccount,
113
        MessageInterface $message,
114
        int $syncFromOldSystem = SingleChatConstant::SYNC_FROM_OLD_SYSTEM_COUNT
115
    ) {
116
        $params = [
117
            'To_Account' => $toAccount,
118
            'From_Account' => $fromAccount,
119
            'MsgRandom' => msgRandom(),
120
            'MsgBody' => $message->transformToArray(),
121
            'MsgTimeStamp' => time(),
122
            'SyncFromOldSystem' => $syncFromOldSystem
123
        ];
124
125
        return $this->httpPostJson('openim/importmsg', $params);
126
    }
127
128
129
    /**
130
     * Query single chat message.
131
     *
132
     * @param string      $fromAccount
133
     * @param string      $toAccount
134
     * @param int         $maxCnt
135
     * @param int         $minTime
136
     * @param int         $maxTime
137
     * @param string|null $lastMsgKey
138
     *
139
     * @return array|\EasyIM\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
140
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
141
     * @throws \GuzzleHttp\Exception\GuzzleException
142
     */
143
    public function queryMsg(
144
        string $fromAccount,
145
        string $toAccount,
146
        int $minTime,
147
        int $maxTime,
148
        int $maxCnt = 60,
149
        string $lastMsgKey = null
150
    ) {
151
        $params = [
152
            'From_Account' => $fromAccount,
153
            'To_Account' => $toAccount,
154
            'MaxCnt' => $maxCnt,
155
            'MinTime' => $minTime,
156
            'MaxTime' => $maxTime,
157
        ];
158
159
        Arr::setNotNullValue($params, 'LastMsgKey', $lastMsgKey);
160
161
        return $this->httpPostJson('openim/admin_getroammsg', $params);
162
    }
163
164
    /**
165
     * Withdraw single chat message.
166
     *
167
     * @param string $fromAccount
168
     * @param string $toAccount
169
     * @param string $msgKey
170
     *
171
     * @return array|\EasyIM\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
172
     * @throws \EasyIM\Kernel\Exceptions\InvalidConfigException
173
     * @throws \GuzzleHttp\Exception\GuzzleException
174
     */
175
    public function withdrawMsg(string $fromAccount, string $toAccount, string $msgKey)
176
    {
177
        $params = [
178
            'From_Account' => $fromAccount,
179
            'To_Account' => $toAccount,
180
            'MsgKey' => $msgKey,
181
        ];
182
183
        return $this->httpPostJson('openim/admin_msgwithdraw', $params);
184
    }
185
}
186