AsyncAwsS3Component   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A initAdapter() 0 12 4
A init() 0 9 1
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use AsyncAws\S3\S3Client;
6
use League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter;
7
use yii\base\InvalidConfigException;
8
9
/**
10
 * Interacting with Aws S3 (Async) filesystem
11
 * Read more about AsyncAws's S3Client in [their documentation](https://async-aws.com/clients/s3.html).
12
 * @see https://flysystem.thephpleague.com/docs/adapter/aws-s3-v3/
13
 * 
14
 * ```php
15
 * 'components' => [
16
 *     'fs' => [
17
 *         'class' => \diecoding\flysystem\AsyncAwsS3Component::class,
18
 *         'endpoint' => 'http://your-endpoint',
19
 *         'bucket' => 'my-bucket',
20
 *         'accessKeyId' => 'my-key',
21
 *         'accessKeySecret' => 'my-secret',
22
 *         // 'sharedCredentialsFile' => '~/.aws/credentials',
23
 *         // 'sharedConfigFile' => '~/.aws/config',
24
 *         // 'region' => 'us-east-1',
25
 *         // 'endpointDiscoveryEnabled' => false,
26
 *         // 'pathStyleEndpoint' => false,
27
 *         // 'sendChunkedBody' => false,
28
 *         // 'debug' => false,
29
 *         // 'prefix' => '',
30
 *     ],
31
 * ],
32
 * ```
33
 * 
34
 * @link      https://sugengsulistiyawan.my.id/
35
 * @author    Sugeng Sulistiyawan <[email protected]>
36
 * @copyright Copyright (c) 2023
37
 */
38
class AsyncAwsS3Component extends AbstractComponent
39
{
40
    /**
41
     * @var string
42
     */
43
    public $bucket;
44
45
    /**
46
     * @var string
47
     */
48
    public $region;
49
50
    /**
51
     * @var string
52
     */
53
    public $accessKeyId;
54
55
    /**
56
     * @var string
57
     */
58
    public $accessKeySecret;
59
60
    /**
61
     * @var string
62
     */
63
    public $sharedCredentialsFile;
64
65
    /**
66
     * @var string
67
     */
68
    public $sharedConfigFile;
69
70
    /**
71
     * @var string
72
     */
73
    public $endpoint;
74
75
    /**
76
     * @var bool
77
     */
78
    public $endpointDiscoveryEnabled;
79
80
    /**
81
     * @var bool
82
     */
83
    public $pathStyleEndpoint;
84
85
    /**
86
     * @var bool
87
     */
88
    public $sendChunkedBody;
89
90
    /**
91
     * @var S3Client
92
     */
93
    protected $client;
94
95
    /**
96
     * @var bool[]
97
     */
98
    protected $_availableOptions = [
99
        'region' => false,
100
        'debug' => true,
101
        'accessKeyId' => false,
102
        'accessKeySecret' => false,
103
        'sharedCredentialsFile' => false,
104
        'sharedConfigFile' => false,
105
        'endpoint' => false,
106
        'endpointDiscoveryEnabled' => true,
107
        'pathStyleEndpoint' => true,
108
        'sendChunkedBody' => true,
109
    ];
110
111
    /**
112
     * @inheritdoc
113
     * @throws InvalidConfigException
114
     */
115
    public function init()
116
    {
117
        $this->validateProperties([
118
            'bucket',
119
            'accessKeyId',
120
            'accessKeySecret',
121
        ]);
122
123
        parent::init();
124
    }
125
126
    /**
127
     * @return AsyncAwsS3Adapter
128
     */
129
    protected function initAdapter()
130
    {
131
        $config = [];
132
        foreach ($this->_availableOptions as $property => $export) {
133
            if ($this->$property !== null) {
134
                $config[$property] = $export ? var_export($this->$property, true) : $this->$property;
135
            }
136
        }
137
138
        $this->client = new S3Client($config);
139
140
        return new AsyncAwsS3Adapter($this->client, $this->bucket, (string) $this->prefix);
141
    }
142
}
143