TemplateApi   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 44.71 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 10
c 0
b 0
f 0
lcom 1
cbo 1
dl 38
loc 85
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apiSetIndustry() 17 17 3
A apiAddTemplate() 16 16 2
B send() 5 21 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace Wechat\API;
3
4
/**
5
 * 模板相关接口.
6
 *
7
 * @author Tian.
8
 */
9
class TemplateApi extends BaseApi
10
{
11
    /**
12
     * 设置所属行业
13
     *
14
     * @param  int $industry_id1 [公众号模板消息所属行业编号]
15
     * @param  int $industry_id2 [公众号模板消息所属行业编号]
16
     *
17
     * @return bool|array
18
     */
19 View Code Duplication
    public function apiSetIndustry($industry_id1, $industry_id2)
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...
20
    {
21
        if (!is_numeric($industry_id1) || !is_numeric($industry_id2)) {
22
            $this->setError('参数错误');
23
24
            return false;
25
        }
26
27
        $queryStr = [
28
            'industry_id1' => $industry_id1,
29
            'industry_id2' => $industry_id2,
30
        ];
31
32
        $res = $this->_post('api_set_industry', $queryStr);
33
34
        return $res;
35
    }
36
37
    /**
38
     * 获得模板ID
39
     *
40
     * @param  String $template_id_short [公众号模板消息所属行业编号]
41
     *
42
     * @return bool|array
43
     */
44 View Code Duplication
    public function apiAddTemplate($template_id_short)
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...
45
    {
46
        if (!is_string($template_id_short)) {
47
            $this->setError('参数错误');
48
49
            return false;
50
        }
51
52
        $queryStr = [
53
            'template_id_short' => $template_id_short,
54
        ];
55
56
        $res = $this->_post('api_add_template', $queryStr);
57
58
        return $res;
59
    }
60
61
    /**
62
     * 发送模板消息
63
     *
64
     * @param  string $touser      [OPENID]
65
     * @param  string $template_id [模板ID]
66
     * @param  string $url         [跳转url]
67
     * @param  array  $data        [模板参数]
68
     *
69
     * @return int               [msgId]
70
     *
71
     */
72
    public function send($touser, $template_id, $url, $data = [])
73
    {
74 View Code Duplication
        if (!is_string($touser) || !is_string($template_id) || !is_array($data) || empty($data)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75
            $this->setError('参数错误');
76
77
            return false;
78
        }
79
80
        $queryStr = [
81
            'touser'      => $touser,
82
            'template_id' => $template_id,
83
            'url'         => $url,
84
            'data'        => $data,
85
        ];
86
87
        $this->module = 'message';
88
89
        $res = $this->_post('template/send', $queryStr);
90
91
        return $res;
92
    }
93
}
94