Completed
Push — master ( bcd560...9ae212 )
by CodexShaper
02:01
created

MongoDumper::prepareCollection()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 1
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 2
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 = "";
15
    /*@var string*/
16
    protected $uri = "";
17
18
    public function setUri(string $uri)
19
    {
20
        $this->uri = $uri;
21
        return $this;
22
    }
23
24 2
    public function setCollection(string $collection)
25
    {
26 2
        $this->collection = $collection;
27 2
        return $this;
28
    }
29 2
    public function setAuthenticationDatabase(string $authenticationDatabase)
30
    {
31 2
        $this->authenticationDatabase = $authenticationDatabase;
32 2
        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 16
    protected function prepareDumpCommand(string $destinationPath): string
52
    {
53 16
        $archive = $this->isCompress ? "--archive --gzip" : "";
54
55 16
        $dumpCommand = sprintf(
56 16
            '%s %s %s %s %s %s %s %s %s',
57 16
            $this->quoteCommand($this->commandBinaryPath . 'mongodump'),
58 8
            $archive,
59 16
            $this->prepareDatabase(),
60 16
            $this->prepareUserName(),
61 16
            $this->preparePassword(),
62 16
            $this->prepareHost(),
63 16
            $this->preparePort(),
64 16
            $this->prepareCollection(),
65 16
            $this->prepareAuthenticateDatabase()
66
        );
67
68 16
        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 16
        if ($this->isCompress) {
79 2
            return "{$dumpCommand} > \"{$destinationPath}{$this->compressExtension}\"";
80
        }
81
82 14
        return "{$dumpCommand} --out \"{$destinationPath}\"";
83
    }
84
85 12
    protected function prepareRestoreCommand(string $filePath): string
86
    {
87
88 12
        $archive = $this->isCompress ? "--gzip --archive" : "";
89
90 12
        $restoreCommand = sprintf("%s %s %s %s %s %s",
91 12
            $this->quoteCommand($this->commandBinaryPath . 'mongorestore'),
92 6
            $archive,
93 12
            $this->prepareHost(),
94 12
            $this->preparePort(),
95 12
            $this->prepareUserName(),
96 12
            $this->prepareAuthenticateDatabase()
97
        );
98
99 12
        if ($this->uri) {
100
            $restoreCommand = sprintf(
101
                '%smongorestore %s --uri %s',
102
                $this->commandBinaryPath,
103
                $archive,
104
                $this->uri
105
            );
106
        }
107
108 12
        if ($this->isCompress) {
109 2
            return "{$restoreCommand} < \"{$filePath}\"";
110
        }
111
112 10
        return "{$restoreCommand} \"{$filePath}\"";
113
    }
114
115 16
    public function preparePassword()
116
    {
117 16
        return !empty($this->password) ? "--password {$this->password}" : "";
118
    }
119
120 28
    public function prepareAuthenticateDatabase()
121
    {
122 28
        return !empty($this->authenticationDatabase) ? "--authenticationDatabase {$this->authenticationDatabase}" : "";
123
    }
124
125 16
    public function prepareCollection()
126
    {
127 16
        return !empty($this->collection) ? "--collection {$this->collection}" : "";
128
    }
129
}
130