Completed
Pull Request — master (#318)
by
unknown
10:48
created

Operation::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.4746

Importance

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