MongoDb::dumpToFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12

Duplication

Lines 12
Ratio 100 %

Importance

Changes 0
Metric Value
dl 12
loc 12
rs 9.8666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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)
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 = 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) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
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) {
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...
117
            $command[] = "--authenticationDatabase {$this->authenticationDatabase}";
118
        }
119
120
        return $this->echoToFile(implode(' ', $command), $filename);
121
    }
122
}
123