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

Pretreat::query()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 2
dl 0
loc 26
rs 8.8571
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
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
}