Passed
Push — main ( d60a9a...d6a9f4 )
by Sugeng
03:49
created

AwsS3Component::init()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 17
rs 9.6111
cc 5
nc 6
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
 *         'prefix' => '',
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
     * @inheritdoc
91
     */
92
    public function init()
93
    {
94
        if (empty($this->credentials)) {
95
            if (empty($this->key)) {
96
                throw new InvalidConfigException('The "key" property must be set.');
97
            }
98
99
            if (empty($this->secret)) {
100
                throw new InvalidConfigException('The "secret" property must be set.');
101
            }
102
        }
103
104
        if (empty($this->bucket)) {
105
            throw new InvalidConfigException('The "bucket" property must be set.');
106
        }
107
108
        parent::init();
109
    }
110
111
    /**
112
     * @return AwsS3V3Adapter
113
     */
114
    protected function initAdapter()
115
    {
116
        $config = [];
117
118
        if (empty($this->credentials)) {
119
            $config['credentials'] = ['key' => $this->key, 'secret' => $this->secret];
120
        } else {
121
            $config['credentials'] = $this->credentials;
122
        }
123
124
        if ($this->usePathStyleEndpoint === true) {
125
            $config['use_path_style_endpoint'] = true;
126
        }
127
128
        if (!empty($this->region)) {
129
            $config['region'] = $this->region;
130
        }
131
132
        if (!empty($this->endpoint)) {
133
            $config['endpoint'] = $this->endpoint;
134
        }
135
136
        $config['version'] = $this->version;
137
138
        $client = new S3Client($config);
139
140
        return new AwsS3V3Adapter($client, $this->bucket, $this->prefix, null, null, $this->options, $this->streamReads);
0 ignored issues
show
Bug introduced by
It seems like $this->prefix 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

140
        return new AwsS3V3Adapter($client, $this->bucket, /** @scrutinizer ignore-type */ $this->prefix, null, null, $this->options, $this->streamReads);
Loading history...
141
    }
142
}
143