FileStorage   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 52
c 1
b 0
f 0
dl 0
loc 143
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A registerAdapter() 0 3 1
A registerAdapters() 0 51 3
A __construct() 0 5 1
A filesystem() 0 32 6
1
<?php
2
3
/**
4
 * FileStorage.php
5
 *
6
 * Manage storage systems for uploaded files.
7
 *
8
 * @package jaxon-upload
9
 * @author Thierry Feuzeu <[email protected]>
10
 * @copyright 2022 Thierry Feuzeu <[email protected]>
11
 * @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License
12
 * @link https://github.com/jaxon-php/jaxon-core
13
 */
14
15
namespace Jaxon\Upload\Manager;
16
17
use Jaxon\App\Config\ConfigManager;
18
use Jaxon\App\I18n\Translator;
19
use Jaxon\Exception\RequestException;
20
use League\Flysystem\Filesystem;
21
use League\Flysystem\Local\LocalFilesystemAdapter;
22
23
use Closure;
24
25
use function call_user_func;
26
use function is_string;
27
use function trim;
28
29
class FileStorage
30
{
31
    /**
32
     * @var ConfigManager
33
     */
34
    protected $xConfigManager;
35
36
    /**
37
     * @var Translator
38
     */
39
    protected $xTranslator;
40
41
    /**
42
     * @var array
43
     */
44
    protected $aAdapters = [];
45
46
    /**
47
     * @var array
48
     */
49
    protected $aFilesystems = [];
50
51
    /**
52
     * The constructor
53
     *
54
     * @param ConfigManager $xConfigManager
55
     * @param Translator $xTranslator
56
     */
57
    public function __construct(ConfigManager $xConfigManager, Translator $xTranslator)
58
    {
59
        $this->xConfigManager = $xConfigManager;
60
        $this->xTranslator = $xTranslator;
61
        $this->registerAdapters();
62
    }
63
64
    /**
65
     * @param string $sStorage
66
     * @param Closure $cFactory
67
     *
68
     * @return void
69
     */
70
    public function registerAdapter(string $sStorage, Closure $cFactory)
71
    {
72
        $this->aAdapters[$sStorage] = $cFactory;
73
    }
74
75
    /**
76
     * Register the file storage adapters
77
     *
78
     * @return void
79
     */
80
    private function registerAdapters()
81
    {
82
        // Local file system adapter
83
        $this->registerAdapter('local', function(string $sRootDir, $xOptions) {
84
            return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) :
85
                new LocalFilesystemAdapter($sRootDir, $xOptions);
86
        });
87
88
        // AWS S3 file system adapter
89
        $this->registerAdapter('aws-s3', function(string $sRootDir, array $aOptions) {
90
            /** @var \Aws\S3\S3ClientInterface $client */
91
            $client = new \Aws\S3\S3Client($aOptions['client'] ?? []);
0 ignored issues
show
Bug introduced by
The type Aws\S3\S3Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
92
            return new \League\Flysystem\AwsS3V3\AwsS3V3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\AwsS3V3\AwsS3V3Adapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
93
        });
94
95
        // Async AWS S3 file system adapter
96
        $this->registerAdapter('async-aws-s3', function(string $sRootDir, array $aOptions) {
97
            $client = isset($aOptions['client']) ? new \AsyncAws\S3\S3Client($aOptions['client']) : new \AsyncAws\S3\S3Client();
0 ignored issues
show
Bug introduced by
The type AsyncAws\S3\S3Client was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
98
            return new \League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter($client, $aOptions['bucket'] ?? '', $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\AsyncAwsS3\AsyncAwsS3Adapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
99
        });
100
101
        // Google Cloud file system adapter
102
        $this->registerAdapter('google-cloud', function(string $sRootDir, array $aOptions) {
103
            $storageClient = new \Google\Cloud\Storage\StorageClient($aOptions['client'] ?? []);
0 ignored issues
show
Bug introduced by
The type Google\Cloud\Storage\StorageClient was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
104
            $bucket = $storageClient->bucket($aOptions['bucket'] ?? '');
105
            return new \League\Flysystem\AzureBlobStorage\GoogleCloudStorageAdapter($bucket, $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\AzureBl...ogleCloudStorageAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
106
        });
107
108
        // Microsoft Azure file system adapter
109
        $this->registerAdapter('azure-blob', function(string $sRootDir, array $aOptions) {
110
            $client = \MicrosoftAzure\Storage\Blob\BlobRestProxy::createBlobService($aOptions['dsn']);
0 ignored issues
show
Bug introduced by
The type MicrosoftAzure\Storage\Blob\BlobRestProxy was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
111
            return new \League\Flysystem\AzureBlobStorage\AzureBlobStorageAdapter($client, $aOptions['container'], $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\AzureBl...AzureBlobStorageAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
112
        });
113
114
        // FTP file system adapter
115
        $this->registerAdapter('ftp', function(string $sRootDir, array $aOptions) {
116
            $aOptions['root'] = $sRootDir;
117
            $xOptions = \League\Flysystem\Ftp\FtpConnectionOptions::fromArray($aOptions);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\Ftp\FtpConnectionOptions was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
118
            return new \League\Flysystem\Ftp\FtpAdapter($xOptions);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\Ftp\FtpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
119
        });
120
121
        // SFTP V2 file system adapter
122
        $this->registerAdapter('sftp-v2', function(string $sRootDir, array $aOptions) {
123
            $provider = new \League\Flysystem\PhpseclibV2\SftpConnectionProvider(...$aOptions);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\Phpsecl...\SftpConnectionProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
124
            return new \League\Flysystem\PhpseclibV2\SftpAdapter($provider, $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\PhpseclibV2\SftpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
125
        });
126
127
        // SFTP V3 file system adapter
128
        $this->registerAdapter('sftp-v3', function(string $sRootDir, array $aOptions) {
129
            $provider = new \League\Flysystem\PhpseclibV3\SftpConnectionProvider(...$aOptions);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\Phpsecl...\SftpConnectionProvider was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
130
            return new \League\Flysystem\PhpseclibV3\SftpAdapter($provider, $sRootDir);
0 ignored issues
show
Bug introduced by
The type League\Flysystem\PhpseclibV3\SftpAdapter was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
131
        });
132
    }
133
134
    /**
135
     * @param string $sField
136
     *
137
     * @return Filesystem
138
     * @throws RequestException
139
     */
140
    public function filesystem(string $sField = ''): Filesystem
141
    {
142
        $sField = trim($sField);
143
        if(isset($this->aFilesystems[$sField]))
144
        {
145
            return $this->aFilesystems[$sField];
146
        }
147
148
        // Default upload dir
149
        $sStorage = $this->xConfigManager->getOption('upload.default.storage', 'local');
150
        $sRootDir = $this->xConfigManager->getOption('upload.default.dir', '');
151
        $aOptions = $this->xConfigManager->getOption('upload.default.options');
152
        $sConfigKey = "upload.files.$sField";
153
        if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey))
154
        {
155
            $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage);
156
            $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir);
157
            $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions);
158
        }
159
160
        if(!is_string($sRootDir))
161
        {
162
            throw new RequestException($this->xTranslator->trans('errors.upload.dir'));
163
        }
164
        if(!isset($this->aAdapters[$sStorage]))
165
        {
166
            throw new RequestException($this->xTranslator->trans('errors.upload.adapter'));
167
        }
168
169
        $xAdapter = call_user_func($this->aAdapters[$sStorage], $sRootDir, $aOptions);
170
        $this->aFilesystems[$sField] = new Filesystem($xAdapter);
171
        return $this->aFilesystems[$sField];
172
    }
173
}
174