Passed
Push — master ( e50567...183de1 )
by Carlos
02:58
created

Client   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 341
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 341
ccs 46
cts 46
cp 1
rs 10
c 0
b 0
f 0
wmc 26

19 Methods

Rating   Name   Duplication   Size   Complexity  
A __call() 0 13 3
A sendNews() 0 3 1
A previewVideo() 0 3 1
A previewCard() 0 3 1
A previewVoice() 0 3 1
A delete() 0 7 1
A sendMessage() 0 11 3
A sendVideo() 0 3 1
A status() 0 7 1
A preview() 0 3 1
A sendText() 0 3 1
A sendImage() 0 3 1
A previewImage() 0 3 1
A previewNews() 0 3 1
A sendVoice() 0 3 1
A send() 0 9 4
A sendCard() 0 3 1
A previewText() 0 3 1
A previewMessage() 0 5 1
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
namespace EasyWeChat\OfficialAccount\Broadcasting;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Contracts\MessageInterface;
16
use EasyWeChat\Kernel\Exceptions\RuntimeException;
17
use EasyWeChat\Kernel\Messages\Card;
18
use EasyWeChat\Kernel\Messages\Image;
19
use EasyWeChat\Kernel\Messages\Media;
20
use EasyWeChat\Kernel\Messages\Text;
21
use EasyWeChat\Kernel\Support\Arr;
22
23
/**
24
 * Class Client.
25
 *
26
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewTextByName($text, $name);
27
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewNewsByName($mediaId, $name);
28
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewVoiceByName($mediaId, $name);
29
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewImageByName($mediaId, $name);
30
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewVideoByName($message, $name);
31
 * @method \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string previewCardByName($cardId, $name);
32
 *
33
 * @author overtrue <[email protected]>
34
 */
