Completed
Pull Request — master (#228)
by r
04:17
created

PersistentFop::status()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
ccs 0
cts 6
cp 0
crap 6
rs 9.6666
c 0
b 0
f 0
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($params, 'pipeline', $this->pipeline);
69 9
        \Qiniu\setWithoutEmpty($params, '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 9
            return array(null, new Error($url, $response));
80
        }
81
        $r = $response->json();
82
        $id = $r['persistentId'];
83
        return array($id, null);
84
    }
85
86
    public static function status($id)
87
    {
88
        $url = Config::API_HOST . "/status/get/prefop?id=$id";
89
        $response = Client::get($url);
90
        if (!$response->ok()) {
91
            return array(null, new Error($url, $response));
92
        }
93
        return array($response->json(), null);
94
    }
95
}
96