Completed
Push — master ( d6a771...3f2a2f )
by Sebastian
03:29
created

Rsync::configureExcludes()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 2
crap 2
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Cmd;
5
use phpbu\App\Cli\Executable;
6
use phpbu\App\Cli\Process;
7
use phpbu\App\Exception;
8
9
/**
10
 * Rsync executable class.
11
 *
12
 * @package    phpbu
13
 * @subpackage Backup
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 2.1.0
19
 */
20
class Rsync extends Abstraction implements Executable
21
{
22
    /**
23
     * Source
24
     *
25
     * @var \phpbu\App\Cli\Executable\Rsync\Location
26
     */
27
    private $source;
28
29
    /**
30
     * Target
31
     *
32
     * @var \phpbu\App\Cli\Executable\Rsync\Location
33
     */
34
    private $target;
35
36
    /**
37
     * Raw args
38
     *
39
     * @var string
40
     */
41
    protected $args;
42
43
    /**
44
     * Files to ignore, extracted from config string separated by ":"
45
     *
46
     * @var array
47
     */
48
    protected $excludes = [];
49
50
    /**
51
     * Remove deleted files remotely as well
52
     *
53
     * @var boolean
54
     */
55
    protected $delete;
56
57
    /**
58
     * Compress data.
59
     *
60
     * @var boolean
61
     */
62
    protected $compressed = false;
63
64
    /**
65
     * Constructor.
66
     *
67
     * @param string $path
68
     */
69
    public function __construct($path = null)
70
    {
71
        $this->setup('rsync', $path);
72
        $this->source = new Rsync\Location();
73
        $this->target = new Rsync\Location();
74
    }
75
76
    /**
77
     * Set custom args
78
     *
79
     * @param  string $args
80
     * @return \phpbu\App\Cli\Executable\Rsync
81
     */
82
    public function useArgs($args)
83 15
    {
84
        $this->args = $args;
85 15
        return $this;
86 15
    }
87 15
88
    /**
89
     * Set source user.
90
     *
91
     * @param  string $user
92
     * @return \phpbu\App\Cli\Executable\Rsync
93
     */
94
    public function fromUser($user)
95 2
    {
96
        $this->source->setUser($user);
97 2
        return $this;
98 2
    }
99
100
    /**
101
     * Set source host.
102
     *
103
     * @param  string $host
104
     * @return \phpbu\App\Cli\Executable\Rsync
105
     */
106
    public function fromHost($host)
107 8
    {
108
        $this->source->setHost($host);
109 8
        return $this;
110 8
    }
111
112
    /**
113
     * Set source path.
114
     *
115
     * @param  string $path
116
     * @return \phpbu\App\Cli\Executable\Rsync
117
     */
118
    public function fromPath($path)
119 4
    {
120
        $this->source->setPath($path);
121 4
        return $this;
122 4
    }
123
124
    /**
125
     * Use compression.
126
     *
127
     * @param  bool $bool
128
     * @return \phpbu\App\Cli\Executable\Rsync
129
     */
130
    public function compressed($bool)
131 5
    {
132
        $this->compressed = $bool;
133 5
        return $this;
134 5
    }
135
136
    /**
137
     * Sync to host.
138
     *
139
     * @param  string $host
140
     * @return \phpbu\App\Cli\Executable\Rsync
141
     */
142
    public function toHost($host)
143 10
    {
144
        $this->target->setHost($host);
145 10
        return $this;
146 10
    }
147
148
    /**
149
     * Set path to sync to.
150
     *
151
     * @param  string $path
152
     * @return \phpbu\App\Cli\Executable\Rsync
153
     */
154
    public function toPath($path)
155 5
    {
156
        $this->target->setPath($path);
157 5
        return $this;
158 5
    }
159
160
    /**
161
     * Set user to connect as.
162
     *
163
     * @param  string $user
164
     * @return \phpbu\App\Cli\Executable\Rsync
165
     */
166
    public function toUser($user)
167 4
    {
168
        $this->target->setUser($user);
169 4
        return $this;
170 4
    }
171
172
    /**
173
     * Exclude files.
174
     *
175
     * @param  array $excludes
176
     * @return \phpbu\App\Cli\Executable\Rsync
177
     */
178
    public function exclude(array $excludes)
179 4
    {
180
        $this->excludes = $excludes;
181 4
        return $this;
182 4
    }
183
184
    /**
185
     * Use --delete option.
186
     *
187
     * @param  boolean $bool
188
     * @return \phpbu\App\Cli\Executable\Rsync
189
     */
190
    public function removeDeleted($bool)
191 11
    {
192
        $this->delete = $bool;
193 11
        return $this;
194 11
    }
195 11
196
    /**
197 11
     * Subclass Process generator.
198 2
     *
199 2
     * @return \phpbu\App\Cli\Process
200 9
     * @throws \phpbu\App\Exception
201 1
     */
202
    protected function createProcess()
203 8
    {
204 1
        $process = new Process();
205
        $cmd     = new Cmd($this->binary);
206
        $process->addCommand($cmd);
207 7
208
        if (!empty($this->args)) {
209
            $cmd->addOption($this->args);
210 7
        } else {
211 7
            if (!$this->source->isValid()) {
212
                throw new Exception('source path is missing');
213 7
            }
214 2
            if (!$this->target->isValid()) {
215 2
                throw new Exception('target path is missing');
216 2
            }
217 2
218
            // use archive mode, verbose and compress if not already done
219 7
            $options = '-av' . ($this->compressed ? 'z' : '');
220 7
            $cmd->addOption($options);
221
            $this->configureExcludes($cmd, $this->excludes);
222
            $cmd->addOptionIfNotEmpty('--delete', $this->delete, false);
223
            $cmd->addArgument($this->source->toString());
224 7
            $cmd->addArgument($this->target->toString());
225
        }
226 7
227
        return $process;
228 7
    }
229
230
    /**
231 9
     * Configure excludes.
232
     *
233
     * @param \phpbu\App\Cli\Cmd $cmd
234
     * @param array              $excludes
235
     */
236
    protected function configureExcludes(Cmd $cmd, array $excludes)
237
    {
238
        foreach ($excludes as $ex) {
239 11
            $cmd->addOption('--exclude', $ex);
240
        }
241 11
    }
242
}
243