Passed
Push — main ( e9e963...ea567a )
by Sugeng
05:33 queued 02:41
created

FtpComponent::init()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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