Completed
Push — master ( 652ef6...d33d89 )
by Bai
10:23
created

PersistentFop   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.88%

Importance

Changes 14
Bugs 1 Features 3
Metric Value
wmc 7
c 14
b 1
f 3
lcom 1
cbo 3
dl 0
loc 82
ccs 29
cts 33
cp 0.8788
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B execute() 0 23 4
A status() 0 9 2
1
<?php
2
namespace Qiniu\Processing;
3
4
use Qiniu\Config;
5
use Qiniu\Http\Client;
6
use Qiniu\Http\Error;
7
use Qiniu\Processing\Operation;
8
9
/**
10
 * 持久化处理类,该类用于主动触发异步持久化操作.
11
 *
12
 * @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
13
 */
14
final class PersistentFop
15
{
16
    /**
17
     * @var 账号管理密钥对,Auth对象
18
     */
19
    private $auth;
20
21
    /**
22
     * @var 操作资源所在空间
23
     */
24
    private $bucket;
25
26
    /**
27
     * @var 多媒体处理队列,详见 https://portal.qiniu.com/mps/pipeline
28
     */
29
    private $pipeline;
30
31
    /**
32
     * @var 持久化处理结果通知URL
33
     */
34
    private $notify_url;
35
36
    /**
37
     * @var boolean 是否强制覆盖已有的重名文件
38
     */
39
    private $force;
40
41
42 9
    public function __construct($auth, $bucket, $pipeline = null, $notify_url = null, $force = false)
43
    {
44 9
        $this->auth = $auth;
45 9
        $this->bucket = $bucket;
46 9
        $this->pipeline = $pipeline;
47 9
        $this->notify_url = $notify_url;
48 9
        $this->force = $force;
49 9
    }
50
51
    /**
52
     * 对资源文件进行异步持久化处理
53
     *
54
     * @param $key   待处理的源文件
55
     * @param $fops  string|array  待处理的pfop操作,多个pfop操作以array的形式传入。
56
     *                eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
57
     *
58
     * @return array 返回持久化处理的persistentId, 和返回的错误。
59
     *
60
     * @link http://developer.qiniu.com/docs/v6/api/reference/fop/
61
     */
62 9
    public function execute($key, $fops)
63
    {
64 9
        if (is_array($fops)) {
65 3
            $fops = implode(';', $fops);
66 3
        }
67 9
        $params = array('bucket' => $this->bucket, 'key' => $key, 'fops' => $fops);
68 9
        \Qiniu\setWithoutEmpty($query, 'pipeline', $this->pipeline);
69 9
        \Qiniu\setWithoutEmpty($query, 'notifyURL', $this->notify_url);
70 9
        if ($this->force) {
71
            $params['force'] = 1;
72
        }
73 9
        $data = http_build_query($params);
74 9
        $url = Config::API_HOST . '/pfop/';
75 9
        $headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
76 9
        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
77 9
        $response = Client::post($url, $data, $headers);
78 9
        if (!$response->ok()) {
79
            return array(null, new Error($url, $response));
80
        }
81 9
        $r = $response->json();
82 9
        $id = $r['persistentId'];
83 9
        return array($id, null);
84
    }
85
86 9
    public static function status($id)
87
    {
88 9
        $url = Config::API_HOST . "/status/get/prefop?id=$id";
89 9
        $response = Client::get($url);
90 9
        if (!$response->ok()) {
91
            return array(null, new Error($url, $response));
92
        }
93 9
        return array($response->json(), null);
94
    }
95
}
96