Completed
Push — develop ( 36acaf...ea3134 )
by Maarten
17:06 queued 14:59
created

FtpService::getTimeout()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
/**
3
 * This file is part of the Amplexor\XConnect library
4
 *
5
 * @license http://opensource.org/licenses/MIT
6
 * @link https://github.com/amplexor-drupal/xconnect/
7
 * @version 1.0.0
8
 * @package Amplexor.XConnect
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Amplexor\XConnect\Service;
15
16
use Amplexor\XConnect\Request\File\FileInterface;
17
18
/**
19
 * FTP Service.
20
 */
21
class FtpService extends ServiceAbstract
22
{
23
    /**
24
     * The FTP transfer mode.
25
     *
26
     * We set this value = 2, thats the same as the FTP_BINARY constant.
27
     *
28
     * @var int
29
     */
30
    const TRANSFER_MODE = 2;
31
32
    /**
33
     * Service connection configuration.
34
     *
35
     * @var array
36
     */
37
    private $config = array(
38
        'hostname' => '',
39
        'port' => 21,
40
        'timeout' => 90,
41
        'username' => '',
42
        'password' => '',
43
    );
44
45
    /**
46
     * The FTP connection resource.
47
     *
48
     * @var resource
49
     */
50
    private $ftp;
51
52
    /**
53
     * Class constructor.
54
     *
55
     * Create a new service by passing the configuration:
56
     * - hostname : The hostname of the FTP service.
57
     * - port : The FRP port.
58
     * - username : The username to connect to the FTP service.
59
     * - password : The password to connect to the FTP service.
60
     *
61
     * Optional configuration:
62
     * - directory_send : The remote directory to store the request file in.
63
     * - directory_send_processed : The remote directory where the processed
64
     *   request files are stored.
65
     * - directory_receive : The remote directory where the translated files are
66
     *   stored to be picked up.
67
     * - directory_receive_processed : The repome directory where to put the
68
     *   translation files that are successfully processed by the local system.
69
     *
70
     * @param array $config
71 81
     */
72
    public function __construct(array $config)
73 81
    {
74 81
        parent::__construct($config);
75 81
        $this->setConfig($config);
76
    }
77
78
    /**
79
     * @inheritDoc
80 18
     */
81
    public function send(FileInterface $file)
82 18
    {
83 12
        $connection = $this->getConnection();
84 12
        $from = $file->getPath();
85 12
        $to = $this->getDirectorySend() . '/' . $file->getFileName();
86
        $result = ftp_put($connection, $to, $from, self::TRANSFER_MODE);
87 12
88 3
        if (!$result) {
89 3
            throw new ServiceException(
90 3
                sprintf(
91 3
                    'File "%s" could not be uploaded to "%s".',
92
                    $from,
93 3
                    $to
94 3
                )
95
            );
96
        }
97 9
98
        return $result;
99
    }
100
101
    /**
102
     * @inheritDoc
103 6
     */
104
    public function scan()
105 6
    {
106 6
        $connection = $this->getConnection();
107
        $directory = $this->getDirectoryReceive();
108
109 6
        // Get the full file list.
110
        $files = ftp_nlist($connection, $directory);
111
112 6
        // We only want files, no directories.
113 6
        foreach ($files as $key => $file) {
114 6
            if (ftp_size($connection, $directory . '/' . $file) < 0) {
115 6
                unset($files[$key]);
116
                continue;
117 6
            }
118
        }
119 6
120
        return array_values($files);
121
    }
122
123
    /**
124
     * @inheritDoc
125 9
     */
126
    public function receive($fileName, $directory)
127 9
    {
128 9
        $connection = $this->getConnection();
129 9
        $to = $directory . '/' . $fileName;
130
        $from = $this->getDirectoryReceive() . '/' . $fileName;
131 9
132
        $result = ftp_get($connection, $to, $from, self::TRANSFER_MODE);
133 9
134 3
        if (!$result) {
135 3
            throw new ServiceException(
136 3
                sprintf(
137 3
                    'File "%s" could not be downloaded to "%s".',
138
                    $from,
139 3
                    $to
140 3
                )
141
            );
142
        }
143 6
144
        return $to;
145
    }
146
147
    /**
148
     * @inheritDoc
149 9
     */
150
    public function processed($fileName)
151 9
    {
152
        $connection = $this->getConnection();
153 9
154 9
        $from = $this->getDirectoryReceive() . '/' . $fileName;
155
        $to = $this->getDirectoryReceiveProcessed() . '/' . $fileName;
156 9
157
        $result = ftp_rename($connection, $from, $to);
158 9
159 3
        if (!$result) {
160 3
            throw new ServiceException(
161 3
                sprintf(
162 3
                    'File "%s" could not be moved to "%s".',
163
                    $from,
164 3
                    $to
165 3
                )
166
            );
167
        }
168 6
169
        return $result;
170
    }
171
172
    /**
173
     * Get the connection.
174
     *
175
     * @return resource
176
     *   A FTP connection resource.
177 42
     */
178
    protected function getConnection()
179 42
    {
180 42
        if (!$this->ftp) {
181 36
            $this->connect();
182 36
        }
183
        return $this->ftp;
184
    }
185
186
    /**
187
     * Connect to the FTP server.
188 42
     */
189
    protected function connect()
190 42
    {
191 42
        $connection = ftp_connect($this->getHostname(), $this->getPort(), $this->getTimeout());
192 3
        if (!$connection) {
193 3
            throw new ServiceException(
194 3
                sprintf('Can\'t connect to host "%s"', $this->getHostname())
195
            );
196
        }
197
198 39
        // Login to host.
199 39
        $result = ftp_login($connection, $this->getUsername(), $this->getPassword());
200 3
        if (!$result) {
201 3
            throw new ServiceException(
202 3
                sprintf('Can\'t login with user "%s"', $this->getUsername())
203
            );
204
        }
205
206 36
        // Set connection to passive mode.
207
        ftp_pasv($connection, true);
208 36
209 36
        $this->ftp = $connection;
210
    }
211
212
213
    /**
214
     * Write the config to the config array.
215
     *
216
     * @param array $config
217 81
     */
218
    protected function setConfig(array $config)
219 81
    {
220 81
        $keyNames = array_keys($this->config);
221 81
        foreach ($keyNames as $key) {
222 48
            if (array_key_exists($key, $config)) {
223 48
                $this->config[$key] = $config[$key];
224 81
            }
225 81
        }
226
    }
227
228
    /**
229
     * Get the hostname from the config.
230
     *
231
     * @return string
232 42
     */
233
    protected function getHostname()
234 42
    {
235
        return $this->config['hostname'];
236
    }
237
238
    /**
239
     * Get the portnumber from the config.
240
     *
241
     * @return int
242 42
     */
243
    protected function getPort()
244 42
    {
245
        return (int) $this->config['port'];
246
    }
247
248
    /**
249
     * Get the timeout from the config.
250
     *
251
     * @return int
252 78
     */
253
    protected function getTimeout()
254 78
    {
255
        return (int) $this->config['timeout'];
256
    }
257
258
    /**
259
     * Get the username.
260
     *
261
     * @return string
262 78
     */
263
    protected function getUsername()
264 78
    {
265
        return $this->config['username'];
266
    }
267
268
    /**
269
     * Get the password.
270
     *
271
     * @return string
272
     */
273
    protected function getPassword()
274
    {
275
        return $this->config['password'];
276
    }
277
}
278