Completed
Pull Request — master (#145)
by Vitaly
02:41
created

Sftp::checkRequirements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 4
cp 0.75
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
crap 2.0625
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Collector;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Result;
7
use phpbu\App\Util;
8
use phpseclib;
9
10
/**
11
 * Sftp sync
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[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
 * @since      Class available since Release 1.0.0
20
 */
21
class Sftp extends Xtp implements Simulator
22
{
23
    /**
24
     * @var phpseclib\Net\SFTP
25
     */
26
    protected $sftp;
27
28
    /**
29
     * (non-PHPDoc)
30
     *
31
     * @see    \phpbu\App\Backup\Sync::setup()
32
     * @param  array $config
33
     * @throws \phpbu\App\Backup\Sync\Exception
34
     * @throws \phpbu\App\Exception
35
     */
36 7
    public function setup(array $config)
37
    {
38 7
        parent::setup($config);
39
40 3
        $this->setUpClearable($config);
41 3
    }
42
43
    /**
44
     * Check for required loaded libraries or extensions.
45
     *
46
     * @throws \phpbu\App\Backup\Sync\Exception
47
     */
48 7
    protected function checkRequirements()
49
    {
50 7
        if (!class_exists('\\phpseclib\\Net\\SFTP')) {
51
            throw new Exception('phpseclib not installed - use composer to install "phpseclib/phpseclib" version 2.x');
52
        }
53 7
    }
54
55
    /**
56
     * Return implemented (*)TP protocol name.
57
     *
58
     * @return string
59
     */
60 1
    protected function getProtocolName()
61
    {
62 1
        return 'SFTP';
63
    }
64
65
    /**
66
     * (non-PHPDoc)
67
     *
68
     * @see    \phpbu\App\Backup\Sync::sync()
69
     * @param  \phpbu\App\Backup\Target $target
70
     * @param  \phpbu\App\Result        $result
71
     * @throws \phpbu\App\Backup\Sync\Exception
72
     */
73 1
    public function sync(Target $target, Result $result)
74
    {
75 1
        $this->sftp     = $this->login();
76 1
        $remoteFilename = $target->getFilename();
77 1
        $localFile      = $target->getPathname();
78
79 1
        foreach ($this->getRemoteDirectoryList() as $dir) {
80 1
            if (!$this->sftp->is_dir($dir)) {
81 1
                $result->debug(sprintf('creating remote dir \'%s\'', $dir));
82 1
                $this->sftp->mkdir($dir);
83
            }
84 1
            $result->debug(sprintf('change to remote dir \'%s\'', $dir));
85 1
            $this->sftp->chdir($dir);
86
        }
87
88 1
        $result->debug(sprintf('store file \'%s\' as \'%s\'', $localFile, $remoteFilename));
89 1
        $result->debug(sprintf('last error \'%s\'', $this->sftp->getLastSFTPError()));
90
91 1
        if (!$this->sftp->put($remoteFilename, $localFile, phpseclib\Net\SFTP::SOURCE_LOCAL_FILE)) {
92
            throw new Exception(sprintf('error uploading file: %s - %s', $localFile, $this->sftp->getLastSFTPError()));
93
        }
94
95
        // run remote cleanup
96 1
        $this->cleanup($target, $result);
97 1
    }
98
99
    /**
100
     * Create a sftp handle.
101
     *
102
     * @return \phpseclib\Net\SFTP
103
     * @throws \phpbu\App\Backup\Sync\Exception
104
     */
105
    protected function login() : phpseclib\Net\SFTP
106
    {
107
        // silence phpseclib
108
        $old  = error_reporting(0);
109
        $sftp = new phpseclib\Net\SFTP($this->host);
110 View Code Duplication
        if (!$sftp->login($this->user, $this->password)) {
111
            error_reporting($old);
112
            throw new Exception(
113
                sprintf(
114
                    'authentication failed for %s@%s%s',
115
                    $this->user,
116
                    $this->host,
117
                    empty($this->password) ? '' : ' with password ****'
118
                )
119
            );
120
        }
121
        // restore old error reporting
122
        error_reporting($old);
123
124
        return $sftp;
125
    }
126
127
    /**
128
     * Return list of remote directories to travers.
129
     *
130
     * @return array
131
     */
132 1
    private function getRemoteDirectoryList() : array
133
    {
134 1
        return Util\Path::getDirectoryListFromAbsolutePath($this->remotePath);
135
    }
136
137
    /**
138
     * Creates collector for SFTP
139
     *
140
     * @param \phpbu\App\Backup\Target $target
141
     * @return \phpbu\App\Backup\Collector
142
     */
143
    protected function createCollector(Target $target): Collector
144
    {
145
        return new Collector\Sftp($target, $this->sftp, $this->remotePath);
146
    }
147
}
148