Completed
Pull Request — master (#140)
by
unknown
10:09 queued 03:44
created

Ftp   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 134
Duplicated Lines 8.21 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 26%

Importance

Changes 0
Metric Value
wmc 14
c 0
b 0
f 0
lcom 1
cbo 8
dl 11
loc 134
ccs 13
cts 50
cp 0.26
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 11 2
A checkRequirements() 0 6 2
A getProtocolName() 0 4 1
B sync() 11 55 8
A createCollector() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Result;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Util\Arr;
8
use phpbu\App\Util\Str;
9
10
/**
11
 * Ftp sync
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Chris Hawes <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 */
20
class Ftp extends Xtp implements Simulator
21
{
22
    use Clearable;
23
24
    /**
25
     * FTP connection stream
26
     *
27
     * @var resource
28
     */
29
    protected $ftpConnection;
30
31
    /**
32
     * Determine should ftp connects via passive mode.
33
     *
34
     * @var bool
35
     */
36
    protected $passive;
37
38
    /**
39
     * Setup the Ftp sync.
40
     *
41
     * @param  array $config
42
     * @throws \phpbu\App\Backup\Sync\Exception
43
     * @throws \phpbu\App\Exception
44
     */
45 6
    public function setup(array $config)
46
    {
47 6
        $path = Arr::getValue($config, 'path', '');
48 6
        if ('/' === substr($path, 0, 1)) {
49 1
            throw new Exception('absolute path is not allowed');
50
        }
51 5
        parent::setup($config);
52
53 2
        $this->passive = Str::toBoolean(Arr::getValue($config, 'passive', ''), false);
54 2
        $this->setUpClearable($config);
55 2
    }
56
57
    /**
58
     * Check for required loaded libraries or extensions.
59
     *
60
     * @throws \phpbu\App\Backup\Sync\Exception
61
     */
62 5
    protected function checkRequirements()
63
    {
64 5
        if (!function_exists('ftp_connect')) {
65
            throw new Exception('ftp functions not enabled');
66
        }
67 5
    }
68
69
    /**
70
     * Return implemented (*)TP protocol name.
71
     *
72
     * @return string
73
     */
74 1
    protected function getProtocolName()
75
    {
76 1
        return 'FTP';
77
    }
78
79
    /**
80
     * (non-PHPDoc)
81
     *
82
     * @see    \phpbu\App\Backup\Sync::sync()
83
     * @param  \phpbu\App\Backup\Target $target
84
     * @param  \phpbu\App\Result        $result
85
     * @throws \phpbu\App\Backup\Sync\Exception
86
     */
87
    public function sync(Target $target, Result $result)
88
    {
89
        // silence ftp errors
90
        $old  = error_reporting(0);
91
        if (!$this->ftpConnection = ftp_connect($this->host)) {
92
            throw new Exception(
93
                sprintf(
94
                    'Unable to connect to ftp server %s',
95
                    $this->host
96
                )
97
            );
98
        }
99
100 View Code Duplication
        if (!ftp_login($this->ftpConnection, $this->user, $this->password)) {
101
            error_reporting($old);
102
            throw new Exception(
103
                sprintf(
104
                    'authentication failed for %s@%s%s',
105
                    $this->user,
106
                    $this->host,
107
                    empty($this->password) ? '' : ' with password ****'
108
                )
109
            );
110
        }
111
112
        // Set passive mode if needed
113
        ftp_pasv($this->ftpConnection, $this->passive);
114
115
        $remoteFilename = $target->getFilename();
116
        $localFile      = $target->getPathname();
117
118
        if ('' !== $this->remotePath) {
119
            $remoteDirs = explode('/', $this->remotePath);
120
            foreach ($remoteDirs as $dir) {
121
                if (!ftp_chdir($this->ftpConnection, $dir)) {
122
                    $result->debug(sprintf('creating remote dir \'%s\'', $dir));
123
                    ftp_mkdir($this->ftpConnection, $dir);
124
                    ftp_chdir($this->ftpConnection, $dir);
125
                } else {
126
                    $result->debug(sprintf('change to remote dir \'%s\'', $dir));
127
                }
128
            }
129
        }
130
        $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
131
132
        if (!ftp_put($this->ftpConnection, $remoteFilename, $localFile, FTP_BINARY)) {
133
            $error = error_get_last();
134
            $message = $error['message'];
135
            throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $message));
136
        }
137
138
        error_reporting($old);
139
140
        $this->cleanup($target, $result);
141
    }
142
143
    /**
144
     * Creates collector for FTP
145
     *
146
     * @param \phpbu\App\Backup\Target $target
147
     * @return \phpbu\App\Backup\Collector
148
     */
149
    protected function createCollector(Target $target): Collector
150
    {
151
        return new \phpbu\App\Backup\Collector\Ftp($target, $this->ftpConnection);
152
    }
153
}
154