Completed
Pull Request — master (#546)
by
unknown
05:32
created

Material::uploadImage()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 10
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
/**
13
 * Material.php.
14
 *
15
 * @author    allen05ren <[email protected]>
16
 * @copyright 2016 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\ShakeAround;
22
23
use EasyWeChat\Core\AbstractAPI;
24
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
25
26
/**
27
 * Class Material.
28
 */
29
class Material extends AbstractAPI
30
{
31
    const API_MATERIAL_ADD = 'https://api.weixin.qq.com/shakearound/material/add';
32
33
    /**
34
     * Upload image material.
35
     *
36
     * @param string $path
37
     * @param string $type
38
     *
39
     * @return string
40
     *
41
     * @throws InvalidArgumentException
42
     */
43
    public function uploadImage($path, $type = 'icon')
44
    {
45
        if (!file_exists($path) || !is_readable($path)) {
46
            throw new InvalidArgumentException("File does not exist, or the file is unreadable: '$path'");
47
        }
48
49
        $type = strtolower($type);
50
51
        return $this->parseJSON('upload', [self::API_MATERIAL_ADD, ['media' => $path], [], ['type' => $type]]);
52
    }
53
}
54