Completed
Pull Request — master (#1413)
by milkmeowo
04:18
created

Client::formatWeappMessage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 7
ccs 4
cts 4
cp 1
crap 1
rs 10
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\MiniProgram\UniformMessage;
13
14
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
15
use EasyWeChat\OfficialAccount\TemplateMessage\Client as BaseClient;
16
17
class Client extends BaseClient
18
{
19
    const API_SEND = 'cgi-bin/message/wxopen/template/uniform_send';
20
21
    /**
22
     * {@inheritdoc}.
23
     *
24
     * @var array
25
     */
26
    protected $message = [
27
        'touser' => '',
28
    ];
29
30
    /**
31
     * Weapp Attributes.
32
     *
33
     * @var array
34
     */
35
    protected $weappMessage = [
36
        'template_id' => '',
37
        'page' => '',
38
        'form_id' => '',
39
        'data' => [],
40
        'emphasis_keyword' => '',
41
    ];
42
43
    /**
44
     * Official account attributes.
45
     *
46
     * @var array
47
     */
48
    protected $mpMessage = [
49
        'appid' => '',
50
        'template_id' => '',
51
        'url' => '',
52
        'miniprogram' => [],
53
        'data' => [],
54
    ];
55
56
    /**
57
     * Required attributes.
58
     *
59
     * @var array
60
     */
61
    protected $required = ['touser', 'template_id', 'form_id', 'miniprogram', 'appid'];
62
63
64
    /**
65
     * @param array $data
66
     * @return array
67
     * @throws InvalidArgumentException
68
     */
69 1
    protected function formatMessage(array $data = [])
70
    {
71 1
        $params = array_merge($this->message, $data);
72
73 1
        if (empty($params['touser'])) {
74 1
            throw new InvalidArgumentException(sprintf('Attribute "touser" can not be empty!'));
75
        }
76
77 1
        if (!empty($params['weapp_template_msg'])) {
78 1
            $params['weapp_template_msg'] = $this->formatWeappMessage($params['weapp_template_msg']);
79
        }
80
81 1
        if (!empty($params['mp_template_msg'])) {
82 1
            $params['mp_template_msg'] = $this->formatMpMessage($params['mp_template_msg']);
83
        }
84
85 1
        return $params;
86
    }
87
88
    /**
89
     * @param array $data
90
     *
91
     * @return array
92
     */
93 2
    protected function formatWeappMessage(array $data = [])
94
    {
95 2
        $params = $this->baseFormat($data, $this->weappMessage);
96
97 2
        $params['data'] = $this->formatData($params['data'] ?? []);
98
99 2
        return $params;
100
    }
101
102
    /**
103
     * @param array $data
104
     *
105
     * @return array
106
     */
107 3
    protected function formatMpMessage(array $data = [])
108
    {
109 3
        $params = $this->baseFormat($data, $this->mpMessage);
110
111 3
        if (empty($params['miniprogram']['appid'])) {
112 1
            $params['miniprogram']['appid'] = $this->app['config']['app_id'];
113
        }
114
115 3
        $params['data'] = $this->formatData($params['data'] ?? []);
116
117 3
        return $params;
118
    }
119
120
    /**
121
     * @param array $data
122
     * @param array $default
123
     *
124
     * @return array
125
     *
126
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
127
     */
128 4
    protected function baseFormat($data = [], $default = [])
129
    {
130 4
        $params = array_merge($default, $data);
131 4
        foreach ($params as $key => $value) {
132 4
            if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) {
133 2
                throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
134
            }
135
136 4
            $params[$key] = empty($value) ? $default[$key] : $value;
137
        }
138
139 4
        return $params;
140
    }
141
}
142