Completed
Push — master ( ab5bc5...b84e40 )
by Freek
01:45
created

MongoDb::setCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
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
    /**
12
     * Mongodb Port.
13
     * @var int
14
     */
15
    protected $port = 27017;
16
17
    /**
18
     * Collection to dump.
19
     * @var null|string
20
     */
21
    protected $collection = null;
22
23
    /**
24
     * Dump the contents of the database to the given file.
25
     *
26
     * @param string $dumpFile
27
     *
28
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
29
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
30
     */
31 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...
32
    {
33
        $this->guardAgainstIncompleteCredentials();
34
35
        $command = $this->getDumpCommand($dumpFile);
36
37
        $process = new Process($command);
38
39
        if (! is_null($this->timeout)) {
40
            $process->setTimeout($this->timeout);
41
        }
42
43
        $process->run();
44
45
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
46
    }
47
48
    /**
49
     * Verifies if the dbname and host options are set.
50
     *
51
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
52
     * @return void
53
     */
54 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...
55
    {
56
        foreach (['dbName', 'host'] as $requiredProperty) {
57
            if (strlen($this->$requiredProperty) === 0) {
58
                throw CannotStartDump::emptyParameter($requiredProperty);
59
            }
60
        }
61
    }
62
63
    /**
64
     * Set the collection property.
65
     *
66
     * @param  string $collection
67
     * @return \Spatie\DbDumper\Databases\MongoDb
68
     */
69
    public function setCollection(string $collection)
70
    {
71
        $this->collection = $collection;
72
73
        return $this;
74
    }
75
76
    /**
77
     * Generate the dump command for MongoDb.
78
     *
79
     * @param  string $filename
80
     * @return string
81
     */
82
    public function getDumpCommand(string $filename) : string
83
    {
84
        $command = [
85
            "'{$this->dumpBinaryPath}mongodump'",
86
            "--db {$this->dbName}",
87
            '--gzip',
88
            "--archive=$filename",
89
        ];
90
91
        if (isset($this->userName)) {
92
            $command[] = "--username {$this->userName}";
93
        }
94
95
        if (isset($this->password)) {
96
            $command[] = "--password {$this->password}";
97
        }
98
99
        if (isset($this->host)) {
100
            $command[] = "--host {$this->host}";
101
        }
102
103
        if (isset($this->port)) {
104
            $command[] = "--port {$this->port}";
105
        }
106
107
        if (isset($this->collection)) {
108
            $command[] = "--collection {$this->collection}";
109
        }
110
111
        return implode(' ', $command);
112
    }
113
}
114