Passed
Push — main ( 2456f4...ce01ad )
by Sugeng
04:03
created

AwsS3Component   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 33
c 1
b 0
f 0
dl 0
loc 103
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 17 5
A initAdapter() 0 21 2
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use Aws\S3\S3Client;
6
use League\Flysystem\AwsS3V3\AwsS3V3Adapter;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Class AwsS3Component
11
 *
12
 * @package diecoding\flysystem
13
 * 
14
 * ```php
15
 * 'components' => [
16
 *     'fs' => [
17
 *         'class' => \diecoding\flysystem\AwsS3Component::class,
18
 *         'endpoint' => 'my-endpoint',
19
 *         'key' => 'my-key',
20
 *         'secret' => 'my-secret',
21
 *         'bucket' => 'my-bucket',
22
 *         'prefix' => '',
23
 *     ],
24
 * ],
25
 * ```
26
 * 
27
 * @link      https://sugengsulistiyawan.my.id/
28
 * @author    Sugeng Sulistiyawan <[email protected]>
29
 * @copyright Copyright (c) 2023
30
 */
31
class AwsS3Component extends AbstractComponent
32
{
33
    /**
34
     * @var string
35
     */
36
    public $endpoint;
37
38
    /**
39
     * @var string
40
     */
41
    public $key;
42
43
    /**
44
     * @var string
45
     */
46
    public $secret;
47
48
    /**
49
     * @var string
50
     */
51
    public $bucket;
52
53
    /**
54
     * @var string
55
     */
56
    public $region = 'us-east-1';
57
58
    /**
59
     * @var string
60
     */
61
    public $version = 'latest';
62
63
    /**
64
     * @var bool
65
     */
66
    public $usePathStyleEndpoint = false;
67
68
    /**
69
     * @var bool
70
     */
71
    public $streamReads = false;
72
73
    /**
74
     * @var array
75
     */
76
    public $options = [];
77
78
    /**
79
     * @var array
80
     */
81
    public $credentials = [];
82
83
    /**
84
     * @var S3Client
85
     */
86
    protected $client;
87
88
    /**
89
     * @inheritdoc
90
     */
91
    public function init()
92
    {
93
        if (empty($this->credentials)) {
94
            if (empty($this->key)) {
95
                throw new InvalidConfigException('The "key" property must be set.');
96
            }
97
98
            if (empty($this->secret)) {
99
                throw new InvalidConfigException('The "secret" property must be set.');
100
            }
101
        }
102
103
        if (empty($this->bucket)) {
104
            throw new InvalidConfigException('The "bucket" property must be set.');
105
        }
106
107
        parent::init();
108
    }
109
110
    /**
111
     * @return AwsS3V3Adapter
112
     */
113
    protected function initAdapter()
114
    {
115
        $config = [];
116
117
        if (empty($this->credentials)) {
118
            $config['credentials'] = [
119
                'key'    => $this->key,
120
                'secret' => $this->secret,
121
            ];
122
        } else {
123
            $config['credentials'] = $this->credentials;
124
        }
125
126
        $config['endpoint']                = $this->endpoint;
127
        $config['use_path_style_endpoint'] = $this->usePathStyleEndpoint;
128
        $config['region']                  = $this->region;
129
        $config['version']                 = $this->version;
130
131
        $this->client = new S3Client($config);
132
133
        return new AwsS3V3Adapter($this->client, $this->bucket, $this->prefix, null, null, $this->options, $this->streamReads);
134
    }
135
}
136