Passed
Push — master ( f5a6fc...a9b671 )
by
unknown
24:52 queued 03:21
created

PersistentFop::getApiHost()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 10
ccs 0
cts 0
cp 0
crap 12
1
<?php
2
3
namespace Qiniu\Processing;
4
5
use Qiniu\Config;
6
use Qiniu\Http\Error;
7
use Qiniu\Http\Client;
8
use Qiniu\Http\Proxy;
9
use Qiniu\Zone;
10
11
/**
12
 * 持久化处理类,该类用于主动触发异步持久化操作.
13
 *
14
 * @link http://developer.qiniu.com/docs/v6/api/reference/fop/pfop/pfop.html
15
 */
16
final class PersistentFop
17
{
18
    /**
19
     * @var 账号管理密钥对,Auth对象
0 ignored issues
show
Documentation Bug introduced by
The doc comment 账号管理密钥对,Auth对象 at position 0 could not be parsed: Unknown type name '账号管理密钥对,Auth对象' at position 0 in 账号管理密钥对,Auth对象.
Loading history...
20
     */
21
    private $auth;
22
23
    /*
24
     * @var 配置对象,Config 对象
0 ignored issues
show
Documentation Bug introduced by
The doc comment 配置对象,Config at position 0 could not be parsed: Unknown type name '配置对象,Config' at position 0 in 配置对象,Config.
Loading history...
25
     * */
26
    private $config;
27 9
28
    /**
29 9
     * @var 代理信息
0 ignored issues
show
Documentation Bug introduced by
The doc comment 代理信息 at position 0 could not be parsed: Unknown type name '代理信息' at position 0 in 代理信息.
Loading history...
30 9
     */
31 9
    private $proxy;
32 9
33
34
    public function __construct($auth, $config = null, $proxy = null, $proxy_auth = null, $proxy_user_password = null)
35 9
    {
36
        $this->auth = $auth;
37
        if ($config == null) {
38
            $this->config = new Config();
39
        } else {
40
            $this->config = $config;
41
        }
42
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
43
    }
44
45
    /**
46
     * 对资源文件进行异步持久化处理
47
     * @param string $bucket 资源所在空间
48
     * @param string $key 待处理的源文件
49
     * @param string|array $fops 待处理的pfop操作,多个pfop操作以array的形式传入。
50
     *                    eg. avthumb/mp3/ab/192k, vframe/jpg/offset/7/w/480/h/360
51
     * @param string $pipeline 资源处理队列
52 9
     * @param string $notify_url 处理结果通知地址
53
     * @param bool $force 是否强制执行一次新的指令
54 9
     * @param int $type 为 `1` 时开启闲时任务
55 3
     *
56 3
     *
57 9
     * @return array 返回持久化处理的 persistentId 与可能出现的错误。
58 9
     *
59 9
     * @link http://developer.qiniu.com/docs/v6/api/reference/fop/
60 9
     */
61
    public function execute(
62
        $bucket,
63 9
        $key,
64 9
        $fops,
65 9
        $pipeline = null,
66
        $notify_url = null,
67
        $force = false,
68 9
        $type = null
69 9
    ) {
70 9
        if (is_array($fops)) {
71 9
            $fops = implode(';', $fops);
72 9
        }
73
        $params = array('bucket' => $bucket, 'key' => $key, 'fops' => $fops);
74
        \Qiniu\setWithoutEmpty($params, 'pipeline', $pipeline);
75 9
        \Qiniu\setWithoutEmpty($params, 'notifyURL', $notify_url);
76 9
        \Qiniu\setWithoutEmpty($params, 'type', $type);
77 9
        if ($force) {
78
            $params['force'] = 1;
79
        }
80 9
        $data = http_build_query($params);
81
        $scheme = "http://";
82 9
        if ($this->config->useHTTPS === true) {
83
            $scheme = "https://";
84 9
        }
85
        $apiHost = $this->getApiHost();
86
        $url = $scheme . $apiHost . '/pfop/';
87 9
        $headers = $this->auth->authorization($url, $data, 'application/x-www-form-urlencoded');
88 9
        $headers['Content-Type'] = 'application/x-www-form-urlencoded';
89 9
        $response = Client::post($url, $data, $headers, $this->proxy->makeReqOpt());
90
        if (!$response->ok()) {
91
            return array(null, new Error($url, $response));
92 9
        }
93
        $r = $response->json();
94
        $id = $r['persistentId'];
95
        return array($id, null);
96
    }
97
98
    /**
99
     * @param string $id
100
     * @return array 返回任务状态与可能出现的错误
101
     */
102
    public function status($id)
103
    {
104
        $scheme = "http://";
105
106
        if ($this->config->useHTTPS === true) {
107
            $scheme = "https://";
108
        }
109
        $apiHost = $this->getApiHost();
110
        $url = $scheme . $apiHost . "/status/get/prefop?id=$id";
111
        $response = Client::get($url, array(), $this->proxy->makeReqOpt());
112
        if (!$response->ok()) {
113
            return array(null, new Error($url, $response));
114
        }
115
        return array($response->json(), null);
116
    }
117
118
    private function getApiHost()
119
    {
120
        if (!empty($this->config->zone) && !empty($this->config->zone->apiHost)) {
121
            $apiHost = $this->config->zone->apiHost;
122
        } else {
123
            $apiHost = Config::API_HOST;
124
        }
125
        return $apiHost;
126
    }
127
}
128