Completed
Push — master ( 98a3b7...d114fb )
by Freek
02:07
created

MongoDb::getDumpCommand()   C

Complexity

Conditions 7
Paths 64

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 18
nc 64
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 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)
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...
28
    {
29
        $this->guardAgainstIncompleteCredentials();
30
31
        $command = $this->getDumpCommand($dumpFile);
32
33
        $process = new Process($command);
34
35
        if (! is_null($this->timeout)) {
36
            $process->setTimeout($this->timeout);
37
        }
38
39
        $process->run();
40
41
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
42
    }
43
44
    /**
45
     * Verifies if the dbname and host options are set.
46
     *
47
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
48
     * @return void
49
     */
50
    protected function guardAgainstIncompleteCredentials()
51
    {
52
        foreach (['dbName', 'host'] as $requiredProperty) {
53
            if (strlen($this->$requiredProperty) === 0) {
54
                throw CannotStartDump::emptyParameter($requiredProperty);
55
            }
56
        }
57
    }
58
59
    /**
60
     * @param string $collection
61
     *
62
     * @return \Spatie\DbDumper\Databases\MongoDb
63
     */
64
    public function setCollection(string $collection)
65
    {
66
        $this->collection = $collection;
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string $authenticationDatabase
73
     *
74
     * @return \Spatie\DbDumper\Databases\MongoDb
75
     */
76
    public function setAuthenticationDatabase(string $authenticationDatabase)
77
    {
78
        $this->authenticationDatabase = $authenticationDatabase;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Generate the dump command for MongoDb.
85
     *
86
     * @param string $filename
87
     *
88
     * @return string
89
     */
90
    public function getDumpCommand(string $filename) : string
91
    {
92
        $command = [
93
            "'{$this->dumpBinaryPath}mongodump'",
94
            "--db {$this->dbName}",
95
            "--archive",
96
        ];
97
98
        if ($this->userName) {
99
            $command[] = "--username '{$this->userName}'";
100
        }
101
102
        if ($this->password) {
103
            $command[] = "--password '{$this->password}'";
104
        }
105
106
        if (isset($this->host)) {
107
            $command[] = "--host {$this->host}";
108
        }
109
110
        if (isset($this->port)) {
111
            $command[] = "--port {$this->port}";
112
        }
113
114
        if (isset($this->collection)) {
115
            $command[] = "--collection {$this->collection}";
116
        }
117
118
        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...
119
            $command[] = "--authenticationDatabase {$this->authenticationDatabase}";
120
        }
121
122
        return $this->echoToFile(implode(' ', $command), $filename);
123
    }
124
}
125