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

Notice::formatData()   C

Complexity

Conditions 8
Paths 7

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 31
ccs 23
cts 23
cp 1
rs 5.3846
cc 8
eloc 22
nc 7
nop 1
crap 8
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
62
    /**
63
     * Notice constructor.
64
     *
65
     * @param \EasyWeChat\Core\AccessToken $accessToken
66
     */
67 4
    public function __construct(AccessToken $accessToken)
68
    {
69 4
        parent::__construct($accessToken);
70
71 4
        $this->messageBackup = $this->message;
72 4
    }
73
74
    /**
75
     * Set industry.
76
     *
77
     * @param int $industryOne
78
     * @param int $industryTwo
79
     *
80
     * @return bool
81
     */
82 1
    public function setIndustry($industryOne, $industryTwo)
83
    {
84
        $params = [
85 1
                   'industry_id1' => $industryOne,
86 1
                   'industry_id2' => $industryTwo,
87 1
                  ];
88
89 1
        return $this->parseJSON('json', [self::API_SET_INDUSTRY, $params]);
90
    }
91
92
    /**
93
     * Add a template and get template ID.
94
     *
95
     * @param string $shortId
96
     *
97
     * @return string
98
     */
99 1
    public function addTemplate($shortId)
100
    {
101 1
        $params = ['template_id_short' => $shortId];
102
103 1
        return $this->parseJSON('json', [self::API_ADD_TEMPLATE, $params]);
104
    }
105
106
    /**
107
     * Send a notice message.
108
     *
109
     * @param $data
110
     *
111
     * @return mixed
112
     *
113
     * @throws \EasyWeChat\Core\Exceptions\InvalidArgumentException
114
     */
115 2
    public function send($data = [])
116
    {
117 2
        $params = array_merge([
118 2
                   'touser' => '',
119 2
                   'template_id' => '',
120 2
                   'url' => '',
121 2
                   'topcolor' => '',
122 2
                   'data' => [],
123 2
                  ], $data);
124
125 2
        $required = ['touser', 'template_id'];
126
127 2
        foreach ($params as $key => $value) {
128 2
            if (in_array($key, $required, true) && empty($value) && empty($this->message[$key])) {
129 1
                throw new InvalidArgumentException("Attibute '$key' can not be empty!");
130
            }
131
132 2
            $params[$key] = empty($value) ? $this->message[$key] : $value;
133 2
        }
134
135 2
        $params['data'] = $this->formatData($params['data']);
136
137 2
        $this->message = $this->messageBackup;
138
139 2
        return $this->parseJSON('json', [self::API_SEND_NOTICE, $params]);
140
    }
141
142
    /**
143
     * Magic access..
144
     *
145
     * @param string $method
146
     * @param array  $args
147
     *
148
     * @return Notice
149
     */
150 2
    public function __call($method, $args)
151
    {
152
        $map = [
153 2
                'template' => 'template_id',
154 2
                'templateId' => 'template_id',
155 2
                'uses' => 'template_id',
156 2
                'to' => 'touser',
157 2
                'receiver' => 'touser',
158 2
                'color' => 'topcolor',
159 2
                'topColor' => 'topcolor',
160 2
                'url' => 'url',
161 2
                'link' => 'url',
162 2
                'data' => 'data',
163 2
                'with' => 'data',
164 2
               ];
165
166 2
        if (0 === stripos($method, 'with')) {
167 1
            $method = lcfirst(substr($method, 4));
168 1
        }
169
170 2
        if (0 === stripos($method, 'and')) {
171 1
            $method = lcfirst(substr($method, 3));
172 1
        }
173
174 2
        if (isset($map[$method])) {
175 2
            $this->message[$map[$method]] = array_shift($args);
176 2
        }
177
178 2
        return $this;
179
    }
180
181
    /**
182
     * Format template data.
183
     *
184
     * @param array $data
185
     *
186
     * @return array
187
     */
188 2
    protected function formatData($data)
189
    {
190 2
        $return = [];
191
192 2
        foreach ($data as $key => $item) {
193 1
            if (is_scalar($item)) {
194 1
                $value = $item;
195 1
                $color = $this->defaultColor;
196 1
            } elseif (is_array($item) && !empty($item)) {
197 1
                if (isset($item['value'])) {
198 1
                    $value = strval($item['value']);
199 1
                    $color = empty($item['color']) ? $this->defaultColor : strval($item['color']);
200 1
                } elseif (count($item) < 2) {
201 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...
202 1
                    $color = $this->defaultColor;
203 1
                } else {
204 1
                    list($value, $color) = $item;
205
                }
206 1
            } else {
207 1
                $value = 'error data item.';
208 1
                $color = $this->defaultColor;
209
            }
210
211 1
            $return[$key] = [
212 1
                'value' => $value,
213 1
                'color' => $color,
214
            ];
215 2
        }
216
217 2
        return $return;
218
    }
219
}
220