Completed
Push — master ( 2686ad...8343d9 )
by Carlos
01:41
created

Client::getJssdkMedia()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 14
loc 14
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\BasicService\Media;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use EasyWeChat\Kernel\Http\StreamResponse;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author overtrue <[email protected]>
22
 */
23
class Client extends BaseClient
24
{
25
    /**
26
     * @var string
27
     */
28
    protected $baseUri = 'https://api.weixin.qq.com/cgi-bin/';
29
30
    /**
31
     * Allow media type.
32
     *
33
     * @var array
34
     */
35
    protected $allowTypes = ['image', 'voice', 'video', 'thumb'];
36
37
    /**
38
     * Upload image.
39
     *
40
     * @param $path
41
     *
42
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
43
     *
44
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
45
     */
46
    public function uploadImage($path)
47
    {
48
        return $this->upload('image', $path);
49
    }
50
51
    /**
52
     * Upload video.
53
     *
54
     * @param $path
55
     *
56
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
57
     *
58
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
59
     */
60
    public function uploadVideo($path)
61
    {
62
        return $this->upload('video', $path);
63
    }
64
65
    /**
66
     * @param string $path
67
     *
68
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
69
     *
70
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
71
     */
72
    public function uploadVoice($path)
73
    {
74
        return $this->upload('voice', $path);
75
    }
76
77
    /**
78
     * @param $path
79
     *
80
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
81
     *
82
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
83
     */
84
    public function uploadThumb($path)
85
    {
86
        return $this->upload('thumb', $path);
87
    }
88
89
    /**
90
     * Upload temporary material.
91
     *
92
     * @param string $type
93
     * @param string $path
94
     *
95
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
96
     *
97
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
98
     */
99
    public function upload(string $type, string $path)
100
    {
101
        if (!file_exists($path) || !is_readable($path)) {
102
            throw new InvalidArgumentException(sprintf("File does not exist, or the file is unreadable: '%s'", $path));
103
        }
104
105
        if (!in_array($type, $this->allowTypes, true)) {
106
            throw new InvalidArgumentException(sprintf("Unsupported media type: '%s'", $type));
107
        }
108
109
        return $this->httpUpload('media/upload', ['media' => $path], ['type' => $type]);
110
    }
111
112
    /**
113
     * @param string $path
114
     * @param string $title
115
     * @param string $description
116
     *
117
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
118
     */
119
    public function uploadVideoForBroadcasting(string $path, string $title, string $description)
120
    {
121
        $response = $this->uploadVideo($path);
122
        $arrayResponse = $this->transformResponseToType($response, 'array');
123
124
        if (!empty($arrayResponse['media_id'])) {
125
            return $this->createVideoForBroadcasting($arrayResponse['media_id'], $title, $description);
126
        }
127
128
        return $response;
129
    }
130
131
    /**
132
     * @param string $mediaId
133
     * @param string $title
134
     * @param string $description
135
     *
136
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
137
     */
138
    public function createVideoForBroadcasting(string $mediaId, string $title, string $description)
139
    {
140
        return $this->httpPostJson('media/uploadvideo', [
141
            'media_id' => $mediaId,
142
            'title' => $title,
143
            'description' => $description,
144
        ]);
145
    }
146
147
    /**
148
     * Fetch item from WeChat server.
149
     *
150
     * @param string $mediaId
151
     *
152
     * @return \EasyWeChat\Kernel\Http\StreamResponse|\Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
153
     */
154 View Code Duplication
    public function get(string $mediaId)
155
    {
156
        $response = $this->requestRaw('media/get', 'GET', [
157
            'query' => [
158
                'media_id' => $mediaId,
159
            ],
160
        ]);
161
162
        if (false !== stripos($response->getHeaderLine('Content-Type'), 'text/plain')) {
163
            return $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));
164
        }
165
166
        return StreamResponse::buildFromPsrResponse($response);
167
    }
168
169
    /**
170
     * @param string $mediaId
171
     *
172
     * @return array|\EasyWeChat\Kernel\Http\Response|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
173
     */
174 View Code Duplication
    public function getJssdkMedia(string $mediaId)
175
    {
176
        $response = $this->requestRaw('media/get/jssdk', 'GET', [
177
            'query' => [
178
                'media_id' => $mediaId,
179
            ],
180
        ]);
181
182
        if (false !== stripos($response->getHeaderLine('Content-Type'), 'text/plain')) {
183
            return $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));
184
        }
185
186
        return StreamResponse::buildFromPsrResponse($response);
187
    }
188
}
189