Completed
Pull Request — master (#140)
by
unknown
05:20
created

Rsync   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 99
Duplicated Lines 15.15 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 94.59%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 9
dl 15
loc 99
ccs 35
cts 37
cp 0.9459
rs 10
c 0
b 0
f 0

5 Methods

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