Passed
Push — master ( bd4606...0b21fb )
by CodexShaper
02:06
created

MongoDumper::prepareDumpUriCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 9
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
        $dumpCommand = $this->prepareDumpOptions();
52
53
        if ($this->uri) {
54
            $dumpCommand = $this->prepareDumpUriCommand();
55
        }
56
57
        if ($this->isCompress) {
58
            return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}";
59
        }
60
61
        return "{$dumpCommand} --out {$destinationPath}";
62
    }
63
64
    public function prepareDumpUriCommand()
65
    {
66
67
        return sprintf(
68
            '%smongodump %s --uri %s %s',
69
            $this->dumpCommandPath,
70
            $this->getArchive(),
71
            $this->uri,
72
            $collection
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $collection seems to be never defined.
Loading history...
73
        );
74
    }
75
76
    public function getArchive()
77
    {
78
        $archive = "";
79
        if ($this->isCompress) {
80
            $archive = "--archive --gzip";
81
        }
82
        return $archive;
83
    }
84
85
    public function prepareDumpOptions()
86
    {
87
        $databaseArg            = !empty($this->dbName) ? "--db " . escapeshellarg($this->dbName) : "";
88
        $username               = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
89
        $password               = !empty($this->password) ? "--password " . escapeshellarg($this->password) : "";
90
        $host                   = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
91
        $port                   = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
92
        $collection             = !empty($this->collection) ? "--collection " . escapeshellarg($this->collection) : "";
93
        $authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
94
95
        return sprintf(
96
            '%smongodump %s %s %s %s %s %s %s %s',
97
            $this->dumpCommandPath,
98
            $this->getArchive(),
99
            $databaseArg,
100
            $username,
101
            $password,
102
            $host,
103
            $port,
104
            $collection,
105
            $authenticationDatabase
106
        );
107
    }
108
109
    protected function prepareRestoreCommand(string $filePath): string
110
    {
111
        $username               = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
112
        $host                   = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
113
        $port                   = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
114
        $authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
115
116
        $archive = "";
117
        if ($this->isCompress) {
118
            $archive = "--gzip --archive";
119
        }
120
121
        $restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
122
            $this->dumpCommandPath,
123
            $archive,
124
            $host,
125
            $port,
126
            $username,
127
            $authenticationDatabase
128
        );
129
130
        if ($this->uri) {
131
            $restoreCommand = sprintf(
132
                '%smongorestore %s --uri %s',
133
                $this->dumpCommandPath,
134
                $archive,
135
                $this->uri
136
            );
137
        }
138
139
        if ($this->isCompress) {
140
141
            return "{$restoreCommand} < {$filePath}";
142
        }
143
144
        return "{$restoreCommand} {$filePath}";
145
    }
146
}
147