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

Pretreat::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 6
rs 9.4285
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