Test Failed
Push — master ( a4259a...64458f )
by Carlos
04:02
created

Client::send()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 7
rs 10
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\SubscribeMessage;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
use ReflectionClass;
17
18
/**
19
 * Class Client.
20
 *
21
 * @author hugo <[email protected]>
22
 */
23
class Client extends BaseClient
24
{
25
    /**
26
     * {@inheritdoc}.
27
     */
28
    protected $message = [
29
        'touser' => '',
30
        'template_id' => '',
31
        'page' => '',
32
        'data' => [],
33
    ];
34
35
    /**
36
     * {@inheritdoc}.
37
     */
38
    protected $required = ['touser', 'template_id', 'data'];
39
40
    /**
41
     * Send a template message.
42
     *
43
     * @param array $data
44
     *
45
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
46
     *
47
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
48
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
49
     * @throws \GuzzleHttp\Exception\GuzzleException
50
     */
51
    public function send(array $data = [])
52
    {
53
        $params = $this->formatMessage($data);
54
55
        $this->restoreMessage();
56
57
        return $this->httpPostJson('cgi-bin/message/subscribe/send', $params);
58
    }
59
60
    /**
61
     * @param array $data
62
     *
63
     * @return array
64
     *
65
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
66
     */
67
    protected function formatMessage(array $data = [])
68
    {
69
        $params = array_merge($this->message, $data);
70
71
        foreach ($params as $key => $value) {
72
            if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) {
73
                throw new InvalidArgumentException(sprintf('Attribute "%s" can not be empty!', $key));
74
            }
75
76
            $params[$key] = empty($value) ? $this->message[$key] : $value;
77
        }
78
79
        foreach ($params['data'] as $key => $value) {
80
            if (is_array($value)) {
81
                if (isset($value['value'])) {
82
                    $params['data'][$key] = ['value' => $value['value']];
83
84
                    continue;
85
                }
86
87
                if (count($value) >= 1) {
88
                    $value = [
89
                        'value' => $value[0],
90
//                        'color' => $value[1],// color unsupported
91
                    ];
92
                }
93
            } else {
94
                $value = [
95
                    'value' => strval($value),
96
                ];
97
            }
98
99
            $params['data'][$key] = $value;
100
        }
101
102
        return $params;
103
    }
104
105
    /**
106
     * Restore message.
107
     */
108
    protected function restoreMessage()
109
    {
110
        $this->message = (new ReflectionClass(static::class))->getDefaultProperties()['message'];
111
    }
112
}
113