Completed
Pull Request — master (#1397)
by
unknown
02:50
created

Client::createActivityId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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