1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper\Databases; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\DbDumper; |
6
|
|
|
use Spatie\DbDumper\Exceptions\CannotStartDump; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
class MongoDb extends DbDumper |
10
|
|
|
{ |
11
|
|
|
protected $port = 27017; |
12
|
|
|
|
13
|
|
|
/** @var null|string */ |
14
|
|
|
protected $collection = null; |
15
|
|
|
|
16
|
|
|
/** @var null|string */ |
17
|
|
|
protected $authenticationDatabase = null; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Dump the contents of the database to the given file. |
21
|
|
|
* |
22
|
|
|
* @param string $dumpFile |
23
|
|
|
* |
24
|
|
|
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
25
|
|
|
* @throws \Spatie\DbDumper\Exceptions\DumpFailed |
26
|
|
|
*/ |
27
|
|
View Code Duplication |
public function dumpToFile(string $dumpFile) |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$this->guardAgainstIncompleteCredentials(); |
30
|
|
|
|
31
|
|
|
$command = $this->getDumpCommand($dumpFile); |
32
|
|
|
|
33
|
|
|
$process = Process::fromShellCommandline($command, null, null, null, $this->timeout); |
34
|
|
|
|
35
|
|
|
$process->run(); |
36
|
|
|
|
37
|
|
|
$this->checkIfDumpWasSuccessFul($process, $dumpFile); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Verifies if the dbname and host options are set. |
42
|
|
|
* |
43
|
|
|
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
protected function guardAgainstIncompleteCredentials() |
47
|
|
|
{ |
48
|
|
View Code Duplication |
foreach (['dbName', 'host'] as $requiredProperty) { |
|
|
|
|
49
|
|
|
if (strlen($this->$requiredProperty) === 0) { |
50
|
|
|
throw CannotStartDump::emptyParameter($requiredProperty); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $collection |
57
|
|
|
* |
58
|
|
|
* @return \Spatie\DbDumper\Databases\MongoDb |
59
|
|
|
*/ |
60
|
|
|
public function setCollection(string $collection) |
61
|
|
|
{ |
62
|
|
|
$this->collection = $collection; |
63
|
|
|
|
64
|
|
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $authenticationDatabase |
69
|
|
|
* |
70
|
|
|
* @return \Spatie\DbDumper\Databases\MongoDb |
71
|
|
|
*/ |
72
|
|
|
public function setAuthenticationDatabase(string $authenticationDatabase) |
73
|
|
|
{ |
74
|
|
|
$this->authenticationDatabase = $authenticationDatabase; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Generate the dump command for MongoDb. |
81
|
|
|
* |
82
|
|
|
* @param string $filename |
83
|
|
|
* |
84
|
|
|
* @return string |
85
|
|
|
*/ |
86
|
|
|
public function getDumpCommand(string $filename): string |
87
|
|
|
{ |
88
|
|
|
$quote = $this->determineQuote(); |
89
|
|
|
|
90
|
|
|
$command = [ |
91
|
|
|
"{$quote}{$this->dumpBinaryPath}mongodump{$quote}", |
92
|
|
|
"--db {$this->dbName}", |
93
|
|
|
'--archive', |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
if ($this->userName) { |
97
|
|
|
$command[] = "--username '{$this->userName}'"; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($this->password) { |
101
|
|
|
$command[] = "--password '{$this->password}'"; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
if (isset($this->host)) { |
105
|
|
|
$command[] = "--host {$this->host}"; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (isset($this->port)) { |
109
|
|
|
$command[] = "--port {$this->port}"; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($this->collection)) { |
113
|
|
|
$command[] = "--collection {$this->collection}"; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($this->authenticationDatabase) { |
|
|
|
|
117
|
|
|
$command[] = "--authenticationDatabase {$this->authenticationDatabase}"; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $this->echoToFile(implode(' ', $command), $filename); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.