Passed
Push — main ( 989c75...a2b15c )
by Sugeng
03:11
created

FtpComponent   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 10
eloc 51
c 2
b 0
f 0
dl 0
loc 152
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 6 4
A checksum() 0 7 2
A initAdapter() 0 19 4
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use diecoding\flysystem\traits\UrlGeneratorTrait;
6
use League\Flysystem\ChecksumAlgoIsNotSupported;
7
use League\Flysystem\ChecksumProvider;
8
use League\Flysystem\Config;
9
use League\Flysystem\Ftp\FtpAdapter;
10
use League\Flysystem\Ftp\FtpConnectionOptions;
11
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
12
use League\Flysystem\UrlGeneration\PublicUrlGenerator;
13
use League\Flysystem\UrlGeneration\TemporaryUrlGenerator;
14
use Yii;
15
16
/**
17
 * Interacting with an ftp filesystem
18
 * ! Notice
19
 * It's important to know this adapter does not fully comply with the adapter contract. The difference(s) is/are:
20
 * - Checksum setting or retrieving is not supported.
21
 * @see https://flysystem.thephpleague.com/docs/adapter/ftp/
22
 * 
23
 * ```php
24
 * 'components' => [
25
 *     'fs' => [
26
 *         'class' => \diecoding\flysystem\FtpComponent::class,
27
 *         'host' => 'hostname',
28
 *         'root' => '/root/path/', // or you can use @alias
29
 *         'username' => 'username',
30
 *         'password' => 'password',
31
 *         'port' => 21,
32
 *         'ssl' => false,
33
 *         'timeout' => 90,
34
 *         'utf8' => false,
35
 *         'passive' => true,
36
 *         'transferMode' => FTP_BINARY,
37
 *         'systemType' => null, // 'windows' or 'unix'
38
 *         'ignorePassiveAddress' => null, // true or false
39
 *         'timestampsOnUnixListingsEnabled' => false,
40
 *         'recurseManually' => true,
41
 *         'useRawListOptions' => null, // true or false
42
 *         'action' => '/site/file',
43
 *         'prefix' => '', 
44
 *     ],
45
 * ],
46
 * ```
47
 * 
48
 * @link      https://sugengsulistiyawan.my.id/
49
 * @author    Sugeng Sulistiyawan <[email protected]>
50
 * @copyright Copyright (c) 2023
51
 */
52
class FtpComponent extends AbstractComponent implements PublicUrlGenerator, TemporaryUrlGenerator, ChecksumProvider
53
{
54
    use UrlGeneratorTrait;
55
56
    /**
57
     * @var string
58
     */
59
    public $host;
60
61
    /**
62
     * @var string
63
     */
64
    public $root;
65
66
    /**
67
     * @var string
68
     */
69
    public $username;
70
71
    /**
72
     * @var string
73
     */
74
    public $password;
75
76
    /**
77
     * @var int
78
     */
79
    public $port;
80
81
    /**
82
     * @var bool
83
     */
84
    public $ssl;
85
86
    /**
87
     * @var int
88
     */
89
    public $timeout;
90
91
    /**
92
     * @var bool
93
     */
94
    public $utf8;
95
96
    /**
97
     * @var bool
98
     */
99
    public $passive;
100
101
    /**
102
     * @var int
103
     */
104
    public $transferMode;
105
106
    /**
107
     * @var string `windows` or `unix`
108
     */
109
    public $systemType;
110
111
    /**
112
     * @var bool
113
     */
114
    public $ignorePassiveAddress;
115
116
    /**
117
     * @var bool
118
     */
119
    public $timestampsOnUnixListingsEnabled;
120
121
    /**
122
     * @var bool
123
     */
124
    public $recurseManually;
125
126
    /**
127
     * @var bool
128
     */
129
    public $useRawListOptions;
130
131
    /**
132
     * @var string
133
     */
134
    public $passphrase;
135
136
    /**
137
     * @var array
138
     */
139
    protected $_availableOptions = [
140
        'host',
141
        'root',
142
        'username',
143
        'password',
144
        'port',
145
        'ssl',
146
        'timeout',
147
        'utf8',
148
        'passive',
149
        'transferMode',
150
        'systemType',
151
        'ignorePassiveAddress',
152
        'timestampsOnUnixListingsEnabled',
153
        'recurseManually',
154
        'useRawListOptions',
155
    ];
156
157
    /**
158
     * @var FtpConnectionOptions
159
     */
160
    protected $_connectionOptions;
161
162
    /**
163
     * @inheritdoc
164
     */
165
    public function init()
166
    {
167
        $this->passphrase = $this->passphrase ?: ($this->password ?: ($this->username ?: Yii::$app->id));
168
        $this->initEncrypter($this->passphrase);
169
170
        parent::init();
171
    }
172
173
    /**
174
     * @return FtpAdapter|PathPrefixedAdapter
175
     */
176
    protected function initAdapter()
177
    {
178
        $this->root = (string) Yii::getAlias($this->root);
179
180
        $options = [];
181
        foreach ($this->_availableOptions as $property) {
182
            if ($this->$property !== null) {
183
                $options[$property] = $this->$property;
184
            }
185
        }
186
187
        $this->_connectionOptions = FtpConnectionOptions::fromArray($options);
188
189
        $adapter = new FtpAdapter($this->_connectionOptions);
190
        if ($this->prefix) {
191
            $adapter = new PathPrefixedAdapter($adapter, $this->prefix);
192
        }
193
194
        return $adapter;
195
    }
196
197
    public function checksum(string $path, Config $config): string
198
    {
199
        if ($this->debug) {
200
            throw new ChecksumAlgoIsNotSupported('FtpComponent does not support this operation.');
201
        }
202
203
        return '';
204
    }
205
}