Completed
Pull Request — master (#292)
by Carlos
03:42
created

Temporary::uploadImage()   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 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 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
/**
13
 * Temporary.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\Material;
22
23
use EasyWeChat\Core\AbstractAPI;
24
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
25
use EasyWeChat\Core\Exceptions\RuntimeException;
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
        $ext = File::getStreamExt($stream);
65
66 1
        file_put_contents($directory.'/'.$filename.'.'.$ext, $stream);
67
68 1
        return $filename.'.'.$ext;
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
        return $response->getBody();
85
    }
86
87
    /**
88
     * Upload temporary material.
89
     *
90
     * @param string $type
91
     * @param string $path
92
     *
93
     * @return string
94
     *
95
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
96
     */
97 2
    public function upload($type, $path)
98
    {
99 2
        if (!file_exists($path) || !is_readable($path)) {
100 1
            throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
101
        }
102
103 2
        if (!in_array($type, $this->allowTypes, true)) {
104 1
            throw new InvalidArgumentException("Unsupported media type: '{$type}'");
105
        }
106
107 1
        return $this->parseJSON('upload', [self::API_UPLOAD, ['media' => $path]]);
108
    }
109
110
    /**
111
     * Upload image.
112
     *
113
     * @param $path
114
     *
115
     * @return string
116
     *
117
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
118
     */
119 1
    public function uploadImage($path)
120
    {
121 1
        return $this->upload('image', $path);
122
    }
123
124
    /**
125
     * Upload video.
126
     *
127
     * @param $path
128
     *
129
     * @return string
130
     *
131
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
132
     */
133 1
    public function uploadVideo($path)
134
    {
135 1
        return $this->upload('video', $path);
136
    }
137
138
    /**
139
     * Upload voice.
140
     *
141
     * @param $path
142
     *
143
     * @return string
144
     *
145
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
146
     */
147 1
    public function uploadVoice($path)
148
    {
149 1
        return $this->upload('voice', $path);
150
    }
151
152
    /**
153
     * Upload thumb.
154
     *
155
     * @param $path
156
     *
157
     * @return string
158
     *
159
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
160
     */
161 1
    public function uploadThumb($path)
162
    {
163 1
        return $this->upload('thumb', $path);
164
    }
165
}
166