Passed
Push — v6 ( aac15a...ba732b )
by 光春
04:36
created

CosService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
eloc 34
c 2
b 0
f 0
dl 0
loc 75
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getConfig() 0 7 1
A secretId() 0 4 1
A secretKey() 0 4 1
A bucket() 0 4 1
A region() 0 4 1
A upload() 0 27 6
1
<?php
2
3
// +----------------------------------------------------------------------
4
// | ThinkLibrary 6.0 for ThinkPhP 6.0
5
// +----------------------------------------------------------------------
6
// | 版权所有 2017~2020 [ https://www.dtapp.net ]
7
// +----------------------------------------------------------------------
8
// | 官方网站: https://gitee.com/liguangchun/ThinkLibrary
9
// +----------------------------------------------------------------------
10
// | 开源协议 ( https://mit-license.org )
11
// +----------------------------------------------------------------------
12
// | gitee 仓库地址 :https://gitee.com/liguangchun/ThinkLibrary
13
// | github 仓库地址 :https://github.com/GC0202/ThinkLibrary
14
// | gitlab 仓库地址 :https://gitlab.com/liguangchun/thinklibrary
15
// | weixin 仓库地址 :https://git.weixin.qq.com/liguangchun/ThinkLibrary
16
// | huaweicloud 仓库地址 :https://codehub-cn-south-1.devcloud.huaweicloud.com/composer00001/ThinkLibrary.git
17
// | Packagist 地址 :https://packagist.org/packages/liguangchun/think-library
18
// +----------------------------------------------------------------------
19
20
namespace DtApp\ThinkLibrary\service\tencent;
21
22
use DtApp\ThinkLibrary\Service;
23
use Exception;
24
use Qcloud\Cos\Client;
25
26
/**
27
 * 腾讯云对象存储
28
 * https://cloud.tencent.com/product/cos
29
 * Class CosService
30
 * @package DtApp\ThinkLibrary\service\tencent
31
 */
32
class CosService extends Service
33
{
34
    private $secretId, $secretKey, $region, $bucket;
35
36
    public function secretId(string $secretId)
37
    {
38
        $this->secretId = $secretId;
39
        return $this;
40
    }
41
42
    public function secretKey(string $secretKey)
43
    {
44
        $this->secretKey = $secretKey;
45
        return $this;
46
    }
47
48
    public function region(string $region)
49
    {
50
        $this->region = $region;
51
        return $this;
52
    }
53
54
    public function bucket(string $bucket)
55
    {
56
        $this->bucket = $bucket;
57
        return $this;
58
    }
59
60
    /**
61
     * 获取配置信息
62
     * @return $this
63
     */
64
    private function getConfig()
65
    {
66
        $this->secretId = config('dtapp.tencent.cos.secret_id');
67
        $this->secretKey = config('dtapp.tencent.cos.secret_key');
68
        $this->region = config('dtapp.tencent.cos.region');
69
        $this->bucket = config('dtapp.tencent.cos.bucket');
70
        return $this;
71
    }
72
73
    /**
74
     * 上传文件
75
     * @param $object
76
     * @param $filePath
77
     * @return bool
78
     * @throws Exception
79
     */
80
    public function upload(string $object, string $filePath)
81
    {
82
        if (empty($this->secretId) || empty($this->secretKey) || empty($this->region)) {
83
            $this->getConfig();
84
        }
85
        $cosClient = new Client(
86
            array(
87
                'region' => $this->region,
88
                'schema' => 'http', //协议头部,默认为http
89
                'credentials' => array(
90
                    'secretId' => $this->secretId,
91
                    'secretKey' => $this->secretKey
92
                )
93
            )
94
        );
95
        $key = $object;
96
        $file = fopen($filePath, "rb");
97
        if ($file && empty($this->bucket)) {
0 ignored issues
show
introduced by
$file is of type false|resource, thus it always evaluated to false.
Loading history...
98
            $this->getConfig();
99
            $result = $cosClient->putObject(
100
                array(
101
                    'Bucket' => $this->bucket,
102
                    'Key' => $key,
103
                    'Body' => $file)
104
            );
105
        }
106
        return config('dtapp.tencent.cos.url', '') . $object;
0 ignored issues
show
Bug Best Practice introduced by
The expression return config('dtapp.ten...cos.url', '') . $object returns the type string which is incompatible with the documented return type boolean.
Loading history...
107
    }
108
}
109