Client   A
last analyzed

Complexity

Total Complexity 23

Size/Duplication

Total Lines 277
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 48
c 2
b 0
f 0
dl 0
loc 277
ccs 54
cts 54
cp 1
rs 10
wmc 23

13 Methods

Rating   Name   Duplication   Size   Complexity  
A uploadImage() 0 3 1
A uploadVoice() 0 3 1
A uploadThumb() 0 3 1
A uploadVideo() 0 13 1
A stats() 0 3 1
A delete() 0 3 1
A updateArticle() 0 13 4
A uploadArticleImage() 0 3 1
A get() 0 9 2
A upload() 0 9 3
A list() 0 9 1
A getApiByType() 0 7 2
A uploadArticle() 0 15 4
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\Material;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use EasyWeChat\Kernel\Http\StreamResponse;
17
use EasyWeChat\Kernel\Messages\Article;
18
19
/**
20
 * Class Client.
21
 *
22
 * @author overtrue <[email protected]>
23
 */
24
class Client extends BaseClient
25
{
26
    /**
27
     * Allow media type.
28
     *
29
     * @var array
30
     */
31
    protected $allowTypes = ['image', 'voice', 'video', 'thumb', 'news_image'];
32
33
    /**
34
     * Upload image.
35
     *
36
     * @param string $path
37
     *
38
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
39
     *
40
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
41
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
42
     * @throws \GuzzleHttp\Exception\GuzzleException
43
     */
44 1
    public function uploadImage(string $path)
45
    {
46 1
        return $this->upload('image', $path);
47
    }
48
49
    /**
50
     * Upload voice.
51
     *
52
     * @param string $path
53
     *
54
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
55
     *
56
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
57
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
58
     * @throws \GuzzleHttp\Exception\GuzzleException
59
     */
60 1
    public function uploadVoice(string $path)
61
    {
62 1
        return $this->upload('voice', $path);
63
    }
64
65
    /**
66
     * Upload thumb.
67
     *
68
     * @param string $path
69
     *
70
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
71
     *
72
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
73
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
74
     * @throws \GuzzleHttp\Exception\GuzzleException
75
     */
76 1
    public function uploadThumb(string $path)
77
    {
78 1
        return $this->upload('thumb', $path);
79
    }
80
81
    /**
82
     * Upload video.
83
     *
84
     * @param string $path
85
     * @param string $title
86
     * @param string $description
87
     *
88
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
89
     *
90
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
91
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
92
     * @throws \GuzzleHttp\Exception\GuzzleException
93
     */
94 1
    public function uploadVideo(string $path, string $title, string $description)
95
    {
96
        $params = [
97 1
            'description' => json_encode(
98
                [
99 1
                    'title' => $title,
100 1
                    'introduction' => $description,
101
                ],
102 1
                JSON_UNESCAPED_UNICODE
103
            ),
104
        ];
105
106 1
        return $this->upload('video', $path, $params);
107
    }
108
109
    /**
110
     * Upload articles.
111
     *
112
     * @param array|Article $articles
113
     *
114
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
115
     *
116
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
117
     * @throws \GuzzleHttp\Exception\GuzzleException
118
     */
119 1
    public function uploadArticle($articles)
120
    {
121 1
        if ($articles instanceof Article || !empty($articles['title'])) {
122 1
            $articles = [$articles];
123
        }
124
125 1
        $params = ['articles' => array_map(function ($article) {
126 1
            if ($article instanceof Article) {
127 1
                return $article->transformForJsonRequestWithoutType();
128
            }
129
130 1
            return $article;
131 1
        }, $articles)];
132
133 1
        return $this->httpPostJson('cgi-bin/material/add_news', $params);
134
    }
135
136
    /**
137
     * Update article.
138
     *
139
     * @param string        $mediaId
140
     * @param array|Article $article
141
     * @param int           $index
142
     *
143
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
144
     *
145
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
146
     * @throws \GuzzleHttp\Exception\GuzzleException
147
     */
148 1
    public function updateArticle(string $mediaId, $article, int $index = 0)
149
    {
150 1
        if ($article instanceof Article) {
151 1
            $article = $article->transformForJsonRequestWithoutType();
152
        }
153
154
        $params = [
155 1
            'media_id' => $mediaId,
156 1
            'index' => $index,
157 1
            'articles' => isset($article['title']) ? $article : (isset($article[$index]) ? $article[$index] : []),
158
        ];
159
160 1
        return $this->httpPostJson('cgi-bin/material/update_news', $params);
161
    }
162
163
    /**
164
     * Upload image for article.
165
     *
166
     * @param string $path
167
     *
168
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
169
     *
170
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
171
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
172
     */
173 1
    public function uploadArticleImage(string $path)
174
    {
175 1
        return $this->upload('news_image', $path);
176
    }
177
178
    /**
179
     * Fetch material.
180
     *
181
     * @param string $mediaId
182
     *
183
     * @return mixed
184
     *
185
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
186
     * @throws \GuzzleHttp\Exception\GuzzleException
187
     */
188 1
    public function get(string $mediaId)
189
    {
190 1
        $response = $this->requestRaw('cgi-bin/material/get_material', 'POST', ['json' => ['media_id' => $mediaId]]);
191
192 1
        if (false !== stripos($response->getHeaderLine('Content-disposition'), 'attachment')) {
193 1
            return StreamResponse::buildFromPsrResponse($response);
194
        }
195
196 1
        return $this->castResponseToType($response, $this->app['config']->get('response_type'));
197
    }
198
199
    /**
200
     * Delete material by media ID.
201
     *
202
     * @param string $mediaId
203
     *
204
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
205
     *
206
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
207
     * @throws \GuzzleHttp\Exception\GuzzleException
208
     */
209 1
    public function delete(string $mediaId)
210
    {
211 1
        return $this->httpPostJson('cgi-bin/material/del_material', ['media_id' => $mediaId]);
212
    }
213
214
    /**
215
     * List materials.
216
     *
217
     * example:
218
     *
219
     * {
220
     *   "total_count": TOTAL_COUNT,
221
     *   "item_count": ITEM_COUNT,
222
     *   "item": [{
223
     *             "media_id": MEDIA_ID,
224
     *             "name": NAME,
225
     *             "update_time": UPDATE_TIME
226
     *         },
227
     *         // more...
228
     *   ]
229
     * }
230
     *
231
     * @param string $type
232
     * @param int    $offset
233
     * @param int    $count
234
     *
235
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
236
     *
237
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
238
     * @throws \GuzzleHttp\Exception\GuzzleException
239
     */
240 1
    public function list(string $type, int $offset = 0, int $count = 20)
241
    {
242
        $params = [
243 1
            'type' => $type,
244 1
            'offset' => $offset,
245 1
            'count' => $count,
246
        ];
247
248 1
        return $this->httpPostJson('cgi-bin/material/batchget_material', $params);
249
    }
250
251
    /**
252
     * Get stats of materials.
253
     *
254
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
255
     *
256
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
257
     */
258 1
    public function stats()
259
    {
260 1
        return $this->httpGet('cgi-bin/material/get_materialcount');
261
    }
262
263
    /**
264
     * Upload material.
265
     *
266
     * @param string $type
267
     * @param string $path
268
     * @param array  $form
269
     *
270
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
271
     *
272
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
273
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
274
     * @throws \GuzzleHttp\Exception\GuzzleException
275
     */
276 1
    public function upload(string $type, string $path, array $form = [])
277
    {
278 1
        if (!file_exists($path) || !is_readable($path)) {
279 1
            throw new InvalidArgumentException(sprintf('File does not exist, or the file is unreadable: "%s"', $path));
280
        }
281
282 1
        $form['type'] = $type;
283
284 1
        return $this->httpUpload($this->getApiByType($type), ['media' => $path], $form);
285
    }
286
287
    /**
288
     * Get API by type.
289
     *
290
     * @param string $type
291
     *
292
     * @return string
293
     */
294 1
    public function getApiByType(string $type)
295
    {
296 1
        switch ($type) {
297 1
            case 'news_image':
298 1
                return 'cgi-bin/media/uploadimg';
299
            default:
300 1
                return 'cgi-bin/material/add_material';
301
        }
302
    }
303
}
304