Passed
Push — master ( cbed52...d9a4d3 )
by CodexShaper
02:18
created

MongoDumper::setAuthenticationDatabase()   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
        $dumpCommand = sprintf(
52
            '%smongodump %s %s %s %s %s %s %s %s',
53
            $this->dumpCommandPath,
54
            $this->getArchiveCommand(),
0 ignored issues
show
Bug introduced by
The method getArchiveCommand() does not exist on CodexShaper\Dumper\Drivers\MongoDumper. Did you maybe mean getArchiveOption()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
            $this->/** @scrutinizer ignore-call */ 
55
                   getArchiveCommand(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
55
            $this->getDatabaseOption(),
56
            $this->getUsernameOption(),
57
            $this->getPasswordOption(),
58
            $this->getHostOption(),
59
            $this->getPortOption(),
60
            $this->getCollectionCommand(),
0 ignored issues
show
Bug introduced by
The method getCollectionCommand() does not exist on CodexShaper\Dumper\Drivers\MongoDumper. Did you maybe mean getCollectionOption()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
            $this->/** @scrutinizer ignore-call */ 
61
                   getCollectionCommand(),

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
61
            $this->getAuthenticateDatabase(),
62
        );
63
64
        if ($this->uri) {
65
            $dumpCommand = sprintf(
66
                '%smongodump %s --uri %s %s',
67
                $this->dumpCommandPath,
68
                $this->getArchiveOption(),
69
                $this->uri,
70
                $this->getCollectionOption()
71
            );
72
        }
73
74
        if ($this->isCompress) {
75
            return "{$dumpCommand} > {$destinationPath}{$this->compressExtension}";
76
        }
77
78
        return "{$dumpCommand} --out {$destinationPath}";
79
    }
80
81
    public function getDatabaseOption()
82
    {
83
        return !empty($this->dbName) ? "--db " . escapeshellarg($this->dbName) : "";
84
    }
85
86
    public function getUsernameOption()
87
    {
88
        return !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
89
    }
90
91
    public function getPasswordOption()
92
    {
93
        return !empty($this->password) ? "--password " . escapeshellarg($this->password) : "";
94
    }
95
96
    public function getHostOption()
97
    {
98
        return !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
99
    }
100
101
    public function getPortOption()
102
    {
103
        return !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
104
    }
105
106
    public function getAuthenticateDatabase()
107
    {
108
        return !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
109
    }
110
111
    public function getCollectionOption()
112
    {
113
        return !empty($this->collection) ? "--collection " . escapeshellarg($this->collection) : "";
114
    }
115
116
    public function getArchiveOption()
117
    {
118
        return $this->isCompress ? "--archive --gzip" : "";
119
    }
120
121
    protected function prepareRestoreCommand(string $filePath): string
122
    {
123
        $username               = !empty($this->username) ? "--username " . escapeshellarg($this->username) : "";
124
        $host                   = !empty($this->host) ? "--host " . escapeshellarg($this->host) : "";
125
        $port                   = !empty($this->port) ? "--port " . escapeshellarg($this->port) : "";
126
        $authenticationDatabase = !empty($this->authenticationDatabase) ? "--authenticationDatabase " . escapeshellarg($this->authenticationDatabase) : "";
127
128
        $archive = "";
129
        if ($this->isCompress) {
130
            $archive = "--gzip --archive";
131
        }
132
133
        $restoreCommand = sprintf("%smongorestore %s %s %s %s %s",
134
            $this->dumpCommandPath,
135
            $archive,
136
            $host,
137
            $port,
138
            $username,
139
            $authenticationDatabase
140
        );
141
142
        if ($this->uri) {
143
            $restoreCommand = sprintf(
144
                '%smongorestore %s --uri %s',
145
                $this->dumpCommandPath,
146
                $archive,
147
                $this->uri
148
            );
149
        }
150
151
        if ($this->isCompress) {
152
153
            return "{$restoreCommand} < {$filePath}";
154
        }
155
156
        return "{$restoreCommand} {$filePath}";
157
    }
158
}
159