Completed
Push — master ( e7a951...39e8d7 )
by WEBEWEB
05:07
created

FTPClient::close()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 4
nc 2
nop 0
1
<?php
2
3
/**
4
 * This file is part of the core-library package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Library\Core\Net;
13
14
use WBW\Library\Core\IO\IOException;
15
16
/**
17
 * FTP client.
18
 *
19
 * @author webeweb <https://github.com/webeweb/>
20
 * @package WBW\Library\Core\Net
21
 */
22
class FTPClient {
23
24
    /**
25
     * Authenticator.
26
     *
27
     * @var Authenticator
28
     */
29
    private $authenticator;
30
31
    /**
32
     * Connection.
33
     *
34
     * @var ressource.
35
     */
36
    private $connection;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param Authenticator $authenticator The authenticator.
42
     */
43
    public function __construct(Authenticator $authenticator) {
44
        $this->authenticator = $authenticator;
45
    }
46
47
    /**
48
     * Closes this FTP connection.
49
     *
50
     * @return FTPClient Returns this FTP client.
51
     * @throws IOException Throws an IO exception if an I/O error occurs.
52
     */
53
    public function close() {
54
        if (false === ftp_close($this->connection)) {
55
            throw new IOException(sprintf("ftp://%s disconnection failed", $this->authenticator->getHost()));
56
        }
57
        return $this;
58
    }
59
60
    /**
61
     * Opens this FTP connection.
62
     *
63
     * @param integer $timeout The timeout.
64
     * @return FTPClient Returns this FTP client.
65
     * @throws IOException Throws an IO exception if an I/O error occurs.
66
     */
67
    public function connect($timeout = 90) {
68
        if (false === ($this->connection = ftp_connect($this->authenticator->getHost(), $this->authenticator->getPort(), $timeout))) {
69
            throw new IOException(sprintf("ftp://%s connection failed", $this->authenticator->getHost()));
70
        }
71
        return $this;
72
    }
73
74
    /**
75
     * Get the authenticator.
76
     *
77
     * @return Auhtenticator Returns the authenticator.
78
     */
79
    public function getAuthenticator() {
80
        return $this->authenticator;
81
    }
82
83
    /**
84
     * Logs in to this FTP connection.
85
     *
86
     * @return FTPClient Returns this FTP client.
87
     * @throws IOException Throws an IO exception if an I/O error occurs.
88
     */
89
    public function login() {
90
        if (false === ftp_login($this->connection, $this->authenticator->getPasswordAuthentication()->getUsername(), $this->authenticator->getPasswordAuthentication()->getPassword())) {
91
            throw new IOException(sprintf("ftp://%s:%s@%s failed", $this->authenticator->getHost(), $this->authenticator->getPasswordAuthentication()->getUsername(), $this->authenticator->getPasswordAuthentication()->getPassword()));
92
        }
93
        return $this;
94
    }
95
96
    /**
97
     * Creates a directory.
98
     *
99
     * @param string $directory The directory.
100
     * @return FTPClient Returns this FTP client.
101
     * @throws IOException Throws an IO exception if an I/O error occurs.
102
     */
103
    public function mkdir($directory) {
104
        if (false === ftp_mkdir($this->connection, $directory)) {
105
            throw new IOException(sprintf("mkdir %s failed", $directory));
106
        }
107
        return $this;
108
    }
109
110
    /**
111
     * Set the authenticator.
112
     *
113
     * @param Authenticator $authenticator The authenticator.
114
     * @return FTPClient Returns this FTP client.
115
     */
116
    public function setAuthenticator(Authenticator $authenticator) {
117
        $this->authenticator = $authenticator;
118
        return $this;
119
    }
120
121
}
122