Completed
Push — master ( 5ddcc0...9e0ead )
by Carlos
42s
created

Temporary   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 88%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
c 2
b 0
f 0
lcom 1
cbo 5
dl 0
loc 135
ccs 22
cts 25
cp 0.88
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A download() 0 16 4
A getStream() 0 6 1
A upload() 0 12 4
A uploadImage() 0 4 1
A uploadVideo() 0 4 1
A uploadVoice() 0 4 1
A uploadThumb() 0 4 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\Support\File;
26
27
/**
28
 * Class Temporary.
29
 */
30
class Temporary extends AbstractAPI
31
{
32
    /**
33
     * Allow media type.
34
     *
35
     * @var array
36
     */
37
    protected $allowTypes = ['image', 'voice', 'video', 'thumb'];
38
39
    const API_GET = 'https://api.weixin.qq.com/cgi-bin/media/get';
40
    const API_UPLOAD = 'https://api.weixin.qq.com/cgi-bin/media/upload';
41
42
    /**
43
     * Download temporary material.
44
     *
45
     * @param string $mediaId
46
     * @param string $directory
47
     * @param string $filename
48
     *
49
     * @return string
50
     *
51
     * @throws InvalidArgumentException
52
     */
53 1
    public function download($mediaId, $directory, $filename = '')
54
    {
55 1
        if (!is_dir($directory) || !is_writable($directory)) {
56 1
            throw new InvalidArgumentException("Directory does not exist or is not writable: '$directory'.");
57
        }
58
59 1
        $filename = $filename ?: $mediaId;
60
61 1
        $stream = $this->getStream($mediaId);
62
63 1
        $ext = File::getStreamExt($stream);
64
65 1
        file_put_contents($directory.'/'.$filename.'.'.$ext, $stream);
66
67 1
        return $filename.'.'.$ext;
68
    }
69
70
    /**
71
     * Fetch item from WeChat server.
72
     *
73
     * @param string $mediaId
74
     *
75
     * @return mixed
76
     *
77
     * @throws \EasyWeChat\Core\Exceptions\RuntimeException
78
     */
79
    public function getStream($mediaId)
80
    {
81
        $response = $this->getHttp()->get(self::API_GET, ['media_id' => $mediaId]);
82
83
        return $response->getBody();
84
    }
85
86
    /**
87
     * Upload temporary material.
88
     *
89
     * @param string $type
90
     * @param string $path
91
     *
92
     * @return string
93
     *
94
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
95
     */
96 2
    public function upload($type, $path)
97
    {
98 2
        if (!file_exists($path) || !is_readable($path)) {
99 1
            throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
100
        }
101
102 2
        if (!in_array($type, $this->allowTypes, true)) {
103 1
            throw new InvalidArgumentException("Unsupported media type: '{$type}'");
104
        }
105
106 1
        return $this->parseJSON('upload', [self::API_UPLOAD, ['media' => $path]]);
107
    }
108
109
    /**
110
     * Upload image.
111
     *
112
     * @param $path
113
     *
114
     * @return string
115
     *
116
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
117
     */
118 1
    public function uploadImage($path)
119
    {
120 1
        return $this->upload('image', $path);
121
    }
122
123
    /**
124
     * Upload video.
125
     *
126
     * @param $path
127
     *
128
     * @return string
129
     *
130
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
131
     */
132 1
    public function uploadVideo($path)
133
    {
134 1
        return $this->upload('video', $path);
135
    }
136
137
    /**
138
     * Upload voice.
139
     *
140
     * @param $path
141
     *
142
     * @return string
143
     *
144
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
145
     */
146 1
    public function uploadVoice($path)
147
    {
148 1
        return $this->upload('voice', $path);
149
    }
150
151
    /**
152
     * Upload thumb.
153
     *
154
     * @param $path
155
     *
156
     * @return string
157
     *
158
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
159
     */
160 1
    public function uploadThumb($path)
161
    {
162 1
        return $this->upload('thumb', $path);
163
    }
164
}
165