Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Mongodump::excludeCollections()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
ccs 2
cts 2
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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
 * Mongodump 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 Mongodump extends Abstraction implements Executable
21
{
22
    use OptionMasker;
23
24
    /**
25
     * Dump Directory
26
     *
27
     * @var string
28
     */
29
    private $dumpDir;
30
31
    /**
32
     * Use IPv6
33
     * --ipv6
34
     *
35
     * @var boolean
36
     */
37
    private $useIPv6;
38
39
    /**
40
     * Host to connect to
41
     * --host <hostname:port>
42
     *
43
     * @var string
44
     */
45
    private $host;
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 use for authentication
65
     * --authenticationDatabase <dbname>
66
     *
67
     * @var string
68
     */
69
    private $authenticationDatabase;
70
71
    /**
72
     * List of databases to backup
73
     * --db <database>
74
     *
75
     * @var array
76
     */
77
    private $databases;
78
79
    /**
80
     * List of collections to backup
81
     * --collection <collection>
82
     *
83
     * @var array
84
     */
85
    private $collections;
86
87
    /**
88
     * List of collections to ignore
89
     * --excludeCollections array of strings
90
     *
91
     * @var array
92
     */
93
    private $excludeCollections;
94
95
    /**
96
     * List of prefixes to exclude collections
97
     * --excludeCollectionWithPrefix array of strings
98
     *
99
     * @var array
100
     */
101
    private $excludeCollectionsWithPrefix;
102
103
    /**
104
     * Constructor.
105
     *
106 12
     * @param string $path
107
     */
108 12
    public function __construct(string $path = '')
109 12
    {
110 12
        $this->setup('mongodump', $path);
111
        $this->setMaskCandidates(['password']);
112
    }
113
114
    /**
115
     * Set path to dump to.
116
     *
117
     * @param  string $path
118 11
     * @return \phpbu\App\Cli\Executable\Mongodump
119
     */
120 11
    public function dumpToDirectory(string $path) : Mongodump
121 11
    {
122
        $this->dumpDir = $path;
123
        return $this;
124
    }
125
126
    /**
127
     * Use ipv6 to connect.
128
     *
129
     * @param  boolean $bool
130 2
     * @return \phpbu\App\Cli\Executable\Mongodump
131
     */
132 2
    public function useIpv6(bool $bool) : Mongodump
133 2
    {
134
        $this->useIPv6 = $bool;
135
        return $this;
136
    }
137
138
    /**
139
     * Set host to dump from.
140
     *
141
     * @param  string $host
142 2
     * @return \phpbu\App\Cli\Executable\Mongodump
143
     */
144 2
    public function useHost(string $host) : Mongodump
145 2
    {
146
        $this->host = $host;
147
        return $this;
148
    }
149
150
    /**
151
     * Set credentials.
152
     *
153
     * @param  string $user
154
     * @param  string $password
155
     * @param  string $authDatabase
156 3
     * @return \phpbu\App\Cli\Executable\Mongodump
157
     */
158 3
    public function credentials(string $user = '', string $password = '', string $authDatabase = '') : Mongodump
159 3
    {
160 3
        $this->user                   = $user;
161 3
        $this->password               = $password;
162
        $this->authenticationDatabase = $authDatabase;
163
        return $this;
164
    }
165
166
    /**
167
     * Dump only given databases.
168
     *
169
     * @param  array $databases
170 2
     * @return \phpbu\App\Cli\Executable\Mongodump
171
     */
172 2
    public function dumpDatabases(array $databases) : Mongodump
173 2
    {
174
        $this->databases = $databases;
175
        return $this;
176
    }
177
178
    /**
179
     * Dump only given collections.
180
     *
181
     * @param  array $collections
182 2
     * @return \phpbu\App\Cli\Executable\Mongodump
183
     */
184 2
    public function dumpCollections(array $collections) : Mongodump
185 2
    {
186
        $this->collections = $collections;
187
        return $this;
188
    }
189
190
    /**
191
     * Exclude collections.
192
     *
193
     * @param  array $collections
194 2
     * @return \phpbu\App\Cli\Executable\Mongodump
195
     */
196 2
    public function excludeCollections(array $collections) : Mongodump
197 2
    {
198
        $this->excludeCollections = $collections;
199
        return $this;
200
    }
201
202
    /**
203
     * Exclude collections with given prefixes.
204
     *
205
     * @param  array $prefixes
206 2
     * @return \phpbu\App\Cli\Executable\Mongodump
207
     */
208 2
    public function excludeCollectionsWithPrefix(array $prefixes) : Mongodump
209 2
    {
210
        $this->excludeCollectionsWithPrefix = $prefixes;
211
        return $this;
212
    }
213
214
    /**
215
     * Mongodump CommandLine generator.
216
     *
217
     * @return \SebastianFeldmann\Cli\CommandLine
218 12
     * @throws \phpbu\App\Exception
219
     */
220 12
    protected function createCommandLine() : CommandLine
221 1
    {
222
        if (empty($this->dumpDir)) {
223 11
            throw new Exception('no directory to dump to');
224 11
        }
225 11
        $process = new CommandLine();
226
        $cmd     = new Cmd($this->binary);
227
        $process->addCommand($cmd);
228 11
229 10
        $cmd->addOption('--out', $this->dumpDir, ' ');
230
        $cmd->addOptionIfNotEmpty('--ipv6', $this->useIPv6, false);
231 10
        $cmd->addOptionIfNotEmpty('--host', $this->host, true, ' ');
232
        $cmd->addOptionIfNotEmpty('--user', $this->user, true, ' ');
233 11
        $cmd->addOptionIfNotEmpty('--password', $this->password, true, ' ');
234 11
        $cmd->addOptionIfNotEmpty('--authenticationDatabase', $this->authenticationDatabase, true, ' ');
235 11
236 11
        if (count($this->databases)) {
237 11
            foreach ($this->databases as $db) {
238 11
                $cmd->addOption('--db', $db, ' ');
239
            }
240 11
        }
241 1
242 1
        if (count($this->collections)) {
243 1
            foreach ($this->collections as $col) {
244 1
                $cmd->addOption('--collection', $col, ' ');
245
            }
246 11
        }
247 1
248 1
        $cmd->addOptionIfNotEmpty('--excludeCollection', $this->excludeCollections);
249 1
        $cmd->addOptionIfNotEmpty('--excludeCollectionWithPrefix', $this->excludeCollectionsWithPrefix);
250 1
251
        return $process;
252 11
    }
253
}
254