Client::formatParameters()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5

Importance

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