Completed
Push — master ( 3eed6d...7635b2 )
by Sebastian
04:09 queued 01:07
created

RedisCli::createCommandLine()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 2
nop 0
crap 2
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
 * RedisCli 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.12
19
 */
20
class RedisCli extends Abstraction implements Executable
21
{
22
    use OptionMasker;
23
24
    /**
25
     * List of implemented redis commands
26
     *
27
     * @var array
28
     */
29
    private $availableCommands = ['BGSAVE' => true, 'LASTSAVE' => true];
30
31
    /**
32
     * Redis command to execute
33
     *
34
     * @var string
35
     */
36
    private $command;
37
38
    /**
39
     * Host to connect to
40
     * -h
41
     *
42
     * @var string
43
     */
44
    private $host;
45
46
    /**
47
     * Port to connect to
48
     * -p
49
     *
50
     * @var integer
51
     */
52
    private $port;
53
54
55
    /**
56
     * Password to connect
57
     * -a
58
     *
59
     * @var string
60
     */
61
    private $password;
62
63
    /**
64
     * Constructor.
65
     *
66
     * @param string $path
67
     */
68 13
    public function __construct(string $path = '')
69
    {
70 13
        $this->setup('redis-cli', $path);
71 13
        $this->setMaskCandidates(['password']);
72 13
    }
73
74
    /**
75
     * Set the redis-cli command to execute.
76
     *
77
     * @param  string $command
78
     * @return \phpbu\App\Cli\Executable\RedisCli
79
     * @throws \phpbu\App\Exception
80
     */
81 12
    public function runCommand(string $command) : RedisCli
82
    {
83 12
        if(!isset($this->availableCommands[$command])) {
84 1
            throw new Exception('Unknown redis-cli command');
85
        }
86 11
        $this->command = $command;
87 11
        return $this;
88
    }
89
90
    /**
91
     * Execute redis-cli BGSAVE command.
92
     *
93
     * @return \phpbu\App\Cli\Executable\RedisCli
94
     * @throws \phpbu\App\Exception
95
     */
96 10
    public function backup() : RedisCli
97
    {
98 10
        return $this->runCommand('BGSAVE');
99
    }
100
101
    /**
102
     * Execute redis-cli LASTSAVE command.
103
     *
104
     * @return \phpbu\App\Cli\Executable\RedisCli
105
     * @throws \phpbu\App\Exception
106
     */
107 6
    public function lastBackupTime() : RedisCli
108
    {
109 6
        return $this->runCommand('LASTSAVE');
110
    }
111
112
    /**
113
     * Host to connect to.
114
     *
115
     * @param  string $host
116
     * @return \phpbu\App\Cli\Executable\RedisCli
117
     */
118 7
    public function useHost(string $host) : RedisCli
119
    {
120 7
        $this->host = $host;
121 7
        return $this;
122
    }
123
124
    /**
125
     * Port to connect to.
126
     *
127
     * @param  int $port
128
     * @return \phpbu\App\Cli\Executable\RedisCli
129
     */
130 7
    public function usePort(int $port) : RedisCli
131
    {
132 7
        $this->port = $port;
133 7
        return $this;
134
    }
135
136
    /**
137
     * Password to authenticate.
138
     *
139
     * @param  string $password
140
     * @return \phpbu\App\Cli\Executable\RedisCli
141
     */
142 7
    public function usePassword(string $password) : RedisCli
143
    {
144 7
        $this->password = $password;
145 7
        return $this;
146
    }
147
148
    /**
149
     * RedisCli CommandLine generator.
150
     *
151
     * @return \SebastianFeldmann\Cli\CommandLine
152
     * @throws \phpbu\App\Exception
153
     */
154 12
    protected function createCommandLine() : CommandLine
155
    {
156 12
        if (empty($this->command)) {
157 1
            throw new Exception('Choose command to execute');
158
        }
159
160 11
        $process = new CommandLine();
161 11
        $cmd     = new Cmd($this->binary);
162 11
        $process->addCommand($cmd);
163
164 11
        $this->setOptions($cmd);
165 11
        $cmd->addOption($this->command);
166
167 11
        return $process;
168
    }
169
170
    /**
171
     * Set the openssl command line options.
172
     *
173
     * @param \SebastianFeldmann\Cli\Command\Executable $cmd
174
     */
175 11
    protected function setOptions(Cmd $cmd)
176
    {
177 11
        $cmd->addOptionIfNotEmpty('-h', $this->host, true, ' ');
178 11
        $cmd->addOptionIfNotEmpty('-p', $this->port, true, ' ');
179 11
        $cmd->addOptionIfNotEmpty('-a', $this->password, true, ' ');
180 11
    }
181
}
182