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

Client::updateMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 3
dl 0
loc 15
ccs 8
cts 8
cp 1
crap 2
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\ActivityMessage;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
17
class Client extends BaseClient
18
{
19
    /**
20
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
21
     */
22 1
    public function createActivityId()
23
    {
24 1
        return $this->httpGet('cgi-bin/message/wxopen/activityid/create');
25
    }
26
27
    /**
28
     * @param string $activityId
29
     * @param int    $state
30
     * @param array  $params
31
     *
32
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 2
    public function updateMessage(string $activityId, int $state = 0, array $params = [])
37
    {
38 2
        if (!in_array($state, [0, 1], true)) {
39 1
            throw new InvalidArgumentException('"state" should be "0" or "1".');
40
        }
41
42 1
        $params = $this->formatParameters($params);
43
44
        $params = [
45 1
            'activity_id' => $activityId,
46 1
            'target_state' => $state,
47 1
            'template_info' => ['parameter_list' => $params],
48
        ];
49
50 1
        return $this->httpPostJson('cgi-bin/message/wxopen/updatablemsg/send', $params);
51
    }
52
53
    /**
54
     * @param array $params
55
     *
56
     * @return array
57
     */
58 1
    protected function formatParameters(array $params)
59
    {
60 1
        $formatted = [];
61
62 1
        foreach ($params as $name => $value) {
63 1
            if (!in_array($name, ['member_count', 'room_limit', 'path', 'version_type'], true)) {
64 1
                continue;
65
            }
66
67 1
            if ('version_type' === $name && !in_array($value, ['develop', 'trial', 'release'], true)) {
68 1
                throw new InvalidArgumentException('Invalid value of attribute "version_type".');
69
            }
70
71 1
            $formatted[] = [
72 1
                'name' => $name,
73 1
                'value' => strval($value),
74
            ];
75
        }
76
77 1
        return $formatted;
78
    }
79
}
80