Completed
Pull Request — master (#288)
by Bai
13:51
created

PersistentFop::execute()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 5.6205

Importance

Changes 0
Metric Value
cc 5
nc 16
nop 6
dl 0
loc 27
ccs 17
cts 24
cp 0.7083
crap 5.6205
rs 9.1768
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 配置对象,Config 对象
23
     * */
24
    private $config;
25
26
27 9
    public function __construct($auth, $config = null)
28
    {
29 9
        $this->auth = $auth;
30 9
        if ($config == null) {
31 9
            $this->config = new Config();
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Qiniu\Config() of type object<Qiniu\Config> is incompatible with the declared type object<Qiniu\Processing\配置对象,Config> of property $config.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32 9
        } else {
33
            $this->config = $config;
34
        }
35 9
    }
36
37
    /**
38
     * 对资源文件进行异步持久化处理
39
     * @param $bucket     资源所在空间
40
     * @param $key        待处理的源文件
41
     * @param $fops       string|array  待处理的pfop操作,多个pfop操作以array的形式传入。
42
     *                    eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
43
     * @param $pipeline   资源处理队列
44
     * @param $notify_url 处理结果通知地址
45
     * @param $force      是否强制执行一次新的指令
46
     *
47
     *
48
     * @return array 返回持久化处理的persistentId, 和返回的错误。
49
     *
50
     * @link http://developer.qiniu.com/docs/v6/api/reference/fop/
51
     */
52 9
    public function execute($bucket, $key, $fops, $pipeline = null, $notify_url = null, $force = false)
53
    {
54 9
        if (is_array($fops)) {
55 3
            $fops = implode(';', $fops);
56 3
        }
57 9
        $params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops);
58 9
        \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline);
59 9
        \Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url);
60 9
        if ($force) {
61
            $params['force'] = 1;
62
        }
63 9
        $data = http_build_query($params);
64 9
        $scheme = "http://";
65 9
        if ($this->config->useHTTPS === true) {
66
            $scheme = "https://";
67
        }
68 9
        $url = $scheme . Config::API_HOST . '/pfop/';
69 9
        $headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
70 9
        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
71 9
        $response = Client::post($url, $data, $headers);
72 9
        if (!$response->ok()) {
73 9
            return array(null, new Error($url, $response));
74
        }
75
        $r = $response->json();
76
        $id = $r['persistentId'];
77
        return array($id, null);
78
    }
79
80
    public function status($id)
81
    {
82
        $scheme = "http://";
83
84
        if ($this->config->useHTTPS === true) {
85
            $scheme = "https://";
86
        }
87
        $url = $scheme . Config::API_HOST . "/status/get/prefop?id=$id";
88
        $response = Client::get($url);
89
        if (!$response->ok()) {
90
            return array(null, new Error($url, $response));
91
        }
92
        return array($response->json(), null);
93
    }
94
}
95