Completed
Push — master ( 3495a1...cbfb36 )
by Freek
01:51
created

MongoDb::enableCompression()   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 0
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
    /**
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 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...
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
     * @return \Spatie\DbDumper\Databases\MongoDb
73
     */
74
    public function enableCompression()
75
    {
76
        $this->enableCompression = true;
77
78
        return $this;
79
    }
80
81
    /**
82
     * Generate the dump command for MongoDb.
83
     *
84
     * @param string $filename
85
     *
86
     * @return string
87
     */
88
    public function getDumpCommand(string $filename) : string
89
    {
90
        $command = [
91
            "'{$this->dumpBinaryPath}mongodump'",
92
            "--db {$this->dbName}",
93
            "--archive=$filename",
94
        ];
95
96
        if (isset($this->userName)) {
97
            $command[] = "--username {$this->userName}";
98
        }
99
100
        if (isset($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->enableCompression) {
117
            $command[] = '--gzip';
118
        }
119
120
        return implode(' ', $command);
121
    }
122
}
123