MessageClient::formatMessage()   A
last analyzed

Complexity

Conditions 5
Paths 16

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 16
nop 1
dl 0
loc 21
ccs 11
cts 11
cp 1
crap 5
rs 9.6111
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\Work\ExternalContact;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
17
/**
18
 * Class MessageClient.
19
 *
20
 * @author milkmeowo <[email protected]>
21
 */
22
class MessageClient extends BaseClient
23
{
24
    /**
25
     * Required attributes.
26
     *
27
     * @var array
28
     */
29
    protected $required = ['content', 'media_id', 'title', 'url', 'pic_media_id', 'appid', 'page'];
30
31
    protected $textMessage = [
32
        'content' => '',
33
    ];
34
35
    protected $imageMessage = [
36
        'media_id' => '',
37
    ];
38
39
    protected $linkMessage = [
40
        'title' => '',
41
        'picurl' => '',
42
        'desc' => '',
43
        'url' => '',
44
    ];
45
46
    protected $miniprogramMessage = [
47
        'title' => '',
48
        'pic_media_id' => '',
49
        'appid' => '',
50
        'page' => '',
51
    ];
52
53
    /**
54
     * 添加企业群发消息模板
55
     *
56
     * @see https://work.weixin.qq.com/api/doc#90000/90135/91560
57
     *
58
     * @param array $msg
59
     *
60
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
61
     *
62
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
63
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
64
     * @throws \GuzzleHttp\Exception\GuzzleException
65
     */
66 5
    public function submit(array $msg)
67
    {
68 5
        $params = $this->formatMessage($msg);
69
70 2
        return $this->httpPostJson('cgi-bin/externalcontact/add_msg_template', $params);
71
    }
72
73
    /**
74
     * 获取企业群发消息发送结果.
75
     *
76
     * @see https://work.weixin.qq.com/api/doc#90000/90135/91561
77
     *
78
     * @param string $msgId
79
     *
80
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
81
     *
82
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
83
     * @throws \GuzzleHttp\Exception\GuzzleException
84
     */
85 1
    public function get(string $msgId)
86
    {
87 1
        return $this->httpPostJson('cgi-bin/externalcontact/get_group_msg_result', [
88 1
            'msgid' => $msgId,
89
        ]);
90
    }
91
92
    /**
93
     * 发送新客户欢迎语.
94
     *
95
     * @see https://work.weixin.qq.com/api/doc#90000/90135/91688
96
     *
97
     * @param string $welcomeCode
98
     * @param array  $msg
99
     *
100
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
101
     *
102
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
103
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
104
     * @throws \GuzzleHttp\Exception\GuzzleException
105
     */
106 1
    public function sendWelcome(string $welcomeCode, array $msg)
107
    {
108 1
        $formattedMsg = $this->formatMessage($msg);
109
110 1
        $params = array_merge($formattedMsg, [
111 1
            'welcome_code' => $welcomeCode,
112
        ]);
113
114 1
        return $this->httpPostJson('cgi-bin/externalcontact/send_welcome_msg', $params);
115
    }
116
117
    /**
118
     * @param array $data
119
     *
120
     * @return array
121
     *
122
     * @throws InvalidArgumentException
123
     */
124 6
    protected function formatMessage(array $data = [])
125
    {
126 6
        $params = $data;
127
128 6
        if (!empty($params['text'])) {
129 3
            $params['text'] = $this->formatFields($params['text'], $this->textMessage);
130
        }
131
132 5
        if (!empty($params['image'])) {
133 2
            $params['image'] = $this->formatFields($params['image'], $this->imageMessage);
134
        }
135
136 4
        if (!empty($params['link'])) {
137 2
            $params['link'] = $this->formatFields($params['link'], $this->linkMessage);
138
        }
139
140 3
        if (!empty($params['miniprogram'])) {
141 2
            $params['miniprogram'] = $this->formatFields($params['miniprogram'], $this->miniprogramMessage);
142
        }
143
144 3
        return $params;
145
    }
146
147
    /**
148
     * @param array $data
149
     * @param array $default
150
     *
151
     * @return array
152
     *
153
     * @throws InvalidArgumentException
154
     */
155 6
    protected function formatFields(array $data = [], array $default = [])
156
    {
157 6
        $params = array_merge($default, $data);
158 6
        foreach ($params as $key => $value) {
159 6
            if (in_array($key, $this->required, true) && empty($value) && empty($default[$key])) {
160 4
                throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
161
            }
162
163 4
            $params[$key] = empty($value) ? $default[$key] : $value;
164
        }
165
166 3
        return $params;
167
    }
168
}
169