Completed
Push — master ( 34457b...994c9e )
by Freek
01:28
created

MongoDb::getDumpCommand()   D

Complexity

Conditions 8
Paths 128

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 4.6666
c 0
b 0
f 0
cc 8
eloc 20
nc 128
nop 1
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Symfony\Component\Process\Process;
7
use Spatie\DbDumper\Exceptions\CannotStartDump;
8
9
class MongoDb extends DbDumper
10
{
11
    protected $port = 27017;
12
13
    /** @var null|string */
14
    protected $collection = null;
15
16
    /** @var bool */
17
    protected $enableCompression = false;
18
19
    /** @var null|string */
20
    protected $authenticationDatabase = null;
21
22
    /**
23
     * Dump the contents of the database to the given file.
24
     *
25
     * @param string $dumpFile
26
     *
27
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
28
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
29
     */
30 View Code Duplication
    public function dumpToFile(string $dumpFile)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
31
    {
32
        $this->guardAgainstIncompleteCredentials();
33
34
        $command = $this->getDumpCommand($dumpFile);
35
36
        $process = new Process($command);
37
38
        if (! is_null($this->timeout)) {
39
            $process->setTimeout($this->timeout);
40
        }
41
42
        $process->run();
43
44
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
45
    }
46
47
    /**
48
     * Verifies if the dbname and host options are set.
49
     *
50
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
51
     * @return void
52
     */
53 View Code Duplication
    protected function guardAgainstIncompleteCredentials()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
54
    {
55
        foreach (['dbName', 'host'] as $requiredProperty) {
56
            if (strlen($this->$requiredProperty) === 0) {
57
                throw CannotStartDump::emptyParameter($requiredProperty);
58
            }
59
        }
60
    }
61
62
    /**
63
     * @param string $collection
64
     *
65
     * @return \Spatie\DbDumper\Databases\MongoDb
66
     */
67
    public function setCollection(string $collection)
68
    {
69
        $this->collection = $collection;
70
71
        return $this;
72
    }
73
74
    /**
75
     * @return \Spatie\DbDumper\Databases\MongoDb
76
     */
77
    public function enableCompression()
78
    {
79
        $this->enableCompression = true;
80
81
        return $this;
82
    }
83
84
    /**
85
     * @param string $authenticationDatabase
86
     *
87
     * @return \Spatie\DbDumper\Databases\MongoDb
88
     */
89
    public function setAuthenticationDatabase(string $authenticationDatabase)
90
    {
91
        $this->authenticationDatabase = $authenticationDatabase;
92
93
        return $this;
94
    }
95
96
    /**
97
     * Generate the dump command for MongoDb.
98
     *
99
     * @param string $filename
100
     *
101
     * @return string
102
     */
103
    public function getDumpCommand(string $filename) : string
104
    {
105
        $command = [
106
            "'{$this->dumpBinaryPath}mongodump'",
107
            "--db {$this->dbName}",
108
            "--archive=$filename",
109
        ];
110
111
        if ($this->userName) {
112
            $command[] = "--username '{$this->userName}'";
113
        }
114
115
        if ($this->password) {
116
            $command[] = "--password '{$this->password}'";
117
        }
118
119
        if (isset($this->host)) {
120
            $command[] = "--host {$this->host}";
121
        }
122
123
        if (isset($this->port)) {
124
            $command[] = "--port {$this->port}";
125
        }
126
127
        if (isset($this->collection)) {
128
            $command[] = "--collection {$this->collection}";
129
        }
130
131
        if ($this->authenticationDatabase) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->authenticationDatabase of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
132
            $command[] = "--authenticationDatabase {$this->authenticationDatabase}";
133
        }
134
135
        if ($this->enableCompression) {
136
            $command[] = '--gzip';
137
        }
138
139
        return implode(' ', $command);
140
    }
141
}
142