Client::formatMessage()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 1
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 6
rs 9.2222
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
namespace EasyWeChat\OfficialAccount\TemplateMessage;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use ReflectionClass;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author overtrue <[email protected]>
22
 */
23
class Client extends BaseClient
24
{
25
    public const API_SEND = 'cgi-bin/message/template/send';
26
27
    /**
28
     * Attributes.
29
     *
30
     * @var array
31
     */
32
    protected $message = [
33
        'touser' => '',
34
        'template_id' => '',
35
        'url' => '',
36
        'data' => [],
37
        'miniprogram' => '',
38
    ];
39
40
    /**
41
     * Required attributes.
42
     *
43
     * @var array
44
     */
45
    protected $required = ['touser', 'template_id'];
46
47
    /**
48
     * Set industry.
49
     *
50
     * @param int $industryOne
51
     * @param int $industryTwo
52
     *
53
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
54
     *
55
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
56
     * @throws \GuzzleHttp\Exception\GuzzleException
57
     */
58 1
    public function setIndustry($industryOne, $industryTwo)
59
    {
60
        $params = [
61 1
            'industry_id1' => $industryOne,
62 1
            'industry_id2' => $industryTwo,
63
        ];
64
65 1
        return $this->httpPostJson('cgi-bin/template/api_set_industry', $params);
66
    }
67
68
    /**
69
     * Get industry.
70
     *
71
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
72
     *
73
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
74
     * @throws \GuzzleHttp\Exception\GuzzleException
75
     */
76 1
    public function getIndustry()
77
    {
78 1
        return $this->httpPostJson('cgi-bin/template/get_industry');
79
    }
80
81
    /**
82
     * Add a template and get template ID.
83
     *
84
     * @param string $shortId
85
     *
86
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
87
     *
88
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
89
     * @throws \GuzzleHttp\Exception\GuzzleException
90
     */
91 1
    public function addTemplate($shortId)
92
    {
93 1
        $params = ['template_id_short' => $shortId];
94
95 1
        return $this->httpPostJson('cgi-bin/template/api_add_template', $params);
96
    }
97
98
    /**
99
     * Get private templates.
100
     *
101
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
102
     *
103
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
104
     * @throws \GuzzleHttp\Exception\GuzzleException
105
     */
106 1
    public function getPrivateTemplates()
107
    {
108 1
        return $this->httpPostJson('cgi-bin/template/get_all_private_template');
109
    }
110
111
    /**
112
     * Delete private template.
113
     *
114
     * @param string $templateId
115
     *
116
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
117
     *
118
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
119
     * @throws \GuzzleHttp\Exception\GuzzleException
120
     */
121 1
    public function deletePrivateTemplate($templateId)
122
    {
123 1
        $params = ['template_id' => $templateId];
124
125 1
        return $this->httpPostJson('cgi-bin/template/del_private_template', $params);
126
    }
127
128
    /**
129
     * Send a template message.
130
     *
131
     * @param array $data
132
     *
133
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
134
     *
135
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
136
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
137
     * @throws \GuzzleHttp\Exception\GuzzleException
138
     */
139 2
    public function send(array $data = [])
140
    {
141 2
        $params = $this->formatMessage($data);
142
143 2
        $this->restoreMessage();
144
145 2
        return $this->httpPostJson(static::API_SEND, $params);
146
    }
147
148
    /**
149
     * Send template-message for subscription.
150
     *
151
     * @param array $data
152
     *
153
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
154
     *
155
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
156
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
157
     * @throws \GuzzleHttp\Exception\GuzzleException
158
     */
159 1
    public function sendSubscription(array $data = [])
160
    {
161 1
        $params = $this->formatMessage($data);
162
163 1
        $this->restoreMessage();
164
165 1
        return $this->httpPostJson('cgi-bin/message/template/subscribe', $params);
166
    }
167
168
    /**
169
     * @param array $data
170
     *
171
     * @return array
172
     *
173
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
174
     */
175 3
    protected function formatMessage(array $data = [])
176
    {
177 3
        $params = array_merge($this->message, $data);
178
179 3
        foreach ($params as $key => $value) {
180 3
            if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
181 3
                throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
182
            }
183
184 3
            $params[$key] = empty($value) ? $this->message[$key] : $value;
185
        }
186
187 3
        $params['data'] = $this->formatData($params['data'] ?? []);
188
189 3
        return $params;
190
    }
191
192
    /**
193
     * @param array $data
194
     *
195
     * @return array
196
     */
197 8
    protected function formatData(array $data)
198
    {
199 8
        $formatted = [];
200
201 8
        foreach ($data as $key => $value) {
202 5
            if (is_array($value)) {
203 4
                if (\array_key_exists('value', $value)) {
204 4
                    $formatted[$key] = $value;
205
206 4
                    continue;
207
                }
208
209 4
                if (count($value) >= 2) {
210
                    $value = [
211 4
                        'value' => $value[0],
212 4
                        'color' => $value[1],
213
                    ];
214
                }
215
            } else {
216
                $value = [
217 5
                    'value' => strval($value),
218
                ];
219
            }
220
221 5
            $formatted[$key] = $value;
222
        }
223
224 8
        return $formatted;
225
    }
226
227
    /**
228
     * Restore message.
229
     */
230 3
    protected function restoreMessage()
231
    {
232 3
        $this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message'];
233 3
    }
234
}
235