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

MessageClient::deleteGroupMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
namespace EasyIM\TencentIM\Group;
4
5
use EasyIM\Kernel\BaseClient;
6
use EasyIM\Kernel\Contracts\MessageInterface;
7
use EasyIM\Kernel\Exceptions\InvalidArgumentException;
8
use EasyIM\Kernel\Exceptions\InvalidConfigException;
9
use EasyIM\Kernel\Support\Arr;
10
use EasyIM\Kernel\Support\Collection;
11
use EasyIM\TencentIM\Kernel\OfflinePushInfo\OfflinePushElem;
12
use GuzzleHttp\Exception\GuzzleException;
13
use Psr\Http\Message\ResponseInterface;
14
15
/**
16
 * Class MessageClient
17
 *
18
 * @package EasyIM\TencentIM\Group
19
 * @author  yingzhan <[email protected]>
20
 *
21
 */
22
class MessageClient extends BaseClient
23
{
24
    /**
25
     * Send normal messages in groups.
26
     *
27
     * @param string               $groupId
28
     * @param MessageInterface     $msgBody
29
     * @param string|null          $fromAccount
30
     * @param string|null          $msgPriority
31
     * @param array|null           $forbidCallbackControl
32
     * @param OfflinePushElem|null $offlinePushInfo
33
     * @param int|null             $onlineOnlyFlag
34
     *
35
     * @return array|Collection|object|ResponseInterface|string
36
     * @throws InvalidArgumentException
37
     * @throws InvalidConfigException
38
     * @throws GuzzleException
39
     */
40
    public function sendMsg(
41
        string $groupId,
42
        MessageInterface $msgBody,
43
        string $fromAccount = null,
44
        string $msgPriority = null,
45
        array $forbidCallbackControl = null,
46
        OfflinePushElem $offlinePushInfo = null,
47
        int $onlineOnlyFlag = null
48
    ) {
49
        $params = [
50
            'GroupId' => $groupId,
51
            'Random' => msgRandom(7),
52
            'MsgBody' => $msgBody->transformToArray()
53
        ];
54
        Arr::setNotNullValue($params, 'From_Account', $fromAccount);
55
        Arr::setNotNullValue($params, 'MsgPriority', $msgPriority);
56
        Arr::setNotNullValue($params, 'ForbidCallbackControl', $forbidCallbackControl);
57
        Arr::setNotNullValue($params, 'OfflinePushInfo', $offlinePushInfo && $offlinePushInfo->transformToArray());
58
        Arr::setNotNullValue($params, 'OnlineOnlyFlag', $onlineOnlyFlag);
59
60
        return $this->httpPostJson('group_open_http_svc/send_group_msg', $params);
61
    }
62
63
    /**
64
     * Send system notification in group.
65
     *
66
     * @param string     $groupId
67
     * @param string     $content
68
     * @param array|null $toMembersAccount
69
     *
70
     * @return array|Collection|object|ResponseInterface|string
71
     * @throws InvalidConfigException
72
     * @throws GuzzleException
73
     */
74
    public function sendNotification(
75
        string $groupId,
76
        string $content,
77
        array $toMembersAccount = null
78
    ) {
79
        $params = [
80
            'GroupId' => $groupId,
81
            'Content' => $content
82
        ];
83
        Arr::setNotNullValue($params, 'ToMembers_Account', $toMembersAccount);
84
85
        return $this->httpPostJson('group_open_http_svc/send_group_system_notification', $params);
86
    }
87
88
    /**
89
     * Recall group message.
90
     *
91
     * @param string    $groupId
92
     * @param array     $msgSeqList
93
     *
94
     * @return array|Collection|object|ResponseInterface|string
95
     * @throws InvalidConfigException
96
     * @throws GuzzleException
97
     */
98
    public function recallGroupMsg(string $groupId, array $msgSeqList)
99
    {
100
        $params = [
101
            'GroupId' => $groupId,
102
            'MsgSeqList' => array_map(function ($item) {
103
                return ['MsgSeq' => $item];
104
            }, $msgSeqList)
105
        ];
106
107
        return $this->httpPostJson('group_open_http_svc/group_msg_recall', $params);
108
    }
109
110
    /**
111
     * Delete messages sent by the specified user.
112
     *
113
     * @param string $groupId
114
     * @param string $senderAccount
115
     *
116
     * @return array|Collection|object|ResponseInterface|string
117
     * @throws InvalidConfigException
118
     * @throws GuzzleException
119
     */
120
    public function deleteGroupMsg(string $groupId, string $senderAccount)
121
    {
122
        $params = [
123
            'GroupId' => $groupId,
124
            'Sender_Account' => $senderAccount
125
        ];
126
127
        return $this->httpPostJson('group_open_http_svc/delete_group_msg_by_sender', $params);
128
    }
129
130
    /**
131
     * Pull group historical news.
132
     *
133
     * @param string   $groupId
134
     * @param int      $reqMsgNumber
135
     * @param int|null $reqMsgSeq
136
     *
137
     * @return array|Collection|object|ResponseInterface|string
138
     * @throws InvalidConfigException
139
     * @throws GuzzleException
140
     */
141
    public function getMsgSimple(string $groupId, int $reqMsgNumber, int $reqMsgSeq = null)
142
    {
143
        $params = [
144
            'GroupId' => $groupId,
145
            'ReqMsgNumber' => $reqMsgNumber
146
        ];
147
        Arr::setNotNullValue($params, 'ReqMsgSeq', $reqMsgSeq);
148
149
        return $this->httpPostJson('group_open_http_svc/group_msg_get_simple', $params);
150
    }
151
}
152