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

Operation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 86.36%

Importance

Changes 0
Metric Value
dl 0
loc 53
ccs 19
cts 22
cp 0.8636
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A buildUrl() 0 13 3
A execute() 0 12 3
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