Passed
Push — master ( 08a73c...a7d833 )
by Sebastian
01:49 queued 12s
created

Mysql::useProtocol()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace phpbu\App\Cli\Executable;
3
4
use phpbu\App\Cli\Executable;
5
use SebastianFeldmann\Cli\CommandLine;
6
use SebastianFeldmann\Cli\Command\Executable as Cmd;
7
8
/**
9
 * Mysql Executable class.
10
 *
11
 * @package    phpbu
12
 * @subpackage Backup
13
 * @author     Sebastian Feldmann <[email protected]>
14
 * @copyright  Sebastian Feldmann <[email protected]>
15
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
16
 * @link       http://phpbu.de/
17
 * @since      Class available since Release 6.0-dev
18
 */
19
class Mysql extends Abstraction implements Executable
20
{
21
    use OptionMasker;
22
23
    /**
24
     * Host to connect to
25
     * --host <hostname>
26
     *
27
     * @var string
28
     */
29
    private $host;
30
31
    /**
32
     * Port to connect to
33
     * --port <number>
34
     *
35
     * @var int
36
     */
37
    private $port;
38
39
    /**
40
     * The connection protocol
41
     * --protocol
42
     *
43
     * @var string
44
     */
45
    private $protocol;
46
47
    /**
48
     * User to connect with
49
     * --user <username>
50
     *
51
     * @var string
52
     */
53
    private $user;
54
55
    /**
56
     * Password to authenticate with
57
     * --password <password>
58
     *
59
     * @var string
60
     */
61
    private $password;
62
63
    /**
64
     * Database to connect to
65
     * --database <database>
66
     *
67
     * @var string
68
     */
69
    private $database;
70
71
    /**
72
     * Use mysql quick mode
73
     * -q
74
     *
75
     * @var bool
76
     */
77
    private $quick = false;
78
79
    /**
80
     * Use mysql with compression
81
     * -C
82
     *
83
     * @var bool
84
     */
85
    private $compress = false;
86
87
    /**
88
     * Name of the source file.
89
     * -e "source $file"
90
     *
91
     * @var string
92
     */
93
    private $sourceFilename;
94
95
    /**
96
     * Constructor.
97
     *
98
     * @param string $path
99
     */
100 11
    public function __construct(string $path = '')
101
    {
102 11
        $this->setup('mysql', $path);
103 11
        $this->setMaskCandidates(['password']);
104 11
    }
105
106
    /**
107
     * Set the credentials.
108
     *
109
     * @param  string $user
110
     * @param  string $password
111
     * @return \phpbu\App\Cli\Executable\Mysql
112
     */
113 4
    public function credentials(string $user = '', string $password = '') : Mysql
114
    {
115 4
        $this->user     = $user;
116 4
        $this->password = $password;
117 4
        return $this;
118
    }
119
120
    /**
121
     * Set the hostname.
122
     *
123
     * @param  string $host
124
     * @return \phpbu\App\Cli\Executable\Mysql
125
     */
126 3
    public function useHost(string $host) : Mysql
127
    {
128 3
        $this->host = $host;
129 3
        return $this;
130
    }
131
132
    /**
133
     * Set the port.
134
     *
135
     * @param  int $port
136
     * @return \phpbu\App\Cli\Executable\Mysql
137
     */
138 3
    public function usePort(int $port) : Mysql
139
    {
140 3
        $this->port = $port;
141 3
        return $this;
142
    }
143
144
    /**
145
     * Set the connection protocol.
146
     *
147
     * @param  string $protocol
148
     * @return \phpbu\App\Cli\Executable\Mysql
149
     */
150 3
    public function useProtocol(string $protocol) : Mysql
151
    {
152 3
        $this->protocol = $protocol;
153 3
        return $this;
154
    }
155
156
    /**
157
     * Set the database.
158
     *
159
     * @param  string $database
160
     * @return \phpbu\App\Cli\Executable\Mysql
161
     */
162 2
    public function useDatabase(string $database) : Mysql
163
    {
164 2
        $this->database = $database;
165 2
        return $this;
166
    }
167
168
    /**
169
     * Use '-q' quick mode.
170
     *
171
     * @param  boolean $bool
172
     * @return \phpbu\App\Cli\Executable\Mysql
173
     */
174 2
    public function useQuickMode(bool $bool) : Mysql
175
    {
176 2
        $this->quick = $bool;
177 2
        return $this;
178
    }
179
180
    /**
181
     * Use '-C' compress mode.
182
     *
183
     * @param  bool $bool
184
     * @return \phpbu\App\Cli\Executable\Mysql
185
     */
186 2
    public function useCompression(bool $bool) : Mysql
187
    {
188 2
        $this->compress = $bool;
189 2
        return $this;
190
    }
191
192
    /**
193
     * Set the source filename.
194
     *
195
     * @param string $sourceFilename
196
     *
197
     * @return \phpbu\App\Cli\Executable\Mysql
198
     */
199 3
    public function useSourceFile(string $sourceFilename) : Mysql
200
    {
201 3
        $this->sourceFilename = $sourceFilename;
202 3
        return $this;
203
    }
204
205
    /**
206
     * Mysql CommandLine generator.
207
     *
208
     * @return \SebastianFeldmann\Cli\CommandLine
209
     * @throws \phpbu\App\Exception
210
     */
211 11
    protected function createCommandLine() : CommandLine
212
    {
213 11
        $process = new CommandLine();
214 11
        $cmd     = new Cmd($this->binary);
215 11
        $process->addCommand($cmd);
216
217 11
        $cmd->addOptionIfNotEmpty('--user', $this->user);
218 11
        $cmd->addOptionIfNotEmpty('--password', $this->password);
219 11
        $cmd->addOptionIfNotEmpty('--host', $this->host);
220 11
        $cmd->addOptionIfNotEmpty('--port', $this->port);
221 11
        $cmd->addOptionIfNotEmpty('--protocol', $this->protocol);
222 11
        $cmd->addOptionIfNotEmpty('--database', $this->database);
223 11
        $cmd->addOptionIfNotEmpty('-q', $this->quick, false);
224 11
        $cmd->addOptionIfNotEmpty('-C', $this->compress, false);
225
226 11
        if (!empty($this->sourceFilename)) {
227 3
            $cmd->addOption('--execute', 'source '.$this->sourceFilename);
228
        }
229
230 11
        return $process;
231
    }
232
}
233