Compressor::force()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
 * 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 7
    public function __construct(string $cmd, string $path = '')
43
    {
44 7
        $this->setup($cmd, $path);
45 7
    }
46
47
    /**
48
     * Set the file to compress.
49
     *
50
     * @param  string $path
51
     * @return \phpbu\App\Cli\Executable\Compressor
52
     */
53 6
    public function compressFile($path)
54
    {
55 6
        $this->fileToCompress = $path;
56 6
        return $this;
57
    }
58
59
    /**
60
     * Use '-f' force mode.
61
     *
62
     * @param  boolean $bool
63
     * @return \phpbu\App\Cli\Executable\Compressor
64
     */
65 6
    public function force($bool)
66
    {
67 6
        $this->force = $bool;
68 6
        return $this;
69
    }
70
71
    /**
72
     * Compressor CommandLine generator
73
     *
74
     * @return \SebastianFeldmann\Cli\CommandLine
75
     * @throws \phpbu\App\Exception
76
     */
77 7
    protected function createCommandLine() : CommandLine
78
    {
79
        // make sure there is a file to compress
80 7
        if (empty($this->fileToCompress)) {
81 1
            throw new Exception('file to compress not set');
82
        }
83 6
        $process = new CommandLine();
84 6
        $cmd     = new Cmd($this->binary);
85 6
        $process->addCommand($cmd);
86
87
        // don't add '-f' option for 'zip' executable issue #34
88 6
        if ($this->cmd !== 'zip') {
89 5
            $cmd->addOptionIfNotEmpty('-f', $this->force, false);
90
        }
91 6
        $cmd->addArgument($this->fileToCompress);
92
93 6
        return $process;
94
    }
95
}
96