Client::__call()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

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