Pretreat   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 93.18%

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
ccs 41
cts 44
cp 0.9318
wmc 6
lcom 1
cbo 6

3 Methods

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