Operation::buildUrl()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 12
rs 10
ccs 2
cts 2
cp 1
crap 3
1
<?php
2
3
namespace Qiniu\Processing;
4
5
use Qiniu\Http\Client;
6
use Qiniu\Http\Error;
7
use Qiniu\Http\Proxy;
8
9
final class Operation
10
{
11
12
    private $auth;
13
    private $token_expire;
14
    private $domain;
15 9
    private $proxy;
16
17 9
    public function __construct(
18 9
        $domain,
19 9
        $auth = null,
20 9
        $token_expire = 3600,
21
        $proxy = null,
22
        $proxy_auth = null,
23
        $proxy_user_password = null
24
    ) {
25
        $this->auth = $auth;
26
        $this->domain = $domain;
27
        $this->token_expire = $token_expire;
28
        $this->proxy = new Proxy($proxy, $proxy_auth, $proxy_user_password);
29
    }
30
31
32
    /**
33
     * 对资源文件进行处理
34 6
     *
35
     * @param string $key 待处理的资源文件名
36 6
     * @param string $fops string|array  fop操作,多次fop操作以array的形式传入。
37 6
     *                eg. imageView2/1/w/200/h/200, imageMogr2/thumbnail/!75px
38 6
     *
39 3
     * @return array 文件处理后的结果及错误。
40
     *
41 3
     * @link http://developer.qiniu.com/docs/v6/api/reference/fop/
42 3
     */
43
    public function execute($key, $fops)
44
    {
45
        $url = $this->buildUrl($key, $fops);
46
        $resp = Client::get($url, array(), $this->proxy->makeReqOpt());
47 9
        if (!$resp->ok()) {
48
            return array(null, new Error($url, $resp));
49 9
        }
50 3
        if ($resp->json() !== null) {
51 3
            return array($resp->json(), null);
52
        }
53 9
        return array($resp->body, null);
54 9
    }
55 3
56 3
    public function buildUrl($key, $fops, $protocol = 'http')
57
    {
58 9
        if (is_array($fops)) {
59
            $fops = implode('|', $fops);
60
        }
61
62
        $url = $protocol . "://$this->domain/$key?$fops";
63
        if ($this->auth !== null) {
64
            $url = $this->auth->privateDownloadUrl($url, $this->token_expire);
65
        }
66
67
        return $url;
68
    }
69
}
70