Completed
Push — master ( 063142...88b393 )
by Carlos
03:03
created

Temporary::uploadVoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
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
/**
13
 * Temporary.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @see      https://github.com/overtrue
19
 * @see      http://overtrue.me
20
 */
21
22
namespace EasyWeChat\Material;
23
24
use EasyWeChat\Core\AbstractAPI;
25
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
26
use EasyWeChat\Support\File;
27
28
/**
29
 * Class Temporary.
30
 */
31
class Temporary extends AbstractAPI
32
{
33
    /**
34
     * Allow media type.
35
     *
36
     * @var array
37
     */
38
    protected $allowTypes = ['image', 'voice', 'video', 'thumb'];
39
40
    const API_GET = 'https://api.weixin.qq.com/cgi-bin/media/get';
41
    const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/upload';
42
43
    /**
44
     * Download temporary material.
45
     *
46
     * @param string $mediaId
47
     * @param string $directory
48
     * @param string $filename
49
     *
50
     * @return string
51
     *
52
     * @throws InvalidArgumentException
53
     */
54 1
    public function download($mediaId, $directory, $filename = '')
55
    {
56 1
        if (!is_dir($directory) || !is_writable($directory)) {
57 1
            throw new InvalidArgumentException("Directory does not exist or is not writable: '$directory'.");
58
        }
59
60 1
        $filename = $filename ?: $mediaId;
61
62 1
        $stream = $this->getStream($mediaId);
63
64 1
        $filename .= File::getStreamExt($stream);
65
66 1
        file_put_contents($directory.'/'.$filename, $stream);
67
68 1
        return $filename;
69
    }
70
71
    /**
72
     * Fetch item from WeChat server.
73
     *
74
     * @param string $mediaId
75
     *
76
     * @return mixed
77
     *
78
     * @throws \EasyWeChat\Core\Exceptions\RuntimeException
79
     */
80
    public function getStream($mediaId)
81
    {
82
        $response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]);
83
84
        $response->getBody()->rewind();
85
86
        $body = $response->getBody()->getContents();
87
88
        $json = json_decode($body, true);
89
90
        if (JSON_ERROR_NONE === json_last_error()) {
91
            $this->checkAndThrow($json);
92
        }
93
94
        return $body;
95
    }
96
97
    /**
98
     * Upload temporary material.
99
     *
100
     * @param string $type
101
     * @param string $path
102
     *
103
     * @return \EasyWeChat\Support\Collection
104
     *
105
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
106
     */
107 2
    public function upload($type, $path)
108
    {
109 2
        if (!file_exists($path) || !is_readable($path)) {
110 1
            throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
111
        }
112
113 2
        if (!in_array($type, $this->allowTypes, true)) {
114 1
            throw new InvalidArgumentException("Unsupported media type: '{$type}'");
115
        }
116
117 1
        return $this->parseJSON('upload', [self::API_UPLOAD, ['media' => $path], [], ['type' => $type]]);
118
    }
119
120
    /**
121
     * Upload image.
122
     *
123
     * @param $path
124
     *
125
     * @return \EasyWeChat\Support\Collection
126
     *
127
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
128
     */
129 1
    public function uploadImage($path)
130
    {
131 1
        return $this->upload('image', $path);
132
    }
133
134
    /**
135
     * Upload video.
136
     *
137
     * @param $path
138
     *
139
     * @return \EasyWeChat\Support\Collection
140
     *
141
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
142
     */
143 1
    public function uploadVideo($path)
144
    {
145 1
        return $this->upload('video', $path);
146
    }
147
148
    /**
149
     * Upload voice.
150
     *
151
     * @param $path
152
     *
153
     * @return \EasyWeChat\Support\Collection
154
     *
155
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
156
     */
157 1
    public function uploadVoice($path)
158
    {
159 1
        return $this->upload('voice', $path);
160
    }
161
162
    /**
163
     * Upload thumb.
164
     *
165
     * @param $path
166
     *
167
     * @return \EasyWeChat\Support\Collection
168
     *
169
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
170
     */
171 1
    public function uploadThumb($path)
172
    {
173 1
        return $this->upload('thumb', $path);
174
    }
175
}
176