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

PersistentFop::execute()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 4.054

Importance

Changes 6
Bugs 0 Features 1
Metric Value
c 6
b 0
f 1
dl 0
loc 23
ccs 17
cts 20
cp 0.85
rs 8.7972
cc 4
eloc 18
nc 8
nop 2
crap 4.054
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