Total Complexity | 40 |
Total Lines | 305 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like FTP often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FTP, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
19 | class FTP |
||
20 | { |
||
21 | /** |
||
22 | * Connection. |
||
23 | * |
||
24 | * @since 1.0.0 |
||
25 | * |
||
26 | * @var resource |
||
27 | */ |
||
28 | private $connection; |
||
29 | |||
30 | /** |
||
31 | * connectionString. |
||
32 | * |
||
33 | * @since 1.0.0 |
||
34 | * |
||
35 | * @var resource |
||
36 | */ |
||
37 | private $connectionString; |
||
38 | |||
39 | /** |
||
40 | * Instantiate the FTP object. |
||
41 | * |
||
42 | * @param (string) $host server host |
||
43 | * @param (string) $user username |
||
44 | * @param (string) $pass password |
||
45 | * @param (string) $secured ftp or sftp |
||
46 | * |
||
47 | * @since 1.0.0 |
||
48 | */ |
||
49 | public function __construct($host, $user, $pass, $secured) |
||
50 | { |
||
51 | if ($secured === false) { |
||
|
|||
52 | $conn = ftp_connect($host); |
||
53 | $login_result = ftp_login($conn, $user, $pass); |
||
54 | $this->connection = $conn; |
||
55 | $this->connectionString = 'ftp://'.$user.':'.$pass.'@'.$host; |
||
56 | } elseif ($secured === true) { |
||
57 | $conn = ftp_ssl_connect($host); |
||
58 | $login_result = ftp_login($conn, $user, $pass); |
||
59 | $this->connection = $conn; |
||
60 | $this->connectionString = 'sftp://'.$user.':'.$pass.'@'.$host; |
||
61 | } else { |
||
62 | $this->connection = null; |
||
63 | $this->connectionString = null; |
||
64 | } |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * get the connection. |
||
69 | * |
||
70 | * @since 1.0.0 |
||
71 | * |
||
72 | * @return resource |
||
73 | */ |
||
74 | public function getConnection() |
||
75 | { |
||
76 | return $this->connection; |
||
77 | } |
||
78 | |||
79 | /** |
||
80 | * check whether the ftp is connected. |
||
81 | * |
||
82 | * @since 1.0.0 |
||
83 | * |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isConnected() |
||
87 | { |
||
88 | return (is_resource($this->getConnection())) ? true : false; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * get the list of files. |
||
93 | * |
||
94 | * @param (string) $dir directory |
||
95 | * |
||
96 | * @since 1.0.0 |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function ftpFiles($dir) |
||
101 | { |
||
102 | return ($this->isConnected() === true) ? ftp_nlist($this->getConnection(), $dir) : false; |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * get the current working directory. |
||
107 | * |
||
108 | * @since 1.0.0 |
||
109 | * |
||
110 | * @return mixed |
||
111 | */ |
||
112 | public function pwd() |
||
113 | { |
||
114 | return ($this->isConnected() === true) ? ftp_pwd($this->getConnection()) : false; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Change directories. |
||
119 | * |
||
120 | * @param (string) $dir directory |
||
121 | * @param (string) $new naw name |
||
122 | * |
||
123 | * @since 1.0.0 |
||
124 | * |
||
125 | * @return bool |
||
126 | */ |
||
127 | public function chdir($dir) |
||
128 | { |
||
129 | return ($this->isConnected() === true) ? ftp_chdir($this->getConnection(), $dir) : false; |
||
130 | } |
||
131 | |||
132 | /** |
||
133 | * Make directory. |
||
134 | * |
||
135 | * @param (string) $dir directory name |
||
136 | * |
||
137 | * @since 1.0.0 |
||
138 | * |
||
139 | * @return bool |
||
140 | */ |
||
141 | public function mkdir($dir) |
||
142 | { |
||
143 | return ($this->isConnected() === true) ? ftp_mkdir($this->getConnection(), $dir) : false; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * Make nested sub-directories. |
||
148 | * |
||
149 | * @param (array) $dirs |
||
150 | * |
||
151 | * @since 1.0.0 |
||
152 | * |
||
153 | * @return object |
||
154 | */ |
||
155 | public function mkdirs($dirs) |
||
156 | { |
||
157 | if (substr($dirs, 0, 1) == '/') { |
||
158 | $dirs = substr($dirs, 1); |
||
159 | } |
||
160 | if (substr($dirs, -1) == '/') { |
||
161 | $dirs = substr($dirs, 0, -1); |
||
162 | } |
||
163 | $dirs = explode('/', $dirs); |
||
164 | $curDir = $this->connectionString; |
||
165 | foreach ($dirs as $dir) { |
||
166 | $curDir .= '/'.$dir; |
||
167 | if (!is_dir($curDir)) { |
||
168 | $this->mkdir($dir); |
||
169 | } |
||
170 | $this->chdir($dir); |
||
171 | } |
||
172 | |||
173 | return $this; |
||
174 | } |
||
175 | |||
176 | /** |
||
177 | * Remove directory. |
||
178 | * |
||
179 | * @param (string) $dir directory |
||
180 | * |
||
181 | * @since 1.0.0 |
||
182 | * |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function rmdir($dir) |
||
186 | { |
||
187 | return ($this->isConnected() === true) ? ftp_rmdir($this->getConnection(), $dir) : false; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * Check if file exists. |
||
192 | * |
||
193 | * @param (string) $file |
||
194 | * |
||
195 | * @since 1.0.0 |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | public function fileExists($file) |
||
200 | { |
||
201 | return ($this->isConnected() === true) ? ftp_rmdir($this->connectionString.$file) : false; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Check is the dir is exists. |
||
206 | * |
||
207 | * @param (string) $dir directory |
||
208 | * |
||
209 | * @since 1.0.0 |
||
210 | * |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function dirExists($dir) |
||
214 | { |
||
215 | return ($this->isConnected() === true) ? ftp_rmdir($this->connectionString.$dir) : false; |
||
216 | } |
||
217 | |||
218 | /** |
||
219 | * Get the file. |
||
220 | * |
||
221 | * @param (mixed) $local local |
||
222 | * @param (mixed) $remote remote |
||
223 | * @param (mixed) $mode mode |
||
224 | * |
||
225 | * @since 1.0.0 |
||
226 | * |
||
227 | * @return bool |
||
228 | */ |
||
229 | public function get($local, $remote, $mode = FTP_BINARY) |
||
230 | { |
||
231 | return ($this->isConnected() === true) ? ftp_get($this->getConnection(), $local, $remote, $mode) : false; |
||
232 | } |
||
233 | |||
234 | /** |
||
235 | * Rename file. |
||
236 | * |
||
237 | * @param (string) $old old |
||
238 | * @param (string) $new naw name |
||
239 | * |
||
240 | * @since 1.0.0 |
||
241 | * |
||
242 | * @return bool |
||
243 | */ |
||
244 | public function rename($old, $new) |
||
245 | { |
||
246 | return ($this->isConnected() === true) ? ftp_rename($this->getConnection(), $old, $new) : false; |
||
247 | } |
||
248 | |||
249 | /** |
||
250 | * Change permission. |
||
251 | * |
||
252 | * @param (string) $file file |
||
253 | * @param (mixed) $mode mode |
||
254 | * |
||
255 | * @since 1.0.0 |
||
256 | * |
||
257 | * @return bool |
||
258 | */ |
||
259 | public function chmod($file, $mode) |
||
260 | { |
||
261 | return ($this->isConnected() === true) ? ftp_chmod($this->getConnection(), $mode, $file) : false; |
||
262 | } |
||
263 | |||
264 | /** |
||
265 | * Delete the files. |
||
266 | * |
||
267 | * @param (string) $file file you want to delete |
||
268 | * |
||
269 | * @since 1.0.0 |
||
270 | * |
||
271 | * @return bool |
||
272 | */ |
||
273 | public function delete($file) |
||
274 | { |
||
275 | return ($this->isConnected() === true) ? ftp_delete($this->getConnection(), $file) : false; |
||
276 | } |
||
277 | |||
278 | /** |
||
279 | * Switch the passive mod. |
||
280 | * |
||
281 | * @param (bool) $flag flag |
||
282 | * |
||
283 | * @since 1.0.0 |
||
284 | * |
||
285 | * @return bool |
||
286 | */ |
||
287 | public function pasv($flag = true) |
||
288 | { |
||
289 | return ($this->isConnected() === true) ? ftp_pasv($this->getConnection(), $flag) : false; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * Close the FTP connection. |
||
294 | * |
||
295 | * @since 1.0.0 |
||
296 | * |
||
297 | * @return void |
||
298 | */ |
||
299 | public function disconnect() |
||
300 | { |
||
301 | if ($this->isConnected()) { |
||
302 | ftp_close($this->connection); |
||
303 | } |
||
304 | } |
||
305 | |||
306 | /** |
||
307 | * Upload the files. |
||
308 | * |
||
309 | * @param (array) $files number of files you want to uplaod |
||
310 | * @param (string) $root Server root directory or sub |
||
311 | * |
||
312 | * @since 1.0.0 |
||
313 | * |
||
314 | * @return mixed |
||
315 | */ |
||
316 | public function put($files, $root = 'public_html') |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 |