Completed
Branch master (72f544)
by Sebastian
11:13 queued 06:42
created

Mcrypt::getExecutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 15
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 11
nc 2
nop 1
crap 2
1
<?php
2
namespace phpbu\App\Backup\Crypter;
3
4
use phpbu\App\Backup\Crypter;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Cli\Executable;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
10
/**
11
 * Mcrypt crypter class.
12
 *
13
 * @package    phpbu
14
 * @subpackage Backup
15
 * @author     Sebastian Feldmann <[email protected]>
16
 * @copyright  Sebastian Feldmann <[email protected]>
17
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
18
 * @link       http://phpbu.de/
19
 * @since      Class available since Release 1.3.0
20
 */
21
class Mcrypt extends Key implements Crypter
22
{
23
    /**
24
     * Path to mcrypt command.
25
     *
26
     * @var string
27
     */
28
    private $pathToMcrypt;
29
30
    /**
31
     * Key to pass via cli
32
     *
33
     * @var string
34
     */
35
    private $key;
36
37
    /**
38
     * Key file
39
     *
40
     * @var string
41
     */
42
    private $keyFile;
43
44
    /**
45
     * Algorithm to use
46
     *
47
     * @var string
48
     */
49
    private $algorithm;
50
51
    /**
52
     * Hash to use
53
     *
54
     * @var string
55
     */
56
    private $hash;
57
58
    /**
59
     * Path to config file
60
     *
61
     * @var string
62
     */
63
    private $config;
64
65
    /**
66
     * Keep the not encrypted file
67
     *
68
     * @var boolean
69
     */
70
    private $keepUncrypted;
71
72
    /**
73
     * Setup.
74
     *
75
     * @see    \phpbu\App\Backup\Crypter
76
     * @param  array $options
77
     * @throws Exception
78
     */
79
    public function setup(array $options = array())
80
    {
81
        if (!Util\Arr::isSetAndNotEmptyString($options, 'algorithm')) {
82
            throw new Exception('mcrypt \'algorithm\' is mandatory');
83
        }
84 5
85
        $this->pathToMcrypt  = Util\Arr::getValue($options, 'pathToMcrypt');
86 5
        $this->keepUncrypted = Util\Str::toBoolean(Util\Arr::getValue($options, 'keepUncrypted', ''), false);
87 1
        $this->key           = Util\Arr::getValue($options, 'key');
88
        $this->keyFile       = $this->toAbsolutePath(Util\Arr::getValue($options, 'keyFile'));
89
        $this->algorithm     = $options['algorithm'];
90 4
        $this->hash          = Util\Arr::getValue($options, 'hash');
91 4
        $this->config        = $this->toAbsolutePath(Util\Arr::getValue($options, 'config'));
92 4
93 4
        if (empty($this->key) && empty($this->keyFile)) {
94 4
            throw new Exception('one of \'key\' or \'keyFile\' is mandatory');
95 4
        }
96 4
    }
97 4
98
    /**
99 4
     * (non-PHPDoc)
100 1
     *
101
     * @see    \phpbu\App\Backup\Crypter
102 3
     * @return string
103
     */
104
    public function getSuffix()
105
    {
106
        return 'nc';
107
    }
108
109
    /**
110
     * Create the Exec to run the 'mcrypt' command.
111
     *
112 2
     * @param  \phpbu\App\Backup\Target $target
113
     * @return \phpbu\App\Cli\Executable
114 2
     */
115
    public function getExecutable(Target $target)
116 2
    {
117
        if (null == $this->executable) {
118 2
            $this->executable = new Executable\Mcrypt($this->pathToMcrypt);
119 1
            $this->executable->useAlgorithm($this->algorithm)
120
                             ->useKey($this->key)
121 1
                             ->useKeyFile($this->keyFile)
122
                             ->useConfig($this->config)
123
                             ->useHash($this->hash)
124
                             ->saveAt($target->getPathname())
125
                             ->deleteUncrypted(!$this->keepUncrypted);
126
        }
127
128
        return $this->executable;
129 1
    }
130
}
131