Completed
Push — master ( f5ece7...0423d2 )
by Sebastian
05:16
created

Arangodump::handleDump()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 0
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 1
crap 6
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
 * Arangodump source class.
11
 *
12
 * @package    phpbu
13
 * @subpackage Backup
14
 * @author     Francis Chuang <[email protected]>
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 2.0.0
20
 */
21
class Arangodump extends Abstraction implements Executable
22
{
23
    use OptionMasker;
24
25
    /**
26
     * Endpoint to connect to
27
     * --server.endpoint <endpoint>
28
     *
29
     * @var string
30
     */
31
    private $endpoint;
32
33
    /**
34
     * Username to connect with
35
     * --server.username <username>
36
     *
37
     * @var string
38
     */
39
    private $username;
40
41
    /**
42
     * Password to authenticate with
43
     * --server.password <password>
44
     *
45
     * @var string
46
     */
47
    private $password;
48
49
    /**
50
     * The database to backup
51
     * --server.database <database>
52
     *
53
     * @var string
54
     */
55
    private $database;
56
57
    /**
58
     * Whether the data should be dumped or not
59
     * --dump-data
60
     *
61
     * @var boolean
62
     */
63
    private $dumpData;
64
65
    /**
66
     * Include system collections
67
     * --include-system-collections
68
     *
69
     * @var boolean
70
     */
71
    private $includeSystemCollections;
72
73
    /**
74
     * Restrict the dump to these collections
75
     * --collection
76
     *
77
     * @var array
78
     */
79
    private $collections;
80
81
    /**
82
     * Do not ask for the username and password when connecting to the server.
83
     * This does not control whether the server requires authentication.
84
     * -- disable-authentication
85
     *
86
     * @var boolean
87
     */
88
    private $disableAuthentication;
89
90
    /**
91
     * Directory to dump to.
92
     *
93
     * @var string
94
     */
95
    private $dumpDir;
96
97
    /**
98
     * Constructor.
99
     *
100 14
     * @param string $path
101
     */
102 14
    public function __construct(string $path = '')
103 14
    {
104 14
        $this->setup('arangodump', $path);
105
        $this->setMaskCandidates(['password']);
106
    }
107
108
    /**
109
     * Set target dump directory.
110
     *
111
     * @param  string $path
112 13
     * @return \phpbu\App\Cli\Executable\Arangodump
113
     */
114 13
    public function dumpTo(string $path) : Arangodump
115 13
    {
116
        $this->dumpDir = $path;
117
        return $this;
118
    }
119
120
    /**
121
     * Set user credentials.
122
     *
123
     * @param  string $username
124
     * @param  string $password
125 5
     * @return \phpbu\App\Cli\Executable\Arangodump
126
     */
127 5
    public function credentials(string $username = '', string $password = '') : Arangodump
128 5
    {
129 5
        $this->username = $username;
130
        $this->password = $password;
131
        return $this;
132
    }
133
134
    /**
135
     * Endpoint to use.
136
     *
137
     * @param  string $endpoint
138 4
     * @return \phpbu\App\Cli\Executable\Arangodump
139
     */
140 4
    public function useEndpoint(string $endpoint) : Arangodump
141 4
    {
142
        $this->endpoint = $endpoint;
143
        return $this;
144
    }
145
146
    /**
147
     * Database to dump.
148
     *
149
     * @param  string $database
150 4
     * @return \phpbu\App\Cli\Executable\Arangodump
151
     */
152 4
    public function dumpDatabase(string $database) : Arangodump
153 4
    {
154
        $this->database = $database;
155
        return $this;
156
    }
157
158
    /**
159
     * Collections to dump.
160
     *
161
     * @param  array $collections
162 4
     * @return \phpbu\App\Cli\Executable\Arangodump
163
     */
164 4
    public function dumpCollections(array $collections) : Arangodump
165 4
    {
166
        $this->collections = $collections;
167
        return $this;
168
    }
169
170
    /**
171
     * Disable authentication.
172
     *
173
     * @param  boolean $bool
174 4
     * @return \phpbu\App\Cli\Executable\Arangodump
175
     */
176 4
    public function disableAuthentication(bool $bool) : Arangodump
177 4
    {
178
        $this->disableAuthentication = $bool;
179
        return $this;
180
    }
181
182
    /**
183
     * Dump system collections.
184
     *
185
     * @param  boolean $bool
186 4
     * @return \phpbu\App\Cli\Executable\Arangodump
187
     */
188 4
    public function includeSystemCollections(bool $bool) : Arangodump
189 4
    {
190
        $this->includeSystemCollections = $bool;
191
        return $this;
192
    }
193
194
    /**
195
     * Dump data as well.
196
     *
197
     * @param  boolean $bool
198 4
     * @return \phpbu\App\Cli\Executable\Arangodump
199
     */
200 4
    public function dumpData(bool $bool) : Arangodump
201 4
    {
202
        $this->dumpData = $bool;
203
        return $this;
204
    }
205
206
    /**
207
     * Arangodump CommandLine generator.
208
     *
209
     * @return \SebastianFeldmann\Cli\CommandLine
210 14
     * @throws \phpbu\App\Exception
211
     */
212 14
    public function createCommandLine() : CommandLine
213 1
    {
214
        if (empty($this->dumpDir)) {
215
            throw new Exception('dump dir is mandatory');
216 13
        }
217 13
218 13
        $process = new CommandLine();
219
        $cmd     = new Cmd($this->binary);
220
        $process->addCommand($cmd);
221 13
222 12
        $cmd->addOptionIfNotEmpty('--server.username', $this->username, true, ' ');
223
        $cmd->addOptionIfNotEmpty('--server.password', $this->password, true, ' ');
224 12
        $cmd->addOptionIfNotEmpty('--server.endpoint', $this->endpoint, true, ' ');
225
        $cmd->addOptionIfNotEmpty('--server.database', $this->database, true, ' ');
226 13
227 13
        $this->handleCollections($cmd);
228 13
229 13
        if ($this->disableAuthentication) {
230
            $cmd->addOption('--server.disable-authentication', 'true', ' ');
231 13
        }
232 2
233 2
        $this->handleDump($cmd);
234 2
235 2
        return $process;
236
    }
237 13
238 1
    /**
239 1
     * Handle command collection settings.
240
     *
241 13
     * @param \SebastianFeldmann\Cli\Command\Executable $cmd
242 1
     */
243 1
    protected function handleCollections(Cmd $cmd)
244
    {
245 13
        if (count($this->collections)) {
246 1
            foreach ($this->collections as $collection) {
247 1
                $cmd->addOption('--collection', $collection, ' ');
248
            }
249 13
        }
250
        if ($this->includeSystemCollections) {
251 13
            $cmd->addOption('--include-system-collections', 'true', ' ');
252
        }
253
    }
254
255
    /**
256
     * Handle command data settings.
257
     *
258
     * @param \SebastianFeldmann\Cli\Command\Executable $cmd
259
     */
260
    protected function handleDump(Cmd $cmd)
261
    {
262
        if ($this->dumpData) {
263
            $cmd->addOption('--dump-data', 'true', ' ');
264
        }
265
        $cmd->addOption('--output-directory', $this->dumpDir, ' ');
266
    }
267
}
268