Completed
Pull Request — master (#140)
by
unknown
03:00
created

Rsync::createExecutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
ccs 17
cts 17
cp 1
cc 2
eloc 18
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup\Sync;
3
4
use phpbu\App\Backup\Cli;
5
use phpbu\App\Backup\File\Remote;
6
use phpbu\App\Backup\Rsync as RsyncTrait;
7
use phpbu\App\Backup\Target;
8
use phpbu\App\Cli\Executable;
9
use phpbu\App\Result;
10
use phpbu\App\Util;
11
12
/**
13
 * Rsync
14
 *
15
 * @package    phpbu
16
 * @subpackage Backup
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       http://phpbu.de/
21
 * @since      Class available since Release 1.1.0
22
 */
23
class Rsync extends Cli implements Simulator
24
{
25
    use RsyncTrait;
26
27
    /**
28
     * Setup the rsync sync.
29
     *
30
     * @see    \phpbu\App\Backup\Sync::setup()
31
     * @param  array $options
32
     * @throws \phpbu\App\Backup\Sync\Exception
33
     */
34 13
    public function setup(array $options)
35
    {
36
        try {
37 13
            $this->setupRsync($options);
38 1
        } catch (\Exception $e) {
39 1
            throw new Exception($e->getMessage());
40
        }
41 12
    }
42
43
    /**
44
     * Execute the sync.
45
     *
46
     * @see    \phpbu\App\Backup\Sync::sync()
47
     * @param  \phpbu\App\Backup\Target $target
48
     * @param  \phpbu\App\Result        $result
49
     * @throws \phpbu\App\Backup\Sync\Exception
50
     */
51 2 View Code Duplication
    public function sync(Target $target, Result $result)
52
    {
53 2
        if ($this->args) {
54
            // pro mode define all arguments yourself
55
            // WARNING! no escaping is done by phpbu
56 1
            $result->debug('WARNING: phpbu uses your rsync args without escaping');
57
        }
58 2
        $rsync = $this->execute($target);
59
60 2
        $result->debug($rsync->getCmd());
61
62 2
        if (!$rsync->isSuccessful()) {
63 1
            throw new Exception('rsync failed: ' . $rsync->getStdErr());
64
        }
65 1
    }
66
67
    /**
68
     * Simulate the sync execution.
69
     *
70
     * @param \phpbu\App\Backup\Target $target
71
     * @param \phpbu\App\Result        $result
72
     */
73 1
    public function simulate(Target $target, Result $result)
74
    {
75 1
        $result->debug(
76 1
            'sync backup with rsync' . PHP_EOL
77 1
            . $this->getExecutable($target)->getCommandPrintable()
78
        );
79 1
    }
80
81
    /**
82
     * Setup the Executable to run the 'rsync' command.
83
     *
84
     * @param  \phpbu\App\Backup\Target
85
     * @return \phpbu\App\Cli\Executable
86
     */
87 9
    protected function createExecutable(Target $target) : Executable
88
    {
89 9
        $executable = new Executable\Rsync($this->pathToRsync);
90 9
        if (!empty($this->args)) {
91 2
            $executable->useArgs(
92 2
                Util\Str::replaceTargetPlaceholders(
93 2
                    $this->args,
94 2
                    $target->getPathname()
95
                )
96
            );
97
        } else {
98 7
            $executable->fromPath($this->getRsyncLocation($target))
99 7
                       ->usePassword($this->password)
100 7
                       ->usePasswordFile($this->passwordFile)
101 7
                       ->toHost($this->host)
102 7
                       ->toPath($this->path)
103 7
                       ->toUser($this->user)
104 7
                       ->compressed(!$target->shouldBeCompressed())
105 7
                       ->removeDeleted($this->delete)
106 7
                       ->exclude($this->excludes);
107
        }
108 9
        return $executable;
109
    }
110
111
    /**
112
     * Execute the remote clean up if needed
113
     *
114
     * @param \phpbu\App\Backup\Target $target
115
     * @param \phpbu\App\Result        $result
116
     */
117
    public function cleanup(Target $target, Result $result)
118
    {
119
        // TODO: Implement cleanup() method.
120
    }
121
}
122