Completed
Push — master ( 689b12...c8d3c9 )
by Sebastian
03:07
created

Tar::useCompressProgram()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Executable;
5
use phpbu\App\Exception;
6
use SebastianFeldmann\Cli\CommandLine;
7
use SebastianFeldmann\Cli\Command\Executable as Cmd;
8
9
/**
10
 * Tar 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 1.0.0
19
 */
20
class Tar extends Abstraction implements Executable
21
{
22
    /**
23
     * Path to compress
24
     *
25
     * @var string
26
     */
27
    private $path;
28
29
    /**
30
     * Compression to use
31
     *
32
     * @var string
33
     */
34
    private $compression;
35
36
    /**
37
     * Compress program to use.
38
     * --use-compress-program
39
     *
40
     * @var string
41
     */
42
    private $compressProgram;
43
44
    /**
45
     * Path to dump file
46
     *
47
     * @var string
48
     */
49
    private $tarPathname;
50
51
    /**
52
     * List of excluded path.
53
     * --exclude='foo'
54
     *
55
     * @var array
56
     */
57
    private $excludes = [];
58
59
    /**
60
     * Force local file resolution
61
     * --force-local
62
     *
63
     * @var bool
64
     */
65
    private $local = false;
66
67
    /**
68
     * Ignore failed reads
69
     * --ignore-failed-read
70
     *
71
     * @var bool
72
     */
73
    private $ignoreFailedRead;
74
75
    /**
76
     * Should the source directory be removed.
77
     *
78
     * @var boolean
79
     */
80
    private $removeSourceDir = false;
81
82
    /**
83
     * Limit data throughput
84
     * | pv -L ${limit}
85
     *
86
     * @var string
87
     */
88
    private $pvLimit = '';
89
90
    /**
91
     * List of available compressors
92
     *
93
     * @var array
94
     */
95
    private static $availableCompressions = [
96
        'bzip2' => 'j',
97
        'gzip'  => 'z',
98
        'xz'    => 'J'
99
    ];
100
101
    /**
102
     * Constructor.
103
     *
104
     * @param string $path
105
     */
106 31
    public function __construct(string $path = '')
107
    {
108 31
        $this->setup('tar', $path);
109 31
    }
110
111
    /**
112
     * Return 'tar' compressor option e.g. 'j' for bzip2.
113
     *
114
     * @param  string $compressor
115
     * @return string
116
     */
117 10
    protected function getCompressionOption(string $compressor) : string
118
    {
119 10
        return $this->isCompressionValid($compressor) ? self::$availableCompressions[$compressor] : '';
120
    }
121
122
    /**
123
     * Compress tar.
124
     *
125
     * @param  string $compression
126
     * @return \phpbu\App\Cli\Executable\Tar
127
     */
128 21
    public function useCompression(string $compression) : Tar
129
    {
130 21
        if ($this->isCompressionValid($compression)) {
131 10
            $this->compression = $this->getCompressionOption($compression);
132
        }
133 21
        return $this;
134
    }
135
136
    /**
137
     * Set compress program.
138
     *
139
     * @param  string $program
140
     * @return \phpbu\App\Cli\Executable\Tar
141
     */
142 14
    public function useCompressProgram(string $program) : Tar
143
    {
144 14
        $this->compressProgram = $program;
145 14
        return $this;
146
    }
147
148
    /**
149
     * Add an path to exclude.
150
     *
151
     * @param  string $path
152
     * @return \phpbu\App\Cli\Executable\Tar
153
     */
154 2
    public function addExclude(string $path) : Tar
155
    {
156 2
        $this->excludes[] = $path;
157 2
        return $this;
158
    }
159
160
    /**
161
     * Force local file resolution.
162
     *
163
     * @param  bool $bool
164
     * @return \phpbu\App\Cli\Executable\Tar
165
     */
166 14
    public function forceLocal(bool $bool) : Tar
167
    {
168 14
        $this->local = $bool;
169 14
        return $this;
170
    }
171
172
    /**
173
     * Ignore failed reads setter.
174
     *
175
     * @param  bool $bool
176
     * @return \phpbu\App\Cli\Executable\Tar
177
     */
178 14
    public function ignoreFailedRead(bool $bool) : Tar
179
    {
180 14
        $this->ignoreFailedRead = $bool;
181 14
        return $this;
182
    }
183
184
    /**
185
     * Limit the data throughput.
186
     *
187
     * @param string $limit
188
     * @return \phpbu\App\Cli\Executable\Tar
189
     */
190 15
    public function throttle(string $limit) : Tar
191
    {
192 15
        $this->pvLimit = $limit;
193 15
        return $this;
194
    }
195
196
    /**
197
     * Does the tar handle the compression.
198
     *
199
     * @return bool
200
     */
201 3
    public function handlesCompression() : bool
202
    {
203 3
        return !empty($this->compression);
204
    }
205
206
    /**
207
     * Set folder to compress.
208
     *
209
     * @param  string $path
210
     * @return \phpbu\App\Cli\Executable\Tar
211
     * @throws \phpbu\App\Exception
212
     */
213 29
    public function archiveDirectory(string $path) : Tar
214
    {
215 29
        $this->validateDirectory($path);
216 28
        $this->path = $path;
217 28
        return $this;
218
    }
219
220
    /**
221
     * Set target filename.
222
     *
223
     * @param  string $path
224
     * @return \phpbu\App\Cli\Executable\Tar
225
     */
226 27
    public function archiveTo(string $path) : Tar
227
    {
228 27
        $this->tarPathname = $path;
229 27
        return $this;
230
    }
231
232
    /**
233
     * Delete the source directory.
234
     *
235
     * @param  boolean $bool
236
     * @return \phpbu\App\Cli\Executable\Tar
237
     */
238 18
    public function removeSourceDirectory(bool $bool) : Tar
239
    {
240 18
        $this->removeSourceDir = $bool;
241 18
        return $this;
242
    }
243
244
    /**
245
     * Tar CommandLine generator.
246
     *
247
     * @return \SebastianFeldmann\Cli\CommandLine
248
     */
249 29
    protected function createCommandLine() : CommandLine
250
    {
251 29
        $this->validateSetup();
252
253 27
        $process = new CommandLine();
254 27
        $tar     = new Cmd($this->binary);
255 27
        $create  = $this->isThrottled() ? 'c' : 'cf';
256 27
        $process->addCommand($tar);
257
258 27
        $this->setExcludeOptions($tar);
259
260 27
        $tar->addOptionIfNotEmpty('--force-local', $this->local, false);
261 27
        $tar->addOptionIfNotEmpty('--ignore-failed-read', $this->ignoreFailedRead, false);
262 27
        $tar->addOptionIfNotEmpty('--use-compress-program', $this->compressProgram);
263 27
        $tar->addOption('-' . (empty($this->compressProgram) ? $this->compression : '') . $create);
264
265 27
        if ($this->isThrottled()) {
266 3
            $pv = new Cmd('pv');
267 3
            $pv->addOption('-qL', $this->pvLimit, ' ');
268 3
            $process->pipeOutputTo($pv);
269 3
            $process->redirectOutputTo($this->tarPathname);
270
        } else {
271 24
            $tar->addArgument($this->tarPathname);
272
        }
273
274 27
        $tar->addOption('-C', dirname($this->path), ' ');
275 27
        $tar->addArgument(basename($this->path));
276
277
        // delete the source data if requested
278 27
        $this->addRemoveCommand($process);
279
280 27
        return $process;
281
    }
282
283
    /**
284
     * Adds necessary exclude options to tat command.
285
     *
286
     * @param \SebastianFeldmann\Cli\Command\Executable $tar
287
     */
288 27
    protected function setExcludeOptions(Cmd $tar)
289
    {
290 27
        foreach ($this->excludes as $path) {
291 2
            $tar->addOption('--exclude', $path);
292
        }
293 27
    }
294
295
    /**
296
     * Add a remove command if requested.
297
     *
298
     * @param \SebastianFeldmann\Cli\CommandLine $process
299
     */
300 27
    protected function addRemoveCommand(CommandLine $process)
301
    {
302 27
        if ($this->removeSourceDir) {
303 6
            $process->addCommand($this->getRmCommand());
304
        }
305 27
    }
306
307
    /**
308
     * Return 'rm' command.
309
     *
310
     * @return \SebastianFeldmann\Cli\Command\Executable
311
     */
312 6
    protected function getRmCommand() : Cmd
313
    {
314 6
        $rm = new Cmd('rm');
315 6
        $rm->addOption('-rf', $this->path, ' ');
316 6
        return $rm;
317
    }
318
319
    /**
320
     * Check directory to compress.
321
     *
322
     * @param  string $path
323
     * @throws \phpbu\App\Exception
324
     */
325 29
    private function validateDirectory(string $path)
326
    {
327 29
        if ($path === '.') {
328 1
            throw new Exception('unable to tar current working directory');
329
        }
330 28
    }
331
332
    /**
333
     * Check if source and target values are set.
334
     *
335
     * @throws \phpbu\App\Exception
336
     */
337 29
    private function validateSetup()
338
    {
339 29
        if (empty($this->path)) {
340 1
            throw new Exception('no directory to compress');
341
        }
342 28
        if (empty($this->tarPathname)) {
343 1
            throw new Exception('no target filename set');
344
        }
345 27
    }
346
347
    /**
348
     * Return true if a given compression is valid false otherwise.
349
     *
350
     * @param  string $compression
351
     * @return bool
352
     */
353 23
    public static function isCompressionValid(string  $compression) : bool
354
    {
355 23
        return isset(self::$availableCompressions[$compression]);
356
    }
357
358
    /**
359
     * Should output be throttled through pv.
360
     *
361
     * @return bool
362
     */
363 27
    public function isThrottled() : bool
364
    {
365 27
        return !empty($this->pvLimit);
366
    }
367
}
368