Client::batchDelete()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Work\User;
13
14
use EasyWeChat\Kernel\BaseClient;
15
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
16
17
/**
18
 * Class Client.
19
 *
20
 * @author mingyoung <[email protected]>
21
 */
22
class Client extends BaseClient
23
{
24
    /**
25
     * Create a user.
26
     *
27
     * @param array $data
28
     *
29
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
30
     *
31
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
32
     * @throws \GuzzleHttp\Exception\GuzzleException
33
     */
34 1
    public function create(array $data)
35
    {
36 1
        return $this->httpPostJson('cgi-bin/user/create', $data);
37
    }
38
39
    /**
40
     * Update an exist user.
41
     *
42
     * @param string $id
43
     * @param array  $data
44
     *
45
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
46
     *
47
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
48
     * @throws \GuzzleHttp\Exception\GuzzleException
49
     */
50 1
    public function update(string $id, array $data)
51
    {
52 1
        return $this->httpPostJson('cgi-bin/user/update', array_merge(['userid' => $id], $data));
53
    }
54
55
    /**
56
     * Delete a user.
57
     *
58
     * @param string|array $userId
59
     *
60
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
61
     *
62
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
63
     * @throws \GuzzleHttp\Exception\GuzzleException
64
     */
65 1
    public function delete($userId)
66
    {
67 1
        if (is_array($userId)) {
68 1
            return $this->batchDelete($userId);
69
        }
70
71 1
        return $this->httpGet('cgi-bin/user/delete', ['userid' => $userId]);
72
    }
73
74
    /**
75
     * Batch delete users.
76
     *
77
     * @param array $userIds
78
     *
79
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
80
     *
81
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
82
     * @throws \GuzzleHttp\Exception\GuzzleException
83
     */
84 1
    public function batchDelete(array $userIds)
85
    {
86 1
        return $this->httpPostJson('cgi-bin/user/batchdelete', ['useridlist' => $userIds]);
87
    }
88
89
    /**
90
     * Get user.
91
     *
92
     * @param string $userId
93
     *
94
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
95
     *
96
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
97
     */
98 1
    public function get(string $userId)
99
    {
100 1
        return $this->httpGet('cgi-bin/user/get', ['userid' => $userId]);
101
    }
102
103
    /**
104
     * Get simple user list.
105
     *
106
     * @param int  $departmentId
107
     * @param bool $fetchChild
108
     *
109
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
110
     *
111
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
112
     */
113 1
    public function getDepartmentUsers(int $departmentId, bool $fetchChild = false)
114
    {
115
        $params = [
116 1
            'department_id' => $departmentId,
117 1
            'fetch_child' => (int) $fetchChild,
118
        ];
119
120 1
        return $this->httpGet('cgi-bin/user/simplelist', $params);
121
    }
122
123
    /**
124
     * Get user list.
125
     *
126
     * @param int  $departmentId
127
     * @param bool $fetchChild
128
     *
129
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
130
     *
131
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
132
     */
133 1
    public function getDetailedDepartmentUsers(int $departmentId, bool $fetchChild = false)
134
    {
135
        $params = [
136 1
            'department_id' => $departmentId,
137 1
            'fetch_child' => (int) $fetchChild,
138
        ];
139
140 1
        return $this->httpGet('cgi-bin/user/list', $params);
141
    }
142
143
    /**
144
     * Convert userId to openid.
145
     *
146
     * @param string   $userId
147
     * @param int|null $agentId
148
     *
149
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
150
     *
151
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
152
     * @throws \GuzzleHttp\Exception\GuzzleException
153
     */
154 1
    public function userIdToOpenid(string $userId, int $agentId = null)
155
    {
156
        $params = [
157 1
            'userid' => $userId,
158 1
            'agentid' => $agentId,
159
        ];
160
161 1
        return $this->httpPostJson('cgi-bin/user/convert_to_openid', $params);
162
    }
163
164
    /**
165
     * Convert openid to userId.
166
     *
167
     * @param string $openid
168
     *
169
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
170
     *
171
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
172
     * @throws \GuzzleHttp\Exception\GuzzleException
173
     */
174 1
    public function openidToUserId(string $openid)
175
    {
176
        $params = [
177 1
            'openid' => $openid,
178
        ];
179
180 1
        return $this->httpPostJson('cgi-bin/user/convert_to_userid', $params);
181
    }
182
183
    /**
184
     * Convert mobile to userId.
185
     *
186
     * @param string $mobile
187
     *
188
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
189
     *
190
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
191
     * @throws \GuzzleHttp\Exception\GuzzleException
192
     */
193 1
    public function mobileToUserId(string $mobile)
194
    {
195
        $params = [
196 1
            'mobile' => $mobile,
197
        ];
198
199 1
        return $this->httpPostJson('cgi-bin/user/getuserid', $params);
200
    }
201
202
    /**
203
     * @param string $userId
204
     *
205
     * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
206
     *
207
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
208
     */
209 1
    public function accept(string $userId)
210
    {
211
        $params = [
212 1
            'userid' => $userId,
213
        ];
214
215 1
        return $this->httpGet('cgi-bin/user/authsucc', $params);
216
    }
217
218
    /**
219
     * Batch invite users.
220
     *
221
     * @param array $params
222
     *
223
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
224
     *
225
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
226
     * @throws \GuzzleHttp\Exception\GuzzleException
227
     */
228 1
    public function invite(array $params)
229
    {
230 1
        return $this->httpPostJson('cgi-bin/batch/invite', $params);
231
    }
232
233
    /**
234
     * Get invitation QR code.
235
     *
236
     * @param int $sizeType
237
     *
238
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
239
     *
240
     * @throws InvalidArgumentException
241
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
242
     */
243 1
    public function getInvitationQrCode(int $sizeType = 1)
244
    {
245 1
        if (!\in_array($sizeType, [1, 2, 3, 4], true)) {
246 1
            throw new InvalidArgumentException('The sizeType must be 1, 2, 3, 4.');
247
        }
248
249 1
        return $this->httpGet('cgi-bin/corp/get_join_qrcode', ['size_type' => $sizeType]);
250
    }
251
}
252