Ftp::createClient()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 7
ccs 0
cts 5
cp 0
crap 12
rs 10
c 1
b 0
f 0
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Backup\Path;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
use SebastianFeldmann\Ftp\Client;
10
11
/**
12
 * Ftp sync
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Chris Hawes <[email protected]>
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       https://phpbu.de/
21
 */
22
class Ftp extends Xtp
23
{
24
    use Cleanable;
0 ignored issues
show
introduced by
The trait phpbu\App\Backup\Sync\Cleanable requires some properties which are not provided by phpbu\App\Backup\Sync\Ftp: $type, $options
Loading history...
25
26
    /**
27
     * FTP connection stream
28
     *
29
     * @var \SebastianFeldmann\Ftp\Client
30
     */
31
    protected $ftpClient;
32
33
    /**
34
     * Determine should ftp connects via passive mode
35
     *
36
     * @var bool
37
     */
38
    protected $passive;
39
40
    /**
41
     * Setup the Ftp sync
42
     *
43
     * @param  array $config
44
     * @throws \phpbu\App\Backup\Sync\Exception
45
     * @throws \phpbu\App\Exception
46
     */
47 9
    public function setup(array $config)
48
    {
49 9
        $path = Util\Arr::getValue($config, 'path', '');
50 9
        if ('/' === substr($path, 0, 1)) {
51 1
            throw new Exception('absolute path is not allowed');
52
        }
53 8
        if (!Util\Arr::isSetAndNotEmptyString($config, 'password')) {
54 1
            throw new Exception('option \'password\' is missing');
55
        }
56 7
        parent::setup($config);
57
58 5
        $this->passive = Util\Str::toBoolean(Util\Arr::getValue($config, 'passive', ''), false);
59 5
        $this->setUpCleanable($config);
60 5
    }
61
62
    /**
63
     * Check for required loaded libraries or extensions
64
     *
65
     * @throws \phpbu\App\Backup\Sync\Exception
66
     */
67 7
    protected function checkRequirements()
68
    {
69 7
        if (!function_exists('ftp_connect')) {
70
            throw new Exception('ftp functions not enabled');
71
        }
72 7
    }
73
74
    /**
75
     * Return implemented (*)TP protocol name
76
     *
77
     * @return string
78
     */
79 1
    protected function getProtocolName()
80
    {
81 1
        return 'FTP';
82
    }
83
84
    /**
85
     * (non-PHPDoc)
86
     *
87
     * @see    \phpbu\App\Backup\Sync::sync()
88
     * @param  \phpbu\App\Backup\Target $target
89
     * @param  \phpbu\App\Result        $result
90
     * @throws \phpbu\App\Backup\Sync\Exception
91
     */
92 3
    public function sync(Target $target, Result $result)
93
    {
94
        try {
95 3
            $client         = $this->createClient();
96 3
            $remoteFilename = $target->getFilename();
97 3
            $localFile      = $target->getPathname();
98
99 3
            $client->uploadFile($localFile, Util\Path::withTrailingSlash($this->remotePath), $remoteFilename);
100 2
            $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
101 2
            $this->cleanup($target, $result);
102 1
        } catch (\Exception $e) {
103 1
            throw new Exception($e->getMessage());
104
        }
105 2
    }
106
107
    /**
108
     * Return FTP client wrapping the ftp connection
109
     *
110
     * @return \SebastianFeldmann\Ftp\Client
111
     */
112
    protected function createClient()
113
    {
114
        if (!$this->ftpClient) {
115
            $login           = $this->user . ($this->password ? ':' . $this->password : '');
116
            $this->ftpClient = new Client('ftp://' . $login . '@' . $this->host, $this->passive);
117
        }
118
        return $this->ftpClient;
119
    }
120
121
    /**
122
     * Creates FTP remote collector
123
     *
124
     * @param  \phpbu\App\Backup\Target $target
125
     * @return \phpbu\App\Backup\Collector
126
     */
127 1
    protected function createCollector(Target $target): Collector
128
    {
129 1
        return new Collector\Ftp($target, new Path($this->remotePath), $this->createClient());
130
    }
131
}
132