Byrobot::delete()   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 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: yiranzai
5
 * Date: 19-3-19
6
 * Time: 上午10:32
7
 */
8
9
namespace Yiranzai\Byrobot;
10
11
use GuzzleHttp\Client;
12
use GuzzleHttp\Exception\GuzzleException;
13
use Yiranzai\Byrobot\Traits\Base;
14
use Yiranzai\Byrobot\Traits\Byrobot as ByrobotTrait;
15
use Yiranzai\Tools\Date;
16
use Yiranzai\Tools\Tools;
17
18
/**
19
 * Class Byrobot
20
 * @package Yiranzai\Byrobot
21
 */
22
final class Byrobot
23
{
24
    use ByrobotTrait, Base;
25
    /**
26
     * @var array
27
     */
28
    private static $guarded = ['url', 'client', 'guarded'];
29
    /**
30
     * @var Client
31
     */
32
    private $client;
33
34
    /**
35
     * Byrobot constructor.
36
     * @param array $config
37
     */
38 3
    private function __construct(array $config = [])
39
    {
40
41 3
        foreach ($config as $key => $value) {
42 3
            if (in_array($key, self::$guarded, true)) {
43
                continue;
44
            }
45 3
            self::$$key = $value;
46
        }
47 3
    }
48
49
    /**
50
     * 获取绑定公司列表接口
51
     *
52
     * @param array $param
53
     * @param bool  $isArr
54
     * @return array|object|null
55
     * @throws \Exception
56
     */
57 6
    public function getCompanys($param = [], $isArr = false)
58
    {
59 6
        return $this->send(__FUNCTION__, $param, $isArr);
60
    }
61
62
    /**
63
     * @param       $str
64
     * @param array $param
65
     * @param bool  $isArr
66
     * @return array|object|null
67
     * @throws \Exception
68
     */
69 6
    private function send($str, $param = [], $isArr = false)
70
    {
71 6
        if (!$this->check()) {
72
            throw  new \RuntimeException('核心参数不足');
73
        }
74 6
        ['url' => $url, 'method' => $method, 'desc' => $desc] = $this->build($str);
75 6
        [$data, $msg] = $this->request($url, $method, $param, $isArr);
76 6
        if ($msg) {
77 6
            throw  new \RuntimeException($url . ' | ' . $desc . ' | ' . $msg);
78
        }
79
        return $data;
80
    }
81
82
    /**
83
     * @return bool
84
     */
85 6
    private function check(): bool
86
    {
87 6
        return self::$appKey !== null && self::$appSecret !== null;
88
    }
89
90
    /**
91
     * @param $str
92
     * @return array
93
     * @throws \Exception
94
     */
95 6
    protected function build($str): array
96
    {
97 6
        if (!isset(self::$url[$str])) {
98
            throw  new \RuntimeException($str . ' api not found');
99
        }
100 6
        $data        = self::$url[$str];
101 6
        $data['url'] = self::$baseUrl .
102 6
            str_replace('{apiVersion}', self::$apiVersion, $data['url']);
103 6
        return $data;
104
    }
105
106
    /**
107
     * @param string $url
108
     * @param string $method
109
     * @param array  $param
110
     * @param bool   $isArr
111
     * @return array
112
     */
113 6
    protected function request($url, $method, $param = [], $isArr = false): array
114
    {
115 6
        $msg = $json = null;
116
        try {
117 6
            $date  = Date::toCarbon()->toRfc7231String();
118 6
            $sign  = $this->generateSign($date);
119 6
            $query = [];
120
121 6
            $method = strtoupper($method);
122
123 6
            if ($method === 'GET') {
124 6
                $query['query'] = $param;
125
            }
126
127 6
            if ($method === 'POST') {
128 3
                $query['json'] = $param;
129
            }
130
131 6
            $query['headers'] = [
132 6
                'datetime' => $date,
133 6
                'appkey'   => self::$appKey,
134 6
                'sign'     => $sign
135
            ];
136 6
            $response         = $this->getClient()->request($method, $url, $query);
137
            //状态码不是200直接抛出异常
138 6
            if (200 !== $response->getStatusCode()) {
139
                throw new \RuntimeException('请求出错!'
140
                    . Tools::arrGet(self::$errorCode, $response->getStatusCode(), ''));
141
            }
142
143
            //返回的内容不是json,抛出异常
144 6
            $json = json_decode($response->getBody(), $isArr);
145 6
            if (JSON_ERROR_NONE !== json_last_error()) {
146
                $json = null;
147
                throw new \RuntimeException('请求结果异常!' . Tools::iteratorGet($json, 'resultMsg', ''));
148
            }
149
150 6
            if (200 !== $code = (int)Tools::iteratorGet($json, 'code')) {
151 6
                $json = null;
152 6
                throw new \RuntimeException('请求返回出错!' . Tools::arrGet(self::$errorCode, $code, ''));
153
            }
154
            $json = Tools::iteratorGet($json, 'data');
155 6
        } catch (\Throwable $e) {
156 6
            $msg = $e->getMessage();
157
        } catch (GuzzleException $e) {
158
            $msg = $e->getMessage();
159
        }
160 6
        return [$json, $msg];
161
    }
162
163
    /**
164
     * 加密
165
     * 规则:GMT时间 + "\n" + appKey
166
     *
167
     * @param String $date 时间:Thu, 13 Dec 2018 01:27:17 GMT
168
     * @return string
169
     */
170 6
    private function generateSign(string $date): string
171
    {
172 6
        if (function_exists('hash_hmac')) {
173 6
            return base64_encode(hash_hmac('sha1', self::$appKey . "\n" . $date, self::$appSecret, true));
174
        }
175
        return null;
176
    }
177
178
    /**
179
     * @return Client
180
     */
181 6
    protected function getClient(): Client
182
    {
183 6
        if (null === $this->client) {
184 3
            $this->client = new Client();
185
        }
186 6
        return $this->client;
187
    }
188
189
    /**
190
     * 获取公司的主叫电话列表接口
191
     *
192
     * @param array $param
193
     * @param bool  $isArr
194
     * @return array|object|null
195
     * @throws \Exception
196
     */
197 3
    public function getPhones($param = [], $isArr = false)
198
    {
199 3
        return $this->send(__FUNCTION__, $param, $isArr);
200
    }
201
202
    /**
203
     * 获取公司的机器人话术列表接口
204
     *
205
     * @param array $param
206
     * @param bool  $isArr
207
     * @return array|object|null
208
     * @throws \Exception
209
     */
210 3
    public function getRobots($param = [], $isArr = false)
211
    {
212 3
        return $this->send(__FUNCTION__, $param, $isArr);
213
    }
214
215
    /**
216
     * 添加单个黑名单到公司默认黑名单组接口
217
     *
218
     * @param array $param
219
     * @param bool  $isArr
220
     * @return array|object|null
221
     * @throws \Exception
222
     */
223 3
    public function addBlackList($param = [], $isArr = false)
224
    {
225 3
        return $this->send(__FUNCTION__, $param, $isArr);
226
    }
227
228
    /**
229
     * 获取公司AI坐席概况接口
230
     *
231
     * @param array $param
232
     * @param bool  $isArr
233
     * @return array|object|null
234
     * @throws \Exception
235
     */
236 3
    public function statistics($param = [], $isArr = false)
237
    {
238 3
        return $this->send(__FUNCTION__, $param, $isArr);
239
    }
240
241
    /**
242
     * 创建任务接口
243
     *
244
     * @param array $param
245
     * @param bool  $isArr
246
     * @return array|object|null
247
     * @throws \Exception
248
     */
249 3
    public function createTask($param = [], $isArr = false)
250
    {
251 3
        return $this->send(__FUNCTION__, $param, $isArr);
252
    }
253
254
    /**
255
     * 启动任务接口
256
     *
257
     * @param array $param
258
     * @param bool  $isArr
259
     * @return array|object|null
260
     * @throws \Exception
261
     */
262 3
    public function start($param = [], $isArr = false)
263
    {
264 3
        return $this->send(__FUNCTION__, $param, $isArr);
265
    }
266
267
    /**
268
     * 暂停任务接口
269
     *
270
     * @param array $param
271
     * @param bool  $isArr
272
     * @return array|object|null
273
     * @throws \Exception
274
     */
275 3
    public function pause($param = [], $isArr = false)
276
    {
277 3
        return $this->send(__FUNCTION__, $param, $isArr);
278
    }
279
280
    /**
281
     * 停止任务接口
282
     *
283
     * @param array $param
284
     * @param bool  $isArr
285
     * @return array|object|null
286
     * @throws \Exception
287
     */
288 3
    public function stop($param = [], $isArr = false)
289
    {
290 3
        return $this->send(__FUNCTION__, $param, $isArr);
291
    }
292
293
    /**
294
     * 删除任务
295
     *
296
     * @param array $param
297
     * @param bool  $isArr
298
     * @return array|object|null
299
     * @throws \Exception
300
     */
301 3
    public function delete($param = [], $isArr = false)
302
    {
303 3
        return $this->send(__FUNCTION__, $param, $isArr);
304
    }
305
306
    /**
307
     * 向任务中导入客户接口
308
     *
309
     * @param array $param
310
     * @param bool  $isArr
311
     * @return array|object|null
312
     * @throws \Exception
313
     */
314 3
    public function importTaskCustomer($param = [], $isArr = false)
315
    {
316 3
        return $this->send(__FUNCTION__, $param, $isArr);
317
    }
318
319
    /**
320
     * 修改任务
321
     *
322
     * @param array $param
323
     * @param bool  $isArr
324
     * @return array|object|null
325
     * @throws \Exception
326
     */
327 3
    public function update($param = [], $isArr = false)
328
    {
329 3
        return $this->send(__FUNCTION__, $param, $isArr);
330
    }
331
332
    /**
333
     * 单次电话外呼
334
     *
335
     * @param array $param
336
     * @param bool  $isArr
337
     * @return array|object|null
338
     * @throws \Exception
339
     */
340 3
    public function call($param = [], $isArr = false)
341
    {
342 3
        return $this->send(__FUNCTION__, $param, $isArr);
343
    }
344
345
    /**
346
     * 根据手机号进行单次电话外呼
347
     *
348
     * @param array $param
349
     * @param bool  $isArr
350
     * @return array|object|null
351
     * @throws \Exception
352
     */
353 3
    public function singleCallByMobile($param = [], $isArr = false)
354
    {
355 3
        return $this->send(__FUNCTION__, $param, $isArr);
356
    }
357
358
    /**
359
     * 获取任务列表接口
360
     *
361
     * @param array $param
362
     * @param bool  $isArr
363
     * @return array|object|null
364
     * @throws \Exception
365
     */
366 6
    public function getTasks($param = [], $isArr = false)
367
    {
368 6
        if (isset($param['pageSize']) && $param['pageSize'] > self::$maxTaskPageSize) {
369 3
            throw  new \RuntimeException('pageSize 不能大于' . self::$maxTaskPageSize);
370
        }
371 3
        return $this->send(__FUNCTION__, $param, $isArr);
372
    }
373
374
    /**
375
     * 获取任务详情接口
376
     *
377
     * @param array $param
378
     * @param bool  $isArr
379
     * @return array|object|null
380
     * @throws \Exception
381
     */
382 3
    public function getTaskDetail($param = [], $isArr = false)
383
    {
384 3
        return $this->send(__FUNCTION__, $param, $isArr);
385
    }
386
387
    /**
388
     * 获取已经完成任务电话号码接口
389
     *
390
     * @param array $param
391
     * @param bool  $isArr
392
     * @return array|object|null
393
     * @throws \Exception
394
     */
395 6
    public function queryDoneTaskPhones($param = [], $isArr = false)
396
    {
397 6
        if (isset($param['pageSize']) && $param['pageSize'] > self::$maxPhoneLogPageSize) {
398 3
            throw  new \RuntimeException('pageSize 不能大于' . self::$maxPhoneLogPageSize);
399
        }
400 3
        return $this->send(__FUNCTION__, $param, $isArr);
401
    }
402
403
    /**
404
     * 获取任务未开始的电话列表
405
     *
406
     * @param array $param
407
     * @param bool  $isArr
408
     * @return array|object|null
409
     * @throws \Exception
410
     */
411 3
    public function notDialedCustomerList($param = [], $isArr = false)
412
    {
413 3
        return $this->send(__FUNCTION__, $param, $isArr);
414
    }
415
416
    /**
417
     * 获取一个通话的详情接口
418
     *
419
     * @param array $param
420
     * @param bool  $isArr
421
     * @return array|object|null
422
     * @throws \Exception
423
     */
424 3
    public function phoneLogInfo($param = [], $isArr = false)
425
    {
426 3
        return $this->send(__FUNCTION__, $param, $isArr);
427
    }
428
429
    /**
430
     * prevent from being unserialized (which would create a second instance of it)
431
     */
432
    public function __wakeup()
433
    {
434
    }
435
436
437
    /**
438
     * prevent the instance from being cloned (which would create a second instance of it)
439
     */
440
    private function __clone()
441
    {
442
    }
443
}
444