35
class Client extends BaseClient
36
{
37
    const PREVIEW_BY_OPENID = 'touser';
38
    const PREVIEW_BY_NAME = 'towxname';
39
40
    /**
41
     * Send a message.
42
     *
43
     * @param array $message
44
     *
45
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
46
     *
47
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
48
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
49
     */
50 1
    public function send(array $message)
51
    {
52 1
        if (empty($message['filter']) && empty($message['touser'])) {
53 1
            throw new RuntimeException('The message reception object is not specified');
54
        }
55
56 1
        $api = Arr::get($message, 'touser') ? 'cgi-bin/message/mass/send' : 'cgi-bin/message/mass/sendall';
57
58 1
        return $this->httpPostJson($api, $message);
59
    }
60
61
    /**
62
     * Preview a message.
63
     *
64
     * @param array $message
65
     *
66
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
67
     *
68
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
69
     */
70 1
    public function preview(array $message)
71
    {
72 1
        return $this->httpPostJson('cgi-bin/message/mass/preview', $message);
73
    }
74
75
    /**
76
     * Delete a broadcast.
77
     *
78
     * @param string $msgId
79
     *
80
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
81
     *
82
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
83
     */
84 1
    public function delete(string $msgId)
85
    {
86
        $options = [
87 1
            'msg_id' => $msgId,
88
        ];
89
90 1
        return $this->httpPostJson('cgi-bin/message/mass/delete', $options);
91
    }
92
93
    /**
94
     * Get a broadcast status.
95
     *
96
     * @param string $msgId
97
     *
98
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
99
     *
100
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
101
     */
102 1
    public function status(string $msgId)
103
    {
104
        $options = [
105 1
            'msg_id' => $msgId,
106
        ];
107
108 1
        return $this->httpPostJson('cgi-bin/message/mass/get', $options);
109
    }
110
111
    /**
112
     * Send a text message.
113
     *
114
     * @param string $message
115
     * @param mixed  $reception
116
     * @param array  $attributes
117
     *
118
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
119
     *
120
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
121
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
122
     */
123 1
    public function sendText(string $message, $reception = null, array $attributes = [])
124
    {
125 1
        return $this->sendMessage(new Text($message), $reception, $attributes);
126
    }
127
128
    /**
129
     * Send a news message.
130
     *
131
     * @param string $mediaId
132
     * @param mixed  $reception
133
     * @param array  $attributes
134
     *
135
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
136
     *
137
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
138
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
139
     */
140 1
    public function sendNews(string $mediaId, $reception = null, array $attributes = [])
141
    {
142 1
        return $this->sendMessage(new Media($mediaId, 'mpnews'), $reception, $attributes);
143
    }
144
145
    /**
146
     * Send a voice message.
147
     *
148
     * @param string $mediaId
149
     * @param mixed  $reception
150
     * @param array  $attributes
151
     *
152
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
153
     *
154
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
155
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
156
     */
157 1
    public function sendVoice(string $mediaId, $reception = null, array $attributes = [])
158
    {
159 1
        return $this->sendMessage(new Media($mediaId, 'voice'), $reception, $attributes);
160
    }
161
162
    /**
163
     * Send a image message.
164
     *
165
     * @param string $mediaId
166
     * @param mixed  $reception
167
     * @param array  $attributes
168
     *
169
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
170
     *
171
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
172
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
173
     */
174 1
    public function sendImage(string $mediaId, $reception = null, array $attributes = [])
175
    {
176 1
        return $this->sendMessage(new Image($mediaId), $reception, $attributes);
177
    }
178
179
    /**
180
     * Send a video message.
181
     *
182
     * @param string $mediaId
183
     * @param mixed  $reception
184
     * @param array  $attributes
185
     *
186
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
187
     *
188
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
189
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
190
     */
191 1
    public function sendVideo(string $mediaId, $reception = null, array $attributes = [])
192
    {
193 1
        return $this->sendMessage(new Media($mediaId, 'mpvideo'), $reception, $attributes);
194
    }
195
196
    /**
197
     * Send a card message.
198
     *
199
     * @param string $cardId
200
     * @param mixed  $reception
201
     * @param array  $attributes
202
     *
203
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
204
     *
205
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
206
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
207
     */
208 1
    public function sendCard(string $cardId, $reception = null, array $attributes = [])
209
    {
210 1
        return $this->sendMessage(new Card($cardId), $reception, $attributes);
211
    }
212
213
    /**
214
     * Preview a text message.
215
     *
216
     * @param string $message   message
217
     * @param string $reception
218
     * @param string $method
219
     *
220
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
221
     *
222
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
223
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
224
     */
225 1
    public function previewText(string $message, $reception, $method = self::PREVIEW_BY_OPENID)
226
    {
227 1
        return $this->previewMessage(new Text($message), $reception, $method);
228
    }
229
230
    /**
231
     * Preview a news message.
232
     *
233
     * @param string $mediaId   message
234
     * @param string $reception
235
     * @param string $method
236
     *
237
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
238
     *
239
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
240
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
241
     */
242 1
    public function previewNews(string $mediaId, $reception, $method = self::PREVIEW_BY_OPENID)
243
    {
244 1
        return $this->previewMessage(new Media($mediaId, 'mpnews'), $reception, $method);
245
    }
246
247
    /**
248
     * Preview a voice message.
249
     *
250
     * @param string $mediaId   message
251
     * @param string $reception
252
     * @param string $method
253
     *
254
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
255
     *
256
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
257
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
258
     */
259 1
    public function previewVoice(string $mediaId, $reception, $method = self::PREVIEW_BY_OPENID)
260
    {
261 1
        return $this->previewMessage(new Media($mediaId, 'voice'), $reception, $method);
262
    }
263
264
    /**
265
     * Preview a image message.
266
     *
267
     * @param string $mediaId   message
268
     * @param string $reception
269
     * @param string $method
270
     *
271
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
272
     *
273
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
274
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
275
     */
276 1
    public function previewImage(string $mediaId, $reception, $method = self::PREVIEW_BY_OPENID)
277
    {
278 1
        return $this->previewMessage(new Image($mediaId), $reception, $method);
279
    }
280
281
    /**
282
     * Preview a video message.
283
     *
284
     * @param string $mediaId   message
285
     * @param string $reception
286
     * @param string $method
287
     *
288
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
289
     *
290
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
291
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
292
     */
293 1
    public function previewVideo(string $mediaId, $reception, $method = self::PREVIEW_BY_OPENID)
294
    {
295 1
        return $this->previewMessage(new Media($mediaId, 'mpvideo'), $reception, $method);
296
    }
297
298
    /**
299
     * Preview a card message.
300
     *
301
     * @param string $cardId    message
302
     * @param string $reception
303
     * @param string $method
304
     *
305
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
306
     *
307
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
308
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
309
     */
310 1
    public function previewCard(string $cardId, $reception, $method = self::PREVIEW_BY_OPENID)
311
    {
312 1
        return $this->previewMessage(new Card($cardId), $reception, $method);
313
    }
314
315
    /**
316
     * @param \EasyWeChat\Kernel\Contracts\MessageInterface $message
317
     * @param string                                        $reception
318
     * @param string                                        $method
319
     *
320
     * @return mixed
321
     *
322
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
323
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
324
     */
325 1
    public function previewMessage(MessageInterface $message, string $reception, $method = self::PREVIEW_BY_OPENID)
326
    {
327 1
        $message = (new MessageBuilder())->message($message)->buildForPreview($method, $reception);
328
329 1
        return $this->preview($message);
330
    }
331
332
    /**
333
     * @param \EasyWeChat\Kernel\Contracts\MessageInterface $message
334
     * @param mixed                                         $reception
335
     * @param array                                         $attributes
336
     *
337
     * @return mixed
338
     *
339
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
340
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
341
     */
342 1
    public function sendMessage(MessageInterface $message, $reception = null, $attributes = [])
343
    {
344 1
        $message = (new MessageBuilder())->message($message)->with($attributes)->toAll();
345
346 1
        if (\is_int($reception)) {
347 1
            $message->toTag($reception);
348
        } elseif (\is_array($reception)) {
349 1
            $message->toUsers($reception);
350
        }
351
352 1
        return $this->send($message->build());
353
    }
354
355
    /**
356
     * @codeCoverageIgnore
357
     *
358
     * @param string $method
359
     * @param array  $args
360
     *
361
     * @return mixed
362
     */
363
    public function __call($method, $args)
364
    {
365
        if (strpos($method, 'ByName') > 0) {
366
            $method = strstr($method, 'ByName', true);
367
368
            if (method_exists($this, $method)) {
369
                array_push($args, self::PREVIEW_BY_NAME);
370
371
                return $this->$method(...$args);
372
            }
373
        }
374
375
        throw new \BadMethodCallException(sprintf('Method %s not exists.', $method));
376
    }
377
}
378