Passed
Push — master ( 4c7def...9c72dc )
by CodexShaper
02:10
created

MongoDumper   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 137
Duplicated Lines 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 64
c 3
b 0
f 0
dl 0
loc 137
rs 9.76
wmc 33

8 Methods

Rating   Name   Duplication   Size   Complexity  
A restore() 0 5 2
A setAuthenticationDatabase() 0 4 1
A setUri() 0 4 1
A setCollection() 0 4 1
A dump() 0 5 2
B prepareRestoreCommand() 0 41 8
A prepareDumpCommand() 0 9 2
F prepareDumpOptions() 0 41 16
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
        $command = $this->prepareDumpOptions();
52
53
        if ($this->isCompress) {
54
            return "{$command} > {$destinationPath}{$this->compressExtension}";
55
        }
56
57
        return "{$command} --out {$destinationPath}";
58
    }
59
60
    protected function prepareDumpOptions()
61
    {
62
        $command = "{$this->dumpCommandPath}mongodump";
63
64
        if ($this->isCompress) {
65
            $command .= " --archive --gzip";
66
        }
67
68
        if ($this->uri) {
69
            $command .= " " . $this->uri;
70
        }
71
        // Database
72
        if ($this->dbName && !$this->uri) {
73
            $command .= " --db {$this->dbName}";
74
        }
75
        // Username
76
        if ($this->username && !$this->uri) {
77
            $command .= " --username {$this->username}";
78
        }
79
        //Password
80
        if ($this->password && !$this->uri) {
81
            $command .= " --password {$this->password}";
82
        }
83
        // Host
84
        if ($this->host && !$this->uri) {
85
            $command .= " --host {$this->host}";
86
        }
87
        // Port
88
        if ($this->port && !$this->uri) {
89
            $command .= " --port {$this->port}";
90
        }
91
        // Collection
92
        if ($this->collection) {
93
            $command .= " --collection {$this->collection}";
94
        }
95
        // Authentication Database
96
        if ($this->authenticationDatabase && !$this->uri) {
97
            $command .= " --authenticationDatabase {$this->authenticationDatabase}";
98
        }
99
100
        return $command;
101
    }
102
103
    protected function prepareRestoreCommand(string $filePath): string
104
    {
105
        // Username
106
        $username = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
107
        // Host
108
        $host = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
109
        // Port
110
        $port = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
111
        // Authentication Database
112
        $authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
113
        // Archive
114
        $archive = "";
115
116
        if ($this->isCompress) {
117
118
            $archive = "--gzip --archive";
119
        }
120
        // Restore Command
121
        $restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
122
            $this->dumpCommandPath,
123
            $archive,
124
            $host,
125
            $port,
126
            $username,
127
            $authenticationDatabase
128
        );
129
        // Generate restore command for uri
130
        if ($this->uri) {
131
            $restoreCommand = sprintf(
132
                '%smongorestore %s --uri %s',
133
                $this->dumpCommandPath,
134
                $archive,
135
                $this->uri
136
            );
137
        }
138
        // Check compress is enable
139
        if ($this->isCompress) {
140
            return "{$restoreCommand} < {$filePath}";
141
        }
142
143
        return "{$restoreCommand} {$filePath}";
144
    }
145
}
146