|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Chinstrap\Core\Factories; |
|
6
|
|
|
|
|
7
|
|
|
use Aws\S3\S3Client; |
|
8
|
|
|
use Chinstrap\Core\Exceptions\Configuration\RootDirNotDefined; |
|
9
|
|
|
use Chinstrap\Core\Exceptions\Factories\BadFlysystemConfigurationException; |
|
10
|
|
|
use League\Flysystem\Adapter\Ftp as FTPAdapter; |
|
11
|
|
|
use League\Flysystem\Adapter\Local; |
|
12
|
|
|
use League\Flysystem\AwsS3v3\AwsS3Adapter; |
|
13
|
|
|
use League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter; |
|
14
|
|
|
use League\Flysystem\Cached\CachedAdapter; |
|
15
|
|
|
use League\Flysystem\Cached\Storage\Stash as StashStore; |
|
16
|
|
|
use League\Flysystem\Filesystem; |
|
17
|
|
|
use League\Flysystem\Memory\MemoryAdapter; |
|
18
|
|
|
use League\Flysystem\Sftp\SftpAdapter; |
|
19
|
|
|
use MicrosoftAzure\Storage\Blob\BlobRestProxy; |
|
20
|
|
|
use PublishingKit\Config\Config; |
|
21
|
|
|
use Spatie\Dropbox\Client; |
|
22
|
|
|
use Spatie\FlysystemDropbox\DropboxAdapter; |
|
23
|
|
|
use Stash\Pool; |
|
24
|
|
|
|
|
25
|
|
|
final class FlysystemFactory |
|
26
|
|
|
{ |
|
27
|
|
|
private Pool $pool; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(Pool $pool) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->pool = $pool; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function make(Config $config): Filesystem |
|
35
|
|
|
{ |
|
36
|
|
|
switch ($config->get('driver')) { |
|
37
|
|
|
case 'memory': |
|
38
|
|
|
$adapter = $this->createMemoryAdapter(); |
|
39
|
|
|
break; |
|
40
|
|
|
case 'dropbox': |
|
41
|
|
|
$adapter = $this->createDropboxAdapter($config); |
|
42
|
|
|
break; |
|
43
|
|
|
case 'azure': |
|
44
|
|
|
$adapter = $this->createAzureAdapter($config); |
|
45
|
|
|
break; |
|
46
|
|
|
case 's3': |
|
47
|
|
|
$adapter = $this->createS3Adapter($config); |
|
48
|
|
|
break; |
|
49
|
|
|
case 'sftp': |
|
50
|
|
|
$adapter = $this->createSftpAdapter($config); |
|
51
|
|
|
break; |
|
52
|
|
|
case 'ftp': |
|
53
|
|
|
$adapter = $this->createFtpAdapter($config); |
|
54
|
|
|
break; |
|
55
|
|
|
default: |
|
56
|
|
|
$adapter = $this->createLocalAdapter($config); |
|
57
|
|
|
break; |
|
58
|
|
|
} |
|
59
|
|
|
$cache = new StashStore($this->pool, 'storageKey', 300); |
|
60
|
|
|
return new Filesystem( |
|
61
|
|
|
new CachedAdapter( |
|
62
|
|
|
$adapter, |
|
63
|
|
|
$cache |
|
64
|
|
|
) |
|
65
|
|
|
); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function createMemoryAdapter(): MemoryAdapter |
|
69
|
|
|
{ |
|
70
|
|
|
return new MemoryAdapter(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
private function createLocalAdapter(Config $config): Local |
|
74
|
|
|
{ |
|
75
|
|
|
if (!defined('ROOT_DIR')) { |
|
76
|
|
|
throw new RootDirNotDefined('Root directory not defined'); |
|
77
|
|
|
} |
|
78
|
|
|
if (!$config->has('path') || !is_string($config->get('path'))) { |
|
79
|
|
|
throw new BadFlysystemConfigurationException('Path not set for local driver'); |
|
80
|
|
|
} |
|
81
|
|
|
return new Local(ROOT_DIR . '/' . $config->get('path')); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
private function createDropboxAdapter(Config $config): DropboxAdapter |
|
85
|
|
|
{ |
|
86
|
|
|
if (!$config->has('token') || !is_string($config->get('token'))) { |
|
87
|
|
|
throw new BadFlysystemConfigurationException('Token not set for Dropbox driver'); |
|
88
|
|
|
} |
|
89
|
|
|
$client = new Client($config->get('token')); |
|
90
|
|
|
return new DropboxAdapter($client); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
private function createAzureAdapter(Config $config): AzureBlobStorageAdapter |
|
94
|
|
|
{ |
|
95
|
|
|
if (!$config->has('container') || !is_string($config->get('container'))) { |
|
96
|
|
|
throw new BadFlysystemConfigurationException('Container not set for Azure driver'); |
|
97
|
|
|
} |
|
98
|
|
|
if (!$config->has('name') || !is_string($config->get('name'))) { |
|
99
|
|
|
throw new BadFlysystemConfigurationException('Account name not set for Azure driver'); |
|
100
|
|
|
} |
|
101
|
|
|
if (!$config->has('key') || !is_string($config->get('key'))) { |
|
102
|
|
|
throw new BadFlysystemConfigurationException('Account key not set for Azure driver'); |
|
103
|
|
|
} |
|
104
|
|
|
$endpoint = sprintf( |
|
105
|
|
|
'DefaultEndpointsProtocol=https;AccountName=%s;AccountKey=%s', |
|
106
|
|
|
$config->get('name'), |
|
107
|
|
|
$config->get('key') |
|
108
|
|
|
); |
|
109
|
|
|
$client = BlobRestProxy::createBlobService($endpoint); |
|
110
|
|
|
return new AzureBlobStorageAdapter($client, $config['container']); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
private function createS3Adapter(Config $config): AwsS3Adapter |
|
114
|
|
|
{ |
|
115
|
|
|
if (!$config->has('bucket')) { |
|
116
|
|
|
throw new BadFlysystemConfigurationException('Bucket not set for S3 driver'); |
|
117
|
|
|
} |
|
118
|
|
|
if (!$config->has('key')) { |
|
119
|
|
|
throw new BadFlysystemConfigurationException('Key not set for S3 driver'); |
|
120
|
|
|
} |
|
121
|
|
|
if (!$config->has('secret')) { |
|
122
|
|
|
throw new BadFlysystemConfigurationException('Secret not set for S3 driver'); |
|
123
|
|
|
} |
|
124
|
|
|
if (!$config->has('region')) { |
|
125
|
|
|
throw new BadFlysystemConfigurationException('Region not set for S3 driver'); |
|
126
|
|
|
} |
|
127
|
|
|
if (!$config->has('version')) { |
|
128
|
|
|
throw new BadFlysystemConfigurationException('Version not set for S3 driver'); |
|
129
|
|
|
} |
|
130
|
|
|
$client = new S3Client([ |
|
131
|
|
|
'credentials' => [ |
|
132
|
|
|
'key' => $config->get('key'), |
|
133
|
|
|
'secret' => $config->get('secret'), |
|
134
|
|
|
], |
|
135
|
|
|
'region' => $config->get('region'), |
|
136
|
|
|
'version' => $config->get('version'), |
|
137
|
|
|
]); |
|
138
|
|
|
return new AwsS3Adapter($client, $config->get('bucket')); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
private function createSftpAdapter(Config $config): SftpAdapter |
|
142
|
|
|
{ |
|
143
|
|
|
if (!$config->has('host')) { |
|
144
|
|
|
throw new BadFlysystemConfigurationException('Host not set for SFTP driver'); |
|
145
|
|
|
} |
|
146
|
|
|
if (!$config->has('username')) { |
|
147
|
|
|
throw new BadFlysystemConfigurationException('Username not set for SFTP driver'); |
|
148
|
|
|
} |
|
149
|
|
|
if (!$config->has('password') && !$config->has('privatekey')) { |
|
150
|
|
|
throw new BadFlysystemConfigurationException('Neither password nor private key set for SFTP driver'); |
|
151
|
|
|
} |
|
152
|
|
|
return new SftpAdapter([ |
|
153
|
|
|
'host' => $config->get('host'), |
|
154
|
|
|
'port' => $config->get('port') ?? 22, |
|
155
|
|
|
'username' => $config->get('username'), |
|
156
|
|
|
'password' => $config->get('password'), |
|
157
|
|
|
'privateKey' => $config->get('privatekey') ?? null, |
|
158
|
|
|
'root' => $config->get('root') ?? null, |
|
159
|
|
|
'timeout' => $config->get('timeout') ?? 10, |
|
160
|
|
|
]); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
private function createFtpAdapter(Config $config): FTPAdapter |
|
164
|
|
|
{ |
|
165
|
|
|
if (!$config->has('host')) { |
|
166
|
|
|
throw new BadFlysystemConfigurationException('Host not set for FTP driver'); |
|
167
|
|
|
} |
|
168
|
|
|
if (!$config->has('username')) { |
|
169
|
|
|
throw new BadFlysystemConfigurationException('Username not set for FTP driver'); |
|
170
|
|
|
} |
|
171
|
|
|
if (!$config->has('password') && !$config->has('privatekey')) { |
|
172
|
|
|
throw new BadFlysystemConfigurationException('Neither password nor private key set for FTP driver'); |
|
173
|
|
|
} |
|
174
|
|
|
return new FTPAdapter([ |
|
175
|
|
|
'host' => $config->get('host'), |
|
176
|
|
|
'port' => $config->get('port') ?? 22, |
|
177
|
|
|
'username' => $config->get('username'), |
|
178
|
|
|
'password' => $config->get('password'), |
|
179
|
|
|
'privateKey' => $config->get('privatekey') ?? null, |
|
180
|
|
|
'root' => $config->get('root') ?? null, |
|
181
|
|
|
'timeout' => $config->get('timeout') ?? 10, |
|
182
|
|
|
]); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|