Passed
Push — main ( 99c3ef...81ef04 )
by Sugeng
02:45
created

FtpComponent::initAdapter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 29
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

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