Passed
Push — main ( adb48c...989c75 )
by Sugeng
02:59
created

AsyncAwsS3Component::initAdapter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 16
rs 9.8666
cc 1
nc 1
nop 0
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use AsyncAws\Core\Configuration;
6
use AsyncAws\S3\S3Client;
7
use League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter;
8
use yii\base\InvalidConfigException;
9
10
/**
11
 * Interacting with Aws S3 (Async)
12
 * Read more about AsyncAws's S3Client in [their documentation](https://async-aws.com/clients/s3.html).
13
 * @see https://flysystem.thephpleague.com/docs/adapter/aws-s3-v3/
14
 * 
15
 * ```php
16
 * 'components' => [
17
 *     'fs' => [
18
 *         'class'           => \diecoding\flysystem\AsyncAwsS3Component::class,
19
 *         'endpoint'        => 'http://your-endpoint',
20
 *         'bucket'          => 'my-bucket',
21
 *         'accessKeyId'     => 'my-key',
22
 *         'accessKeySecret' => 'my-secret',
23
 *         'prefix'          => '',
24
 *         // 'sharedCredentialsFile'    => '~/.aws/credentials',
25
 *         // 'sharedConfigFile'         => '~/.aws/config',
26
 *         // 'region'                   => 'us-east-1',
27
 *         // 'debug'                    => false,
28
 *         // 'endpointDiscoveryEnabled' => false,
29
 *         // 'pathStyleEndpoint'        => false,
30
 *         // 'sendChunkedBody'          => false,
31
 *     ],
32
 * ],
33
 * ```
34
 * 
35
 * @link      https://sugengsulistiyawan.my.id/
36
 * @author    Sugeng Sulistiyawan <[email protected]>
37
 * @copyright Copyright (c) 2023
38
 */
39
class AsyncAwsS3Component extends AbstractComponent
40
{
41
    /**
42
     * @var string
43
     */
44
    public $bucket;
45
46
    /**
47
     * @var string
48
     */
49
    public $region = Configuration::DEFAULT_REGION;
50
51
    /**
52
     * @var bool
53
     */
54
    public $debug = false;
55
56
    /**
57
     * @var string
58
     */
59
    public $accessKeyId;
60
61
    /**
62
     * @var string
63
     */
64
    public $accessKeySecret;
65
66
    /**
67
     * @var string
68
     */
69
    public $sharedCredentialsFile;
70
71
    /**
72
     * @var string
73
     */
74
    public $sharedConfigFile;
75
76
    /**
77
     * @var string
78
     */
79
    public $endpoint;
80
81
    /**
82
     * @var bool
83
     */
84
    public $endpointDiscoveryEnabled = false;
85
86
    /**
87
     * @var bool
88
     */
89
    public $pathStyleEndpoint = false;
90
91
    /**
92
     * @var bool
93
     */
94
    public $sendChunkedBody = false;
95
96
    /**
97
     * @var S3Client
98
     */
99
    protected $client;
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function init()
105
    {
106
        if (empty($this->accessKeyId)) {
107
            throw new InvalidConfigException('The "accessKeyId" property must be set.');
108
        }
109
        if (empty($this->accessKeySecret)) {
110
            throw new InvalidConfigException('The "accessKeySecret" property must be set.');
111
        }
112
        if (empty($this->bucket)) {
113
            throw new InvalidConfigException('The "bucket" property must be set.');
114
        }
115
116
        parent::init();
117
    }
118
119
    /**
120
     * @return AsyncAwsS3Adapter
121
     */
122
    protected function initAdapter()
123
    {
124
        $config['region'] = $this->region;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$config was never initialized. Although not strictly required by PHP, it is generally a good practice to add $config = array(); before regardless.
Loading history...
125
        $config['debug'] = var_export($this->debug, true);
126
        $config['accessKeyId'] = $this->accessKeyId;
127
        $config['accessKeySecret'] = $this->accessKeySecret;
128
        $config['sharedCredentialsFile'] = $this->sharedCredentialsFile;
129
        $config['sharedConfigFile'] = $this->sharedConfigFile;
130
        $config['endpoint'] = $this->endpoint;
131
        $config['endpointDiscoveryEnabled'] = var_export($this->endpointDiscoveryEnabled, true);
132
        $config['pathStyleEndpoint'] = var_export($this->pathStyleEndpoint, true);
133
        $config['sendChunkedBody'] = var_export($this->sendChunkedBody, true);
134
135
        $this->client = new S3Client($config);
136
137
        return new AsyncAwsS3Adapter($this->client, $this->bucket, $this->prefix);
138
    }
139
}