Passed
Push — master ( 812cef...79dd4d )
by sabaku
25:16 queued 11:52
created

Pretreat::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Upyun\Api;
3
4
use GuzzleHttp\Psr7;
5
use GuzzleHttp\Client;
6
use Upyun\Config;
7
use Upyun\Signature;
8
use Upyun\Util;
9
10
class Pretreat
11
{
12
13
    /**
14
     * @var Config
15
     */
16
    protected $config;
17
18 3
    public function __construct(Config $config)
19
    {
20 3
        $this->config = $config;
21 3
    }
22
23 1
    public function process($source, $tasks)
24
    {
25 1
        $encodedTasks = Util::base64Json($tasks);
26
27 1
        $client = new Client([
28 1
            'timeout' => $this->config->timeout,
29 1
        ]);
30
31
        $params = array(
32 1
            'bucket_name' => $this->config->bucketName,
33 1
            'notify_url' => $this->config->processNotifyUrl,
34 1
            'source' => $source,
35 1
            'tasks' => $encodedTasks,
36
            'accept' => 'json'
37 1
        );
38
39 1
        $path = '/pretreatment/';
40 1
        $method = 'POST';
41 1
        $signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
42
43 1
        $url = $this->config->getPretreatEndPoint() . $path;
44 1
        $response = $client->request($method, $url, [
45 1
            'headers' => $signedHeaders,
46
            'form_params' => $params
47 1
        ]);
48
49 1
        $body = $response->getBody()->getContents();
50 1
        return json_decode($body, true);
51
    }
52
53
54 2
    public function query($taskIds, $path)
55
    {
56 2
        $client = new Client([
57 2
            'timeout' => $this->config->timeout,
58 2
        ]);
59
60
        $params = array(
61 2
            'service' => $this->config->bucketName,
62 2
            'task_ids' => implode(',', $taskIds)
63 2
        );
64 2
        $path = $path . '?' . http_build_query($params);
65
66 2
        $method = 'GET';
67 2
        $url = $this->config->getPretreatEndPoint() . $path;
68 2
        $signedHeaders = Signature::getHeaderSign($this->config, $method, $path);
69 2
        $response = $client->request($method, $url, [
70
            'headers' => $signedHeaders
71 2
        ]);
72
73 2
        if ($response->getStatusCode() === 200) {
74 2
            $body = $response->getBody()->getContents();
75 2
            $result = json_decode($body, true);
76 2
            if (is_array($result)) {
77 2
                return $result['tasks'];
78
            }
79
        }
80
        return false;
81
    }
82
}
83