Passed
Push — master ( 05b6e4...08159b )
by CodexShaper
02:17
created

MongoDumper::prepareCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 6
rs 10
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 = "admin";
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
        $this->command   = $this->prepareDumpCommand($destinationPath);
39
        $this->run();
40
    }
41
42
    public function restore(string $restorePath = "")
43
    {
44
        $restorePath   = !empty($restorePath) ? $restorePath : $this->restorePath;
45
        $this->command = $this->prepareRestoreCommand($restorePath);
46
        $this->run();
47
    }
48
49
    protected function prepareDumpCommand(string $destinationPath): string
50
    {
51
        $archive = $this->isCompress ? "--archive --gzip" : "";
52
53
        $dumpCommand = sprintf(
54
            '%smongodump %s %s %s %s %s %s %s %s',
55
            $this->dumpCommandPath,
56
            $archive,
57
            $this->prepareDatabase(),
58
            $this->prepareUsername(),
59
            $this->preparePassword(),
60
            $this->prepareHost(),
61
            $this->preparePort(),
62
            $this->prepareCollection(),
63
            $this->prepareAuthenticateDatabase()
64
        );
65
66
        if ($this->uri) {
67
            $dumpCommand = sprintf(
68
                '%smongodump %s --uri %s %s',
69
                $this->dumpCommandPath,
70
                $archive,
71
                $this->uri,
72
                $this->prepareCollection()
73
            );
74
        }
75
76
        if ($this->isCompress) {
77
            return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}";
78
        }
79
80
        return "{$dumpCommand} --out {$destinationPath}";
81
    }
82
83
    protected function prepareRestoreCommand(string $filePath): string
84
    {
85
86
        $archive = $this->isCompress ? "--gzip --archive" : "";
87
88
        $restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
89
            $this->dumpCommandPath,
90
            $archive,
91
            $this->prepareHost(),
92
            $this->preparePort(),
93
            $this->prepareUsername(),
94
            $this->prepareAuthenticateDatabase()
95
        );
96
97
        if ($this->uri) {
98
            $restoreCommand = sprintf(
99
                '%smongorestore %s --uri %s',
100
                $this->dumpCommandPath,
101
                $archive,
102
                $this->uri
103
            );
104
        }
105
106
        if ($this->isCompress) {
107
108
            return "{$restoreCommand} < {$filePath}";
109
        }
110
111
        return "{$restoreCommand} {$filePath}";
112
    }
113
114
    public function prepareDatabase()
115
    {
116
        return !empty($this->dbName) ? "--db {$this->dbName}" : "";
117
    }
118
119
    public function prepareUsername()
120
    {
121
        return !empty($this->username) ? "--username {$this->username}" : "";
122
    }
123
124
    public function preparePassword()
125
    {
126
        return !empty($this->password) ? "--password {$this->password}" : "";
127
    }
128
129
    public function prepareHost()
130
    {
131
        return !empty($this->host) ? "--host {$this->host}" : "";
132
    }
133
134
    public function preparePort()
135
    {
136
        return !empty($this->port) ? "--port {$this->port}" : "";
137
    }
138
139
    public function prepareAuthenticateDatabase()
140
    {
141
        return !empty($this->authenticationDatabase) ? "--authenticationDatabase {$this->authenticationDatabase}" : "";
142
    }
143
144
    public function prepareCollection()
145
    {
146
        return !empty($this->collection) ? "--collection {$this->collection}" : "";
147
    }
148
}
149