MaterialApi::get()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 16
loc 16
rs 9.4285
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Wechat\API;
4
5
use Wechat\Api;
6
7
/**
8
 * 素材相关接口.
9
 *
10
 * @author Tian.
11
 */
12
class MaterialApi extends BaseApi
13
{
14
    /**
15
     * 获取素材列表.
16
     *
17
     * @param string $type 获取素材类型,
18
     * @param        $type $offset 偏移 ,
0 ignored issues
show
Documentation introduced by
The doc-type $type could not be parsed: Unknown type name "$type" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
19
     * @param        $type $count 数量  不能大于20
0 ignored issues
show
Documentation introduced by
The doc-type $type could not be parsed: Unknown type name "$type" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
20
     *
21
     * @return array. 注 返回值为图文(news)时 无法获得文章的正文
0 ignored issues
show
Documentation introduced by
The doc-type array. could not be parsed: Unknown type name "array." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
22
     */
23
    public function batchget($type = 'image', $offset = 0, $count = 20)
24
    {
25
        if (!is_string($type)) {
26
            $this->setError('参数错误,类型参数错误');
27
28
            return false;
29
        }
30
31
        $queryStr = [
32
            "type" => $type, 'offset' => $offset, 'count' => $count,
33
        ];
34
35
        $res = $this->_post('batchget_material', $queryStr);
36
37
        return $res;
38
    }
39
40
    /**
41
     *  上传图文素材.
42
     *
43
     * @param array $info 图文素材的信息, title 标题,thumb_media_id 封面图片ID,author 作者,digest 摘要,show_cover_pic 是否显示封面,content 内容支持html少于2万字符 小于1M,content_source_url 点击 阅读原文的url
44
     *
45
     * @return string.  成功返回mediaid
0 ignored issues
show
Documentation introduced by
The doc-type string. could not be parsed: Unknown type name "string." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
46
     */
47 View Code Duplication
    public function addNews($info)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
    {
49
        if (!is_array($info)) {
50
            $this->setError('参数错误');
51
52
            return false;
53
        }
54
55
        $data = ['articles' => $info];
56
57
        $res = $this->_post('add_news', $data);
58
59
        return $res;
60
    }
61
62
    /**
63
     *  新增永久素材.
64
     *
65
     * @param string $file 媒体文件路径
66
     * @param string $type 文件类型 (video,image,voice,thumb)
67
     *
68
     * @return array. 媒体文件ID 获取地址  https://mmbiz.qlogo.cn/mmbiz/NdxGKqW8jE9GbAqUEPdSgSvbUbProSmE8NbUFwIYnp0Duibs611ZsCLza6b2dS8Ex3CO5dtv0u1HP9QY32djCxA/0?wx_fmt=jpeg   视频素材 官方接口也无法上传
0 ignored issues
show
Documentation introduced by
The doc-type array. could not be parsed: Unknown type name "array." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
69
     */
70
    public function add($file, $type = "image", $info = [])
71
    {
72
        if (!$file || !$type) {
73
            $this->setError('参数缺失');
74
75
            return false;
76
        }
77
78
        if (!file_exists($file)) {
79
            $this->setError('文件路径不正确');
80
81
            return false;
82
        }
83
84
        if ($type == "video") {
85
            if (!is_array($info)) {
86
                $this->setError('视频信息必须是数组格式');
87
88
                return false;
89
            }
90
91
            if (empty($info['title']) || empty($info['introduction'])) {
92
                $this->setError('上传视频请填写标题介绍');
93
94
                return false;
95
            }
96
97
            $description['title']        = $info['title'];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$description was never initialized. Although not strictly required by PHP, it is generally a good practice to add $description = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
98
            $description['introduction'] = $info['introduction'];
99
            $des                         = json_encode($description);
100
            $data                        = ['media' => '@' . realpath($file), "type" => $type, "description" => $des];
101
        } else {
102
            $data = ['media' => '@' . realpath($file), "type" => $type];
103
        }
104
105
        $node = 'add_material';
106
        $res  = $this->_post($node, $data, false);
107
108
        return $res;
109
    }
110
111
    /**
112
     * 获取 永久素材.通常为图文素材 其他素材可以在GetMaterialList获取对应地址
113
     *
114
     * @param string $media_id 素材ID
115
     *
116
     * @return array.
0 ignored issues
show
Documentation introduced by
The doc-type array. could not be parsed: Unknown type name "array." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
117
     */
118 View Code Duplication
    public function get($media_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        if (!is_string($media_id)) {
121
            $this->setError('参数错误,媒体ID参数错误');
122
123
            return false;
124
        }
125
126
        $queryStr = [
127
            'media_id' => $media_id,
128
        ];
129
130
        $res = $this->_post('get_material', $queryStr);
131
132
        return $res;
133
    }
134
135
    /**
136
     *   删除永久素材.
137
     *
138
     * @param string $media_id 素材ID
139
     *
140
     * @return bool.  成功返回ok
0 ignored issues
show
Documentation introduced by
The doc-type bool. could not be parsed: Unknown type name "bool." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
141
     */
142 View Code Duplication
    public function del($media_id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
143
    {
144
        if (!is_string($media_id)) {
145
            $this->setError('参数错误,媒体ID参数错误');
146
147
            return false;
148
        }
149
150
        $queryStr = [
151
            "media_id" => $media_id,
152
        ];
153
154
        $res = $this->_post('del_material', $queryStr);
155
156
        return $res;
157
    }
158
159
    /**
160
     *   修改永久素材.
161
     *
162
     * @param string $media_id 素材ID,$info 保存信息,$index 多图文素材时才有意义 更新位置
163
     *
164
     * @return bool.  成功返回ok
0 ignored issues
show
Documentation introduced by
The doc-type bool. could not be parsed: Unknown type name "bool." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
165
     */
166
    public function updateNews($media_id, $articles, $index = 0)
167
    {
168
        if (!is_string($media_id)) {
169
            $this->setError('参数错误,媒体ID参数错误');
170
171
            return false;
172
        }
173
174
        $queryStr = [
175
            "media_id" => $media_id,
176
            "index"    => intval($index),
177
            "articles" => $articles,
178
        ];
179
180
        $res = $this->_post('update_news', $queryStr);
181
182
        return $res;
183
    }
184
185
    /**
186
     * 获取素材总数.
187
     *
188
     * @return array.
0 ignored issues
show
Documentation introduced by
The doc-type array. could not be parsed: Unknown type name "array." at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
189
     */
190
    public function getCount()
191
    {
192
        $queryStr = [];
193
194
        $res = $this->_get('get_materialcount', $queryStr);
195
196
        return $res;
197
    }
198
}
199