Completed
Pull Request — master (#292)
by Carlos
03:42
created

Group::moveUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 1
eloc 5
nc 1
nop 2
crap 1
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
/**
13
 * Group.php.
14
 *
15
 * @author    overtrue <[email protected]>
16
 * @copyright 2015 overtrue <[email protected]>
17
 *
18
 * @link      https://github.com/overtrue
19
 * @link      http://overtrue.me
20
 */
21
namespace EasyWeChat\User;
22
23
use EasyWeChat\Core\AbstractAPI;
24
25
/**
26
 * Class Group.
27
 */
28
class Group extends AbstractAPI
29
{
30
    const API_GET = 'https://api.weixin.qq.com/cgi-bin/groups/get';
31
    const API_CREATE = 'https://api.weixin.qq.com/cgi-bin/groups/create';
32
    const API_UPDATE = 'https://api.weixin.qq.com/cgi-bin/groups/update';
33
    const API_DELETE = 'https://api.weixin.qq.com/cgi-bin/groups/delete';
34
    const API_USER_GROUP_ID = 'https://api.weixin.qq.com/cgi-bin/groups/getid';
35
    const API_MEMBER_UPDATE = 'https://api.weixin.qq.com/cgi-bin/groups/members/update';
36
    const API_MEMBER_BATCH_UPDATE = 'https://api.weixin.qq.com/cgi-bin/groups/members/batchupdate';
37
38
    /**
39
     * Create group.
40
     *
41
     * @param string $name
42
     *
43
     * @return int
44
     */
45 1 View Code Duplication
    public function create($name)
46
    {
47
        $params = [
48 1
                   'group' => ['name' => $name],
49 1
                  ];
50
51 1
        return $this->parseJSON('json', [self::API_CREATE, $params]);
52
    }
53
54
    /**
55
     * List all groups.
56
     *
57
     * @return array
58
     */
59 1
    public function lists()
60
    {
61 1
        return $this->parseJSON('get', [self::API_GET]);
62
    }
63
64
    /**
65
     * Update a group name.
66
     *
67
     * @param int    $groupId
68
     * @param string $name
69
     *
70
     * @return bool
71
     */
72 1 View Code Duplication
    public function update($groupId, $name)
73
    {
74
        $params = [
75
                   'group' => [
76 1
                               'id' => $groupId,
77 1
                               'name' => $name,
78 1
                              ],
79 1
                  ];
80
81 1
        return $this->parseJSON('json', [self::API_UPDATE, $params]);
82
    }
83
84
    /**
85
     * Delete group.
86
     *
87
     * @param int $groupId
88
     *
89
     * @return bool
90
     */
91 1 View Code Duplication
    public function delete($groupId)
92
    {
93
        $params = [
94 1
                   'group' => ['id' => $groupId],
95 1
                  ];
96
97 1
        return $this->parseJSON('json', [self::API_DELETE, $params]);
98
    }
99
100
    /**
101
     * Get user group.
102
     *
103
     * @param string $openId
104
     *
105
     * @return int
106
     */
107 1
    public function userGroup($openId)
108
    {
109 1
        $params = ['openid' => $openId];
110
111 1
        return $this->parseJSON('json', [self::API_USER_GROUP_ID, $params]);
112
    }
113
114
    /**
115
     * Move user to a group.
116
     *
117
     * @param string $openId
118
     * @param int    $groupId
119
     *
120
     * @return bool
121
     */
122 1
    public function moveUser($openId, $groupId)
123
    {
124
        $params = [
125 1
                   'openid' => $openId,
126 1
                   'to_groupid' => $groupId,
127 1
                  ];
128
129 1
        return $this->parseJSON('json', [self::API_MEMBER_UPDATE, $params]);
130
    }
131
132
    /**
133
     * Batch move users to a group.
134
     *
135
     * @param array $openIds
136
     * @param int   $groupId
137
     *
138
     * @return bool
139
     */
140 1
    public function moveUsers(array $openIds, $groupId)
141
    {
142
        $params = [
143 1
                   'openid_list' => $openIds,
144 1
                   'to_groupid' => $groupId,
145 1
                  ];
146
147 1
        return $this->parseJSON('json', [self::API_MEMBER_BATCH_UPDATE, $params]);
148
    }
149
}
150