Completed
Push — master ( bfc0cc...f0e226 )
by sabaku
04:05
created

Pretreat   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B process() 0 25 1
B query() 0 26 3
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
11
class Pretreat {
12
13
    protected $url = 'http://p0.api.upyun.com';
14
15
    public function __construct(Config $config) {
16
        $this->config = $config;
0 ignored issues
show
Bug introduced by
The property config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
17
    }
18
19
    public function process($source, $tasks) {
20
        $encodedTasks = Util::base64Json($tasks);
21
22
        $client = new Client([
23
            'timeout' => $this->config->timeout,
24
        ]);
25
26
        $params = array(
27
            'bucket_name' => $this->config->bucketName,
28
            'notify_url' => $this->config->processNotifyUrl,
29
            'source' => $source,
30
            'tasks' => $encodedTasks,
31
            'accept' => 'json'
32
        );
33
34
        $url = $this->url . '/pretreatment';
35
        $signature = Signature::getSignature($this->config, $params, Signature::SIGN_VIDEO);
36
        $response = $client->request('POST', $url, [
37
            'headers' => array('Authorization' => "UPYUN {$this->config->operatorName}:$signature"),
38
            'form_params' => $params
39
        ]);
40
41
        $body = $response->getBody()->getContents();
42
        return json_decode($body, true);
43
    }
44
45
46
    public function query($taskIds, $path) {
47
        $client = new Client([
48
            'timeout' => $this->config->timeout,
49
        ]);
50
51
        $params = array(
52
            'bucket_name' => $this->config->bucketName,
53
            'task_ids' => implode(',', $taskIds)
54
        );
55
56
        $url = $this->url . $path;
57
        $signature = Signature::getSignature($this->config, $params, Signature::SIGN_VIDEO);
58
        $response = $client->request('GET', $url, [
59
            'headers' => array('Authorization' => "UPYUN {$this->config->operatorName}:$signature"),
60
            'query' => $params
61
        ]);
62
63
        if ($response->getStatusCode() === 200) {
64
            $body = $response->getBody()->getContents();
65
            $result = json_decode($body, true);
66
            if (is_array($result)) {
67
                return $result['tasks'];
68
            }
69
        }
70
        return false;
71
    }
72
}