Completed
Push — master ( 8b2ec6...0e6a6f )
by CodexShaper
02:08
created

MongoDumper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 121
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 8
Bugs 0 Features 0
Metric Value
eloc 61
c 8
b 0
f 0
dl 0
loc 121
ccs 0
cts 67
cp 0
rs 10
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A setAuthenticationDatabase() 0 4 1
A setUri() 0 4 1
A setCollection() 0 4 1
A dump() 0 6 2
A restore() 0 6 2
A prepareDumpCommand() 0 32 4
A prepareRestoreCommand() 0 28 4
A prepareAuthenticateDatabase() 0 3 2
A preparePassword() 0 3 2
A prepareCollection() 0 3 2
1
<?php
2
3
namespace CodexShaper\Dumper\Drivers;
4
5
use CodexShaper\Dumper\Dumper;
6
7
class MongoDumper extends Dumper
8
{
9
    /*@var int*/
10
    protected $port = 27017;
11
    /*@var string*/
12
    protected $collection = "";
13
    /*@var string*/
14
    protected $authenticationDatabase = "";
15
    /*@var string*/
16
    protected $uri = "";
17
18
    public function setUri(string $uri)
19
    {
20
        $this->uri = $uri;
21
        return $this;
22
    }
23
24
    public function setCollection(string $collection)
25
    {
26
        $this->collection = $collection;
27
        return $this;
28
    }
29
    public function setAuthenticationDatabase(string $authenticationDatabase)
30
    {
31
        $this->authenticationDatabase = $authenticationDatabase;
32
        return $this;
33
    }
34
35
    public function dump(string $destinationPath = "")
36
    {
37
        $destinationPath = !empty($destinationPath) ? '"' . $destinationPath . '"' : '"' . $this->destinationPath . '"';
38
        $dumpCommand     = $this->prepareDumpCommand($destinationPath);
39
        $this->command   = $this->removeExtraSpaces($dumpCommand);
40
        $this->run();
41
    }
42
43
    public function restore(string $restorePath = "")
44
    {
45
        $restorePath    = !empty($restorePath) ? '"' . $restorePath . '"' : '"' . $this->restorePath . '"';
46
        $restoreCommand = $this->prepareRestoreCommand($restorePath);
47
        $this->command  = $this->removeExtraSpaces($restoreCommand);
48
        $this->run();
49
    }
50
51
    protected function prepareDumpCommand(string $destinationPath): string
52
    {
53
        $archive = $this->isCompress ? "--archive --gzip" : "";
54
55
        $dumpCommand = sprintf(
56
            '%s %s %s %s %s %s %s %s %s',
57
            $this->quoteCommand($this->commandBinaryPath . 'mongodump'),
58
            $archive,
59
            $this->prepareDatabase(),
60
            $this->prepareUserName(),
61
            $this->preparePassword(),
62
            $this->prepareHost(),
63
            $this->preparePort(),
64
            $this->prepareCollection(),
65
            $this->prepareAuthenticateDatabase()
66
        );
67
68
        if ($this->uri) {
69
            $dumpCommand = sprintf(
70
                '%s %s --uri %s %s',
71
                $this->quoteCommand($this->commandBinaryPath . 'mongodump'),
72
                $archive,
73
                $this->uri,
74
                $this->prepareCollection()
75
            );
76
        }
77
78
        if ($this->isCompress) {
79
            return "{$dumpCommand} > \"{$destinationPath}{$this->compressExtension}\"";
80
        }
81
82
        return "{$dumpCommand} --out \"{$destinationPath}\"";
83
    }
84
85
    protected function prepareRestoreCommand(string $filePath): string
86
    {
87
88
        $archive = $this->isCompress ? "--gzip --archive" : "";
89
90
        $restoreCommand = sprintf("%s %s %s %s %s %s",
91
            $this->quoteCommand($this->commandBinaryPath . 'mongorestore'),
92
            $archive,
93
            $this->prepareHost(),
94
            $this->preparePort(),
95
            $this->prepareUserName(),
96
            $this->prepareAuthenticateDatabase()
97
        );
98
99
        if ($this->uri) {
100
            $restoreCommand = sprintf(
101
                '%smongorestore %s --uri %s',
102
                $this->commandBinaryPath,
103
                $archive,
104
                $this->uri
105
            );
106
        }
107
108
        if ($this->isCompress) {
109
            return "{$restoreCommand} < \"{$filePath}\"";
110
        }
111
112
        return "{$restoreCommand} \"{$filePath}\"";
113
    }
114
115
    public function preparePassword()
116
    {
117
        return !empty($this->password) ? "--password {$this->password}" : "";
118
    }
119
120
    public function prepareAuthenticateDatabase()
121
    {
122
        return !empty($this->authenticationDatabase) ? "--authenticationDatabase {$this->authenticationDatabase}" : "";
123
    }
124
125
    public function prepareCollection()
126
    {
127
        return !empty($this->collection) ? "--collection {$this->collection}" : "";
128
    }
129
}
130