Completed
Push — master ( e2a90b...0f1023 )
by Sebastian
09:28
created

Compressor::createProcess()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 3
Bugs 0 Features 0
Metric Value
dl 0
loc 18
ccs 11
cts 11
cp 1
rs 9.4285
c 3
b 0
f 0
cc 3
eloc 10
nc 3
nop 0
crap 3
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
 * Compressor 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 Compressor extends Abstraction implements Executable
21
{
22
    /**
23
     * File to compress.
24
     *
25
     * @var string
26
     */
27
    protected $fileToCompress;
28
29
    /**
30
     * Force overwrite.
31
     *
32
     * @var boolean
33
     */
34
    protected $force = false;
35
36
    /**
37
     * Constructor.
38
     *
39
     * @param string $cmd
40
     * @param string $path
41
     */
42 5
    public function __construct($cmd, $path = null)
43
    {
44 5
        $this->cmd = $cmd;
45 5
        parent::__construct($path);
46 5
    }
47
48
    /**
49
     * Set the file to compress.
50
     *
51
     * @param  string $path
52
     * @return \phpbu\App\Cli\Executable\Compressor
53
     * @throws \phpbu\App\Exception
54
     */
55 4
    public function compressFile($path)
56
    {
57 4
        if (!file_exists($path)) {
58 1
            throw new Exception('file does not exist: ' . $path);
59
        }
60 3
        $this->fileToCompress = $path;
61 3
        return $this;
62
    }
63
64
    /**
65
     * Use '-f' force mode.
66
     *
67
     * @param  boolean $bool
68
     * @return \phpbu\App\Cli\Executable\Compressor
69
     */
70 3
    public function force($bool)
71
    {
72 3
        $this->force = $bool;
73 3
        return $this;
74
    }
75
76
    /**
77
     * Process generator
78
     *
79
     * @throws \phpbu\App\Exception
80
     */
81 4
    protected function createProcess()
82
    {
83
        // make sure there is a file to compress
84 4
        if (empty($this->fileToCompress)) {
85 1
            throw new Exception('file to compress not set');
86
        }
87 3
        $process = new Process();
88 3
        $cmd     = new Cmd($this->binary);
89 3
        $process->addCommand($cmd);
90
91
        // don't add '-f' option for 'zip' executable issue #34
92 3
        if ($this->cmd !== 'zip') {
93 3
            $cmd->addOptionIfNotEmpty('-f', $this->force, false);
94
        }
95 3
        $cmd->addArgument($this->fileToCompress);
96
97 3
        return $process;
98 2
    }
99
}
100