Completed
Pull Request — master (#140)
by
unknown
11:59 queued 08:36
created

Ftp::getProtocolName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use League\Flysystem\Config;
5
use League\Flysystem\Filesystem;
6
use League\Flysystem\Adapter\Ftp as FtpAdapter;
7
use phpbu\App\Result;
8
use phpbu\App\Backup\Target;
9
use phpbu\App\Util\Arr;
10
use phpbu\App\Util\Str;
11
12
/**
13
 * Ftp sync
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
17
 * @author     Chris Hawes <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 */
22
class Ftp extends Xtp implements Simulator
23
{
24
    use Clearable;
25
26
    /**
27
     * FlySystem instance
28
     *
29
     * @var Filesystem
30
     */
31
    protected $flySystem;
32
33
    /**
34
     * Determine should ftp connects via passive mode.
35
     *
36
     * @var bool
37
     */
38
    protected $passiveMode;
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 5
    public function setup(array $config)
48
    {
49 5
        parent::setup($config);
50
51 2
        $this->passiveMode = Str::toBoolean(Arr::getValue($config, 'passive_mode', ''), false);
52 2
        $this->setUpClearable($config);
53 2
    }
54
55
    /**
56
     * Check for required loaded libraries or extensions.
57
     *
58
     * @throws \phpbu\App\Backup\Sync\Exception
59
     */
60 5
    protected function checkRequirements()
61
    {
62 5
        if (!function_exists('ftp_connect')) {
63
            throw new Exception('ftp functions not enabled');
64
        }
65 5
    }
66
67
    /**
68
     * Return implemented (*)TP protocol name.
69
     *
70
     * @return string
71
     */
72 1
    protected function getProtocolName()
73
    {
74 1
        return 'FTP';
75
    }
76
77
    /**
78
     * (non-PHPDoc)
79
     *
80
     * @see    \phpbu\App\Backup\Sync::sync()
81
     * @param  \phpbu\App\Backup\Target $target
82
     * @param  \phpbu\App\Result        $result
83
     * @throws \phpbu\App\Backup\Sync\Exception
84
     */
85
    public function sync(Target $target, Result $result)
86
    {
87
        $this->flySystem = new Filesystem(new FtpAdapter([
88
            'host'     => $this->host,
89
            'username' => $this->user,
90
            'password' => $this->password,
91
            'root'     => $this->remotePath,
92
            'passive'  => $this->passiveMode,
93
            'timeout'  => 30,
94
        ]), new Config([
95
            'disable_asserts' => true,
96
        ]));
97
98
        // silence ftp errors
99
        $old  = error_reporting(0);
100
101
        try {
102
            if ($this->flySystem->has($target->getFilename())) {
103
                $this->flySystem->update($target->getFilename(), file_get_contents($target->getPathname()));
104
            } else {
105
                $this->flySystem->write($target->getFilename(), file_get_contents($target->getPathname()));
106
            }
107
        } catch (\Exception $exception) {
108
            throw new Exception($exception->getMessage());
109
        }
110
111
        // run remote cleanup
112
        $this->cleanup($target, $result);
113
114
        error_reporting($old);
115
    }
116
117
    /**
118
     * Execute the remote clean up if needed
119
     *
120
     * @param \phpbu\App\Backup\Target $target
121
     * @param \phpbu\App\Result        $result
122
     */
123
    public function cleanup(Target $target, Result $result)
124
    {
125
        if (!$this->cleaner) {
126
            return;
127
        }
128
129
        $collector = new \phpbu\App\Backup\Collector\Ftp($target, $this->flySystem);
130
        $this->cleaner->cleanup($target, $collector, $result);
131
    }
132
}
133