Test Failed
Push — main ( 1f6f9b...f1a6e0 )
by Sugeng
03:22
created

FTPComponent   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 50
c 1
b 0
f 0
dl 0
loc 138
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 15 4
A initAdapter() 0 31 3
1
<?php
2
3
namespace diecoding\flysystem;
4
5
use DateTimeInterface;
6
use diecoding\flysystem\traits\UrlGeneratorTrait;
7
use League\Flysystem\Ftp\FtpAdapter;
8
use League\Flysystem\Ftp\FtpConnectionOptions;
9
use League\Flysystem\Local\LocalFilesystemAdapter;
10
use Yii;
11
use yii\base\InvalidConfigException;
12
use yii\helpers\Json;
13
use yii\helpers\StringHelper;
14
use yii\helpers\Url;
15
16
/**
17
 * Class FTPComponent
18
 * 
19
 * ```php
20
 * 'components' => [
21
 *     'fs' => [
22
 *         'class' => \diecoding\flysystem\FTPComponent::class,
23
 *         'host' => 'hostname',
24
 *         'root' => '/root/path/', // or you can use @alias
25
 *         'username' => 'username',
26
 *         'password' => 'password',
27
 *         'port' => 21,
28
 *         'ssl' => false,
29
 *         'timeout' => 90,
30
 *         'utf8' => false,
31
 *         'passive' => true,
32
 *         'transferMode' => FTP_BINARY,
33
 *         'systemType' => null, // 'windows' or 'unix'
34
 *         'ignorePassiveAddress' => null, // true or false
35
 *         'timestampsOnUnixListingsEnabled' => false,
36
 *         'recurseManually' => true,
37
 *         'useRawListOptions' => null, // true or false
38
 *         'action' => '/site/file',
39
 *         'prefix' => '', 
40
 *     ],
41
 * ],
42
 * ```
43
 * 
44
 * @package diecoding\flysystem
45
 * 
46
 * @link      https://sugengsulistiyawan.my.id/
47
 * @author    Sugeng Sulistiyawan <[email protected]>
48
 * @copyright Copyright (c) 2023
49
 */
50
class FTPComponent extends AbstractComponent
51
{
52
    use UrlGeneratorTrait;
53
54
    /**
55
     * @var string
56
     */
57
    public $host;
58
59
    /**
60
     * @var string
61
     */
62
    public $root;
63
64
    /**
65
     * @var string
66
     */
67
    public $username;
68
69
    /**
70
     * @var string
71
     */
72
    public $password;
73
74
    /**
75
     * @var int
76
     */
77
    public $port = 21;
78
79
    /**
80
     * @var bool
81
     */
82
    public $ssl = false;
83
84
    /**
85
     * @var int
86
     */
87
    public $timeout = 90;
88
89
    /**
90
     * @var bool
91
     */
92
    public $utf8 = false;
93
94
    /**
95
     * @var bool
96
     */
97
    public $passive = true;
98
99
    /**
100
     * @var int
101
     */
102
    public $transferMode = FTP_BINARY;
103
104
    /**
105
     * @var string|null `windows` or `unix`
106
     */
107
    public $systemType = null;
108
109
    /**
110
     * @var bool|null
111
     */
112
    public $ignorePassiveAddress = null;
113
114
    /**
115
     * @var bool
116
     */
117
    public $timestampsOnUnixListingsEnabled = false;
118
119
    /**
120
     * @var bool
121
     */
122
    public $recurseManually = false;
123
124
    /**
125
     * @var bool|null
126
     */
127
    public $useRawListOptions = null;
128
129
    /**
130
     * @var FtpConnectionOptions
131
     */
132
    protected $_connectionOptions;
133
134
    /**
135
     * @inheritdoc
136
     */
137
    public function init()
138
    {
139
        if (empty($this->host)) {
140
            throw new InvalidConfigException('The "host" property must be set.');
141
        }
142
        if (empty($this->username)) {
143
            throw new InvalidConfigException('The "username" property must be set.');
144
        }
145
        if (empty($this->password)) {
146
            throw new InvalidConfigException('The "password" property must be set.');
147
        }
148
149
        $this->setEncrypter($this->password, $this->username);
150
151
        parent::init();
152
    }
153
154
    /**
155
     * @return LocalFilesystemAdapter
156
     */
157
    protected function initAdapter()
158
    {
159
        $this->root = (string) Yii::getAlias($this->root);
160
        $this->root = $this->normalizePath($this->root . '/' . $this->prefix);
161
162
        $options = [];
163
        foreach ([
164
            'host',
165
            'root',
166
            'username',
167
            'password',
168
            'port',
169
            'ssl',
170
            'timeout',
171
            'utf8',
172
            'passive',
173
            'transferMode',
174
            'systemType',
175
            'ignorePassiveAddress',
176
            'timestampsOnUnixListingsEnabled',
177
            'recurseManually',
178
            'useRawListOptions',
179
        ] as $property) {
180
            if ($this->$property !== null) {
181
                $options[$property] = $this->$property;
182
            }
183
        }
184
185
        $this->_connectionOptions = FtpConnectionOptions::fromArray($options);
186
187
        return new FtpAdapter($this->_connectionOptions);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new League\Flysys...is->_connectionOptions) returns the type League\Flysystem\Ftp\FtpAdapter which is incompatible with the documented return type League\Flysystem\Local\LocalFilesystemAdapter.
Loading history...
188
    }
189
}
190