Completed
Push — master ( fe4c20...8b4ace )
by Carlos
03:18
created

Notice   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 227
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 91.46%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 227
ccs 75
cts 82
cp 0.9146
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setIndustry() 0 9 1
A addTemplate() 0 6 1
A getPrivateTemplates() 0 4 1
A deletePrivateTemplate() 0 6 1
B send() 0 26 6
B __call() 0 30 4
C formatData() 0 31 8
A getIndustry() 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
 * Notice.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\Notice;
22
23
use EasyWeChat\Core\AbstractAPI;
24
use EasyWeChat\Core\AccessToken;
25
use EasyWeChat\Core\Exceptions\InvalidArgumentException;
26
27
/**
28
 * Class Notice.
29
 */
30
class Notice extends AbstractAPI
31
{
32
    /**
33
     * Default color.
34
     *
35
     * @var string
36
     */
37
    protected $defaultColor = '#173177';
38
39
    /**
40
     * Attributes.
41
     *
42
     * @var array
43
     */
44
    protected $message = [
45
                          'touser' => '',
46
                          'template_id' => '',
47
                          'url' => '',
48
                          'topcolor' => '#FF0000',
49
                          'data' => [],
50
                         ];
51
    /**
52
     * Message backup.
53
     *
54
     * @var array
55
     */
56
    protected $messageBackup;
57
58
    const API_SEND_NOTICE = 'https://api.weixin.qq.com/cgi-bin/message/template/send';
59
    const API_SET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry';
60
    const API_ADD_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template';
61
    const API_GET_INDUSTRY = 'https://api.weixin.qq.com/cgi-bin/template/get_industry';
62
    const API_GET_ALL_PRIVATE_TEMPLATE = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template';
63
    const API_DEL_PRIVATE_TEMPLATE = 'https://api,weixin.qq.com/cgi-bin/template/del_private_template';
64
65
    /**
66
     * Notice constructor.
67
     *
68
     * @param \EasyWeChat\Core\AccessToken $accessToken
69
     */
70 4
    public function __construct(AccessToken $accessToken)
71
    {
72 4
        parent::__construct($accessToken);
73
74 4
        $this->messageBackup = $this->message;
75 4
    }
76
77
    /**
78
     * Set industry.
79
     *
80
     * @param int $industryOne
81
     * @param int $industryTwo
82
     *
83
     * @return bool
84
     */
85 1
    public function setIndustry($industryOne, $industryTwo)
86
    {
87
        $params = [
88 1
                   'industry_id1' => $industryOne,
89 1
                   'industry_id2' => $industryTwo,
90 1
                  ];
91
92 1
        return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]);
93
    }
94
95
    /**
96
     * Get industry.
97
     *
98
     * @return array
99
     */
100
    public function getIndustry()
101
    {
102
        return $this->parseJSON('json', [self::API_GET_INDUSTRY]);
103
    }
104
105
    /**
106
     * Add a template and get template ID.
107
     *
108
     * @param string $shortId
109
     *
110
     * @return string
111
     */
112 1
    public function addTemplate($shortId)
113
    {
114 1
        $params = ['template_id_short' => $shortId];
115
116 1
        return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]);
117
    }
118
119
    /**
120
     * Get private templates.
121
     *
122
     * @return array
123
     */
124
    public function getPrivateTemplates()
125
    {
126
        return $this->parseJSON('json', [self::API_GET_ALL_PRIVATE_TEMPLATE]);
127
    }
128
129
    /**
130
     * Delete private template.
131
     *
132
     * @param string $templateId
133
     *
134
     * @return array
135
     */
136
    public function deletePrivateTemplate($templateId)
137
    {
138
        $params = ['template_id' => $templateId];
139
140
        return $this->parseJSON('json', [self::API_DEL_PRIVATE_TEMPLATE, $params]);
141
    }
142
143
    /**
144
     * Send a notice message.
145
     *
146
     * @param $data
147
     *
148
     * @return mixed
149
     *
150
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
151
     */
152 2
    public function send($data = [])
153
    {
154 2
        $params = array_merge([
155 2
                   'touser' => '',
156 2
                   'template_id' => '',
157 2
                   'url' => '',
158 2
                   'topcolor' => '',
159 2
                   'data' => [],
160 2
                  ], $data);
161
162 2
        $required = ['touser', 'template_id'];
163
164 2
        foreach ($params as $key => $value) {
165 2
            if (in_array($key, $required, true) && empty($value) && empty($this->message[$key])) {
166 1
                throw new InvalidArgumentException("Attribute '$key' can not be empty!");
167
            }
168
169 2
            $params[$key] = empty($value) ? $this->message[$key] : $value;
170 2
        }
171
172 2
        $params['data'] = $this->formatData($params['data']);
173
174 2
        $this->message = $this->messageBackup;
175
176 2
        return $this->parseJSON('json', [self::API_SEND_NOTICE, $params]);
177
    }
178
179
    /**
180
     * Magic access..
181
     *
182
     * @param string $method
183
     * @param array  $args
184
     *
185
     * @return Notice
186
     */
187 2
    public function __call($method, $args)
188
    {
189
        $map = [
190 2
                'template' => 'template_id',
191 2
                'templateId' => 'template_id',
192 2
                'uses' => 'template_id',
193 2
                'to' => 'touser',
194 2
                'receiver' => 'touser',
195 2
                'color' => 'topcolor',
196 2
                'topColor' => 'topcolor',
197 2
                'url' => 'url',
198 2
                'link' => 'url',
199 2
                'data' => 'data',
200 2
                'with' => 'data',
201 2
               ];
202
203 2
        if (0 === stripos($method, 'with')) {
204 1
            $method = lcfirst(substr($method, 4));
205 1
        }
206
207 2
        if (0 === stripos($method, 'and')) {
208 1
            $method = lcfirst(substr($method, 3));
209 1
        }
210
211 2
        if (isset($map[$method])) {
212 2
            $this->message[$map[$method]] = array_shift($args);
213 2
        }
214
215 2
        return $this;
216
    }
217
218
    /**
219
     * Format template data.
220
     *
221
     * @param array $data
222
     *
223
     * @return array
224
     */
225 2
    protected function formatData($data)
226
    {
227 2
        $return = [];
228
229 2
        foreach ($data as $key => $item) {
230 1
            if (is_scalar($item)) {
231 1
                $value = $item;
232 1
                $color = $this->defaultColor;
233 1
            } elseif (is_array($item) && !empty($item)) {
234 1
                if (isset($item['value'])) {
235 1
                    $value = strval($item['value']);
236 1
                    $color = empty($item['color']) ? $this->defaultColor : strval($item['color']);
237 1
                } elseif (count($item) < 2) {
238 1
                    $value = array_shift($item);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $value is correct as array_shift($item) (which targets array_shift()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
239 1
                    $color = $this->defaultColor;
240 1
                } else {
241 1
                    list($value, $color) = $item;
242
                }
243 1
            } else {
244 1
                $value = 'error data item.';
245 1
                $color = $this->defaultColor;
246
            }
247
248 1
            $return[$key] = [
249 1
                'value' => $value,
250 1
                'color' => $color,
251
            ];
252 2
        }
253
254 2
        return $return;
255
    }
256
}
257