Mcrypt::useKeyFile()   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
 * Mcrypt 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 Mcrypt extends Abstraction implements Executable
21
{
22
    use OptionMasker;
23
24
    /**
25
     * Key to pass via cli
26
     *
27
     * @var string
28
     */
29
    private $key;
30
31
    /**
32
     * Key file
33
     *
34
     * @var string
35
     */
36
    private $keyFile;
37
38
    /**
39
     * Algorithm to use
40
     *
41
     * @var string
42
     */
43
    private $algorithm;
44
45
    /**
46
     * Hash to use
47
     *
48
     * @var string
49
     */
50
    private $hash;
51
52
    /**
53
     * Path to config file
54
     *
55
     * @var string
56
     */
57
    private $config;
58
59
    /**
60
     * Keep the not encrypted file
61
     *
62
     * @var boolean
63
     */
64
    private $deleteUncrypted = true;
65
66
    /**
67
     * Path to the encrypted file
68
     *
69
     * @var string
70
     */
71
    private $targetFile;
72
73
    /**
74
     * Constructor.
75
     *
76
     * @param string $path
77
     */
78 10
    public function __construct(string $path = '')
79
    {
80 10
        $this->setup('mcrypt', $path);
81 10
        $this->setMaskCandidates(['key']);
82 10
    }
83
84
    /**
85
     * Set the target file.
86
     *
87
     * @param  string $path
88
     * @return \phpbu\App\Cli\Executable\Mcrypt
89
     */
90 9
    public function saveAt(string $path) : Mcrypt
91
    {
92 9
        $this->targetFile = $path;
93 9
        return $this;
94
    }
95
96
    /**
97
     * Delete the uncrypted data.
98
     *
99
     * @param  bool $bool
100
     * @return \phpbu\App\Cli\Executable\Mcrypt
101
     */
102 4
    public function deleteUncrypted(bool $bool) : Mcrypt
103
    {
104 4
        $this->deleteUncrypted = $bool;
105 4
        return $this;
106
    }
107
108
    /**
109
     * Key to use for encryption.
110
     *
111
     * @param  string $key
112
     * @return \phpbu\App\Cli\Executable\Mcrypt
113
     */
114 7
    public function useKey(string $key) : Mcrypt
115
    {
116 7
        $this->key = $key;
117 7
        return $this;
118
    }
119
120
    /**
121
     * Key file to use for encryption.
122
     *
123
     * @param  string $keyFile
124
     * @return \phpbu\App\Cli\Executable\Mcrypt
125
     */
126 5
    public function useKeyFile(string $keyFile) : Mcrypt
127
    {
128 5
        $this->keyFile = $keyFile;
129 5
        return $this;
130
    }
131
132
    /**
133
     * Set algorithm to use.
134
     *
135
     * @param  string $algorithm
136
     * @return \phpbu\App\Cli\Executable\Mcrypt
137
     */
138 10
    public function useAlgorithm(string $algorithm) : Mcrypt
139
    {
140 10
        $this->algorithm = $algorithm;
141 10
        return $this;
142
    }
143
144
    /**
145
     * Hash to use for encryption.
146
     *
147
     * @param  string $hash
148
     * @return \phpbu\App\Cli\Executable\Mcrypt
149
     */
150 7
    public function useHash(string $hash) : Mcrypt
151
    {
152 7
        $this->hash = $hash;
153 7
        return $this;
154
    }
155
156
    /**
157
     * Set path to sync to.
158
     *
159
     * @param  string $path
160
     * @return \phpbu\App\Cli\Executable\Mcrypt
161
     */
162 5
    public function useConfig(string $path) : Mcrypt
163
    {
164 5
        $this->config = $path;
165 5
        return $this;
166
    }
167
168
    /**
169
     * Mcrypt CommandLine generator.
170
     *
171
     * @return \SebastianFeldmann\Cli\CommandLine
172
     * @throws \phpbu\App\Exception
173
     */
174 10
    protected function createCommandLine() : CommandLine
175
    {
176 10
        if (empty($this->targetFile)) {
177 1
            throw new Exception('target file is missing');
178
        }
179 9
        if (empty($this->key) && empty($this->keyFile)) {
180 1
            throw new Exception('one of \'key\' or \'keyFile\' is mandatory');
181
        }
182 8
        $process = new CommandLine();
183 8
        $cmd     = new Cmd($this->binary);
184 8
        $process->addCommand($cmd);
185
186 8
        $cmd->addOptionIfNotEmpty('-u', $this->deleteUncrypted, false);
187 8
        $cmd->addOptionIfNotEmpty('-k', $this->key, true, ' ');
188 8
        $cmd->addOptionIfNotEmpty('-f', $this->keyFile, true, ' ');
189 8
        $cmd->addOptionIfNotEmpty('-h', $this->hash, true, ' ');
190 8
        $cmd->addOptionIfNotEmpty('-a', $this->algorithm, true, ' ');
191 8
        $cmd->addOptionIfNotEmpty('-c', $this->config, true, ' ');
192
193 8
        $cmd->addArgument($this->targetFile);
194
195 8
        return $process;
196
    }
197
}
198