Passed
Pull Request — master (#1395)
by
unknown
03:06
created

Client   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 116
rs 10
c 0
b 0
f 0
wmc 12

4 Methods

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