Test Failed
Pull Request — master (#46)
by
unknown
03:08
created

Pretreat   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 76
ccs 0
cts 55
cp 0
rs 10
wmc 6
lcom 1
cbo 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
B process() 0 29 1
B query() 0 28 3
1
<?php
2
namespace Upyun\Api;
3
4
use GuzzleHttp\Client;
5
use Upyun\Config;
6
use Upyun\Signature;
7
use Upyun\Util;
8
9
class Pretreat
10
{
11
12
    /**
13
     * @var Config
14
     */
15
    protected $config;
16
17
    public function __construct(Config $config)
18
    {
19
        if (!$config->processNotifyUrl) {
20
            throw new \Exception("should config prosessNotifyUrl first.");
21
        }
22
        $this->config = $config;
23
    }
24
25
    public function process($tasks, $optionalParams = array())
26
    {
27
        $encodedTasks = Util::base64Json($tasks);
28
29
        $client = new Client([
30
            'timeout' => $this->config->timeout,
31
        ]);
32
33
        $params = array(
34
            'service' => $this->config->serviceName,
35
            'notify_url' => $this->config->processNotifyUrl,
36
            'tasks' => $encodedTasks,
37
        );
38
39
        $params = array_merge($params, $optionalParams);
40
41
        $path = '/pretreatment/';
42
        $method = 'POST';
43
        $signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
44
45
        $url = $this->config->getPretreatEndPoint() . $path;
46
        $response = $client->request($method, $url, [
47
            'headers' => $signedHeaders,
48
            'form_params' => $params
49
        ]);
50
51
        $body = $response->getBody()->getContents();
52
        return json_decode($body, true);
53
    }
54
55
56
    public function query($taskIds, $path)
57
    {
58
        $client = new Client([
59
            'timeout' => $this->config->timeout,
60
        ]);
61
62
        $params = array(
63
            'service' => $this->config->serviceName,
64
            'task_ids' => implode(',', $taskIds)
65
        );
66
        $path = $path . '?' . http_build_query($params);
67
68
        $method = 'GET';
69
        $url = $this->config->getPretreatEndPoint() . $path;
70
        $signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
71
        $response = $client->request($method, $url, [
72
            'headers' => $signedHeaders
73
        ]);
74
75
        if ($response->getStatusCode() === 200) {
76
            $body = $response->getBody()->getContents();
77
            $result = json_decode($body, true);
78
            if (is_array($result)) {
79
                return $result['tasks'];
80
            }
81
        }
82
        return false;
83
    }
84
}
85