Passed
Push — main ( e654d0...9e81b9 )
by Thierry
02:07
created

FileStorage::registerAdapters()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 56
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 28
nc 1
nop 0
dl 0
loc 56
rs 9.472
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * FileStorage.php
5
 *
6
 * Manage storage systems for uploaded files.
7
 *
8
 * @package jaxon-core
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
    }
62
63
    /**
64
     * @param string $sStorage
65
     * @param Closure $cFactory
66
     *
67
     * @return void
68
     */
69
    public function registerAdapter(string $sStorage, Closure $cFactory)
70
    {
71
        $this->aAdapters[$sStorage] = $cFactory;
72
    }
73
74
    /**
75
     * Register the file storage adapters
76
     *
77
     * @return void
78
     */
79
    public function registerAdapters()
80
    {
81
        // Local file system adapter
82
        $this->registerAdapter('local', function(string $sRootDir, $xOptions) {
83
            return empty($xOptions) ? new LocalFilesystemAdapter($sRootDir) :
84
                new LocalFilesystemAdapter($sRootDir, $xOptions);
85
        });
86
87
        // In memory file system adapter
88
        $this->registerAdapter('memory', function() {
89
            return new \League\Flysystem\InMemory\InMemoryFilesystemAdapter();
0 ignored issues
show
Bug introduced by
The type League\Flysystem\InMemor...MemoryFilesystemAdapter 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...
90
        });
91
92
        // AWS S3 file system adapter
93
        $this->registerAdapter('aws-s3', function(string $sRootDir, array $aOptions) {
94
            /** @var \Aws\S3\S3ClientInterface $client */
95
            $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...
96
            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...
97
        });
98
99
        // Async AWS S3 file system adapter
100
        $this->registerAdapter('async-aws-s3', function(string $sRootDir, array $aOptions) {
101
            $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...
102
            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...
103
        });
104
105
        // Google Cloud file system adapter
106
        $this->registerAdapter('google-cloud', function(string $sRootDir, array $aOptions) {
107
            $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...
108
            $bucket = $storageClient->bucket($aOptions['bucket'] ?? '');
109
            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...
110
        });
111
112
        // Microsoft Azure file system adapter
113
        $this->registerAdapter('azure-blob', function(string $sRootDir, array $aOptions) {
114
            $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...
115
            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...
116
        });
117
118
        // FTP file system adapter
119
        $this->registerAdapter('ftp', function(string $sRootDir, array $aOptions) {
120
            $aOptions['root'] = $sRootDir;
121
            $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...
122
            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...
123
        });
124
125
        // SFTP V2 file system adapter
126
        $this->registerAdapter('sftp-v2', function(string $sRootDir, array $aOptions) {
127
            $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...
128
            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...
129
        });
130
131
        // SFTP V3 file system adapter
132
        $this->registerAdapter('sftp-v3', function(string $sRootDir, array $aOptions) {
133
            $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...
134
            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...
135
        });
136
    }
137
138
    /**
139
     * @param string $sField
140
     *
141
     * @return Filesystem
142
     * @throws RequestException
143
     */
144
    public function filesystem(string $sField = ''): Filesystem
145
    {
146
        $sField = trim($sField);
147
        if(isset($this->aFilesystems[$sField]))
148
        {
149
            return $this->aFilesystems[$sField];
150
        }
151
152
        // Default upload dir
153
        $sStorage = $this->xConfigManager->getOption('upload.default.storage', 'local');
154
        $sRootDir = $this->xConfigManager->getOption('upload.default.dir', '');
155
        $aOptions = $this->xConfigManager->getOption('upload.default.options');
156
        $sConfigKey = "upload.files.$sField";
157
        if($sField !== '' && $this->xConfigManager->hasOption($sConfigKey))
158
        {
159
            $sStorage = $this->xConfigManager->getOption("$sConfigKey.storage", $sStorage);
160
            $sRootDir = $this->xConfigManager->getOption("$sConfigKey.dir", $sRootDir);
161
            $aOptions = $this->xConfigManager->getOption("$sConfigKey.options", $aOptions);
162
        }
163
164
        if(!is_string($sRootDir))
165
        {
166
            throw new RequestException($this->xTranslator->trans('errors.upload.dir'));
167
        }
168
        if(!isset($this->aAdapters[$sStorage]))
169
        {
170
            throw new RequestException($this->xTranslator->trans('errors.upload.adapter'));
171
        }
172
173
        $this->aFilesystems[$sField] = new Filesystem(call_user_func($this->aAdapters[$sStorage], $sRootDir, $aOptions));
174
        return $this->aFilesystems[$sField];
175
    }
176
}
177