Passed
Push — master ( d4c14a...3f9fd3 )
by CodexShaper
13:12
created

MongoDumper::setCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
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
        $options = [
52
            'archive'                => '',
53
            'database'               => '',
54
            'username'               => '',
55
            'password'               => '',
56
            'host'                   => '',
57
            'port'                   => '',
58
            'collection'             => '',
59
            'authenticationDatabase' => '',
60
        ];
61
        // Database
62
        if (!empty($this->dbName)) {
63
            $options['database'] = "--db {$this->dbName}";
64
        }
65
        // Username
66
        if (!empty($this->username)) {
67
            $options['username'] = "--username {$this->username}";
68
        }
69
        //Password
70
        if (!empty($this->password)) {
71
            $options['password'] = "--password {$this->password}";
72
        }
73
        // Host
74
        if (!empty($this->host)) {
75
            $options['host'] = "--host {$this->host}";
76
        }
77
        // Port
78
        if (!empty($this->port)) {
79
            $options['port'] = "--port {$this->port}";
80
        }
81
        // Collection
82
        if (!empty($this->collection)) {
83
            $options['collection'] = "--collection {$this->collection}";
84
        }
85
        // Authentication Database
86
        if (!empty($this->authenticationDatabase)) {
87
            $options[] = "--authenticationDatabase {$this->authenticationDatabase}";
88
        }
89
        // Archive
90
        if ($this->isCompress) {
91
            $options['archive'] = "--archive --gzip";
92
        }
93
        // Dump Command
94
        $dumpCommand = sprintf(
95
            '%smongodump %s %s %s %s %s %s %s %s',
96
            $this->dumpCommandPath,
97
            $options['archive'],
98
            $options['database'],
99
            $options['username'],
100
            $options['password'],
101
            $options['host'],
102
            $options['port'],
103
            $options['collection'],
104
            $options['authenticationDatabase']
105
        );
106
        // Generate dump command from uri
107
        if ($this->uri) {
108
            $dumpCommand = sprintf(
109
                '%smongodump %s --uri %s %s',
110
                $this->dumpCommandPath,
111
                $options['archive'],
112
                $this->uri,
113
                $options['collection']
114
            );
115
        }
116
117
        if ($this->isCompress) {
118
            return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}";
119
        }
120
121
        return "{$dumpCommand} --out {$destinationPath}";
122
    }
123
124
    protected function prepareRestoreCommand(string $filePath): string
125
    {
126
        // Username
127
        $username = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
128
        // Host
129
        $host = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
130
        // Port
131
        $port = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
132
        // Authentication Database
133
        $authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
134
        // Archive
135
        $archive = "";
136
137
        if ($this->isCompress) {
138
139
            $archive = "--gzip --archive";
140
        }
141
        // Restore Command
142
        $restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
143
            $this->dumpCommandPath,
144
            $archive,
145
            $host,
146
            $port,
147
            $username,
148
            $authenticationDatabase
149
        );
150
        // Generate restore command for uri
151
        if ($this->uri) {
152
            $restoreCommand = sprintf(
153
                '%smongorestore %s --uri %s',
154
                $this->dumpCommandPath,
155
                $archive,
156
                $this->uri
157
            );
158
        }
159
        // Check compress is enable
160
        if ($this->isCompress) {
161
            return "{$restoreCommand} < {$filePath}";
162
        }
163
164
        return "{$restoreCommand} {$filePath}";
165
    }
166
}
167