Completed
Push — master ( d8fe59...e9be78 )
by Sebastian
03:10
created

Rsync::getExecutable()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 16

Duplication

Lines 22
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 22
loc 22
rs 9.2
cc 3
eloc 16
nc 3
nop 1
1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Rsync as RsyncTrait;
5
use phpbu\App\Backup\Source;
6
use phpbu\App\Backup\Target;
7
use phpbu\App\Cli\Executable;
8
use phpbu\App\Exception;
9
use phpbu\App\Result;
10
use phpbu\App\Util;
11
12
/**
13
 * Rsync source class.
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.0.0
22
 */
23
class Rsync extends SimulatorExecutable implements Simulator
24
{
25
    use RsyncTrait;
26
27
    /**
28
     * Setup.
29
     *
30
     * @see    \phpbu\App\Backup\Source
31
     * @param  array $conf
32
     * @throws \phpbu\App\Exception
33
     */
34
    public function setup(array $conf = [])
35
    {
36
        $this->setupRsync($conf);
37
    }
38
39
    /**
40
     * Execute the backup.
41
     *
42
     * @see    \phpbu\App\Backup\Source
43
     * @param  \phpbu\App\Backup\Target $target
44
     * @param  \phpbu\App\Result        $result
45
     * @return \phpbu\App\Backup\Source\Status
46
     * @throws \phpbu\App\Exception
47
     */
48
    public function backup(Target $target, Result $result)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
49
    {
50
        $rsync = $this->execute($target);
51
        $result->debug($rsync->getCmd());
52
53
        if (!$rsync->wasSuccessful()) {
54
            throw new Exception('rsync failed:' . $rsync->getStdErr());
55
        }
56
57
        return $this->createStatus($target);
58
    }
59
60
    /**
61
     * Setup the Executable to run the 'rsync' command.
62
     *
63
     * @param  \phpbu\App\Backup\Target
64
     * @return \phpbu\App\Cli\Executable
65
     */
66 View Code Duplication
    public function getExecutable(Target $target)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68
        if (null == $this->executable) {
69
            $this->executable = new Executable\Rsync($this->pathToRsync);
70
            if (!empty($this->args)) {
71
                $this->executable->useArgs(
72
                    Util\Str::replaceTargetPlaceholders(
73
                        $this->args,
74
                        $target->getPathnamePlain()
75
                    )
76
                );
77
            } else {
78
                $this->executable->fromHost($this->host)
79
                                 ->fromUser($this->user)
80
                                 ->fromPath($this->path)
81
                                 ->toPath($this->getRsyncLocation($target))
82
                                 ->removeDeleted($this->delete)
83
                                 ->exclude($this->excludes);
84
            }
85
        }
86
        return $this->executable;
87
    }
88
89
    /**
90
     * Create backup status.
91
     *
92
     * @param  \phpbu\App\Backup\Target
93
     * @return \phpbu\App\Backup\Source\Status
94
     */
95
    protected function createStatus(Target $target)
96
    {
97
        $status = Status::create();
98
        if (!$this->isDirSync) {
99
            $targetFile = $target->getPathnamePlain();
100
            is_dir($targetFile) ? $status->uncompressedDirectory($targetFile) : $status->uncompressedFile($targetFile);
101
        }
102
        return $status;
103
    }
104
}
105