Passed
Push — main ( 082ac6...e6b4be )
by Sugeng
03:46
created

AwsS3Component::initAdapter()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
c 1
b 0
f 0
dl 0
loc 27
rs 9.4888
cc 5
nc 16
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use Aws\S3\S3Client;
6
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
7
use Yii;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Class AwsS3Component
12
 *
13
 * @package diecoding\flysystem
14
 * 
15
 * ```php
16
 * 'components' => [
17
 *     'fs' => [
18
 *         'class' => \diecoding\flysystem\AwsS3Component::class,
19
 *         'endpoint' => 'my-endpoint',
20
 *         'credentials' => [ // array|\Aws\CacheInterface|\Aws\Credentials\CredentialsInterface|bool|callable
21
 *             'key' => 'my-key',
22
 *             'secret' => 'my-secret',
23
 *         ],
24
 *         'bucket' => 'my-bucket',
25
 *         'region' => 'us-east-1',
26
 *         'version' => 'latest',
27
 *         'usePathStyleEndpoint' => true,
28
 *         'basePath' => '',
29
 *     ],
30
 * ],
31
 * ```
32
 * 
33
 * @link      https://sugengsulistiyawan.my.id/
34
 * @author    Sugeng Sulistiyawan <[email protected]>
35
 * @copyright Copyright (c) 2023
36
 */
37
class AwsS3Component extends AbstractComponent
38
{
39
    /**
40
     * @var string
41
     */
42
    public $endpoint = '';
43
44
    /**
45
     * @var string
46
     */
47
    public $key = '';
48
49
    /**
50
     * @var string
51
     */
52
    public $secret = '';
53
54
    /**
55
     * @var string
56
     */
57
    public $region = '';
58
59
    /**
60
     * @var string
61
     */
62
    public $version = 'latest';
63
64
    /**
65
     * @var string
66
     */
67
    public $bucket = '';
68
69
    /**
70
     * @var bool
71
     */
72
    public $usePathStyleEndpoint = false;
73
74
    /**
75
     * @var array
76
     */
77
    public $options = [];
78
79
    /**
80
     * @var bool
81
     */
82
    public $streamReads = false;
83
84
    /**
85
     * @var array|\Aws\CacheInterface|\Aws\Credentials\CredentialsInterface|bool|callable
86
     */
87
    public $credentials;
88
89
    /**
90
     * @var S3Client
91
     */
92
    protected $client;
93
94
    /**
95
     * @inheritdoc
96
     */
97
    public function init()
98
    {
99
        if (empty($this->credentials)) {
100
            if (empty($this->key)) {
101
                throw new InvalidConfigException('The "key" property must be set.');
102
            }
103
104
            if (empty($this->secret)) {
105
                throw new InvalidConfigException('The "secret" property must be set.');
106
            }
107
        }
108
109
        if (empty($this->bucket)) {
110
            throw new InvalidConfigException('The "bucket" property must be set.');
111
        }
112
113
        parent::init();
114
    }
115
116
    /**
117
     * Get a URL
118
     * 
119
     * @param string $filePath
120
     * @return string
121
     */
122
    public function getUrl(string $filePath)
123
    {
124
        return $this->client->getObjectUrl($this->bucket, $this->normalizePath($filePath));
125
    }
126
127
    /**
128
     * Get a pre-signed URL
129
     * 
130
     * @param string $filePath
131
     * @param int|string|\DateTimeInterface $expires
132
     * @return string
133
     */
134
    public function getPresignedUrl(string $filePath, $expires = '+5 Minutes')
135
    {
136
        $command = $this->client->getCommand('GetObject', [
137
            'Bucket' => $this->bucket,
138
            'Key' => $this->normalizePath($filePath),
139
        ]);
140
        $presignedRequest = $this->client->createPresignedRequest($command, $expires);
141
142
        return (string) $presignedRequest->getUri();
143
    }
144
145
    /**
146
     * @return AwsS3V3Adapter
147
     */
148
    protected function initAdapter()
149
    {
150
        $config = [];
151
152
        if (empty($this->credentials)) {
153
            $config['credentials'] = ['key' => $this->key, 'secret' => $this->secret];
154
        } else {
155
            $config['credentials'] = $this->credentials;
156
        }
157
158
        if ($this->usePathStyleEndpoint === true) {
159
            $config['use_path_style_endpoint'] = true;
160
        }
161
162
        if (!empty($this->region)) {
163
            $config['region'] = $this->region;
164
        }
165
166
        if (!empty($this->endpoint)) {
167
            $config['endpoint'] = $this->endpoint;
168
        }
169
170
        $config['version'] = $this->version;
171
172
        $this->client = new S3Client($config);
173
174
        return new AwsS3V3Adapter($this->client, $this->bucket, $this->basePath, null, null, $this->options, $this->streamReads);
0 ignored issues
show
Bug introduced by
It seems like $this->basePath can also be of type null; however, parameter $prefix of League\Flysystem\AwsS3V3...3Adapter::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

174
        return new AwsS3V3Adapter($this->client, $this->bucket, /** @scrutinizer ignore-type */ $this->basePath, null, null, $this->options, $this->streamReads);
Loading history...
175
    }
176
}
177