Completed
Push — master ( 4f1d49...8186c0 )
by Sebastian
07:15
created

Mongodump   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 192
Duplicated Lines 18.75 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 9
Bugs 0 Features 0
Metric Value
wmc 9
c 9
b 0
f 0
lcom 1
cbo 9
dl 36
loc 192
rs 10
ccs 41
cts 41
cp 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 0 9 1
A setupSourceData() 7 7 1
A setupCredentials() 0 7 1
A backup() 13 13 2
A getExecutable() 16 16 2
A createStatus() 0 4 1
A getDumpDir() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
namespace phpbu\App\Backup\Source;
3
4
use phpbu\App\Backup\Source;
5
use phpbu\App\Backup\Target;
6
use phpbu\App\Cli\Executable;
7
use phpbu\App\Exception;
8
use phpbu\App\Result;
9
use phpbu\App\Util;
10
11
/**
12
 * Mongodump source class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Sebastian Feldmann <[email protected]>
17
 * @copyright  Sebastian Feldmann <[email protected]>
18
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
19
 * @link       http://phpbu.de/
20
 * @since      Class available since Release 1.1.6
21
 */
22
class Mongodump extends SimulatorExecutable implements Simulator
23
{
24
    /**
25
     * Path to mongodump command.
26
     *
27
     * @var string
28
     */
29
    private $pathToMongodump;
30
31
    /**
32
     * Use IPv6
33
     * --ipv6
34
     *
35
     * @var boolean
36
     */
37
    private $useIPv6;
38
39
    /**
40
     * Host to connect to
41
     * --host <hostname:port>
42
     *
43
     * @var string
44
     */
45
    private $host;
46
47
    /**
48
     * User to connect with
49
     * --user <username>
50
     *
51
     * @var string
52
     */
53
    private $user;
54
55
    /**
56
     * Password to authenticate with
57
     * --password <password>
58
     *
59
     * @var string
60
     */
61
    private $password;
62
63
    /**
64
     * Database to use for authentication
65
     * --authenticationDatabase <dbname>
66
     *
67
     * @var string
68
     */
69
    private $authenticationDatabase;
70
71
    /**
72
     * List of databases to backup
73
     * --db <database>
74
     *
75
     * @var array
76
     */
77
    private $databases;
78
79
    /**
80
     * List of collections to backup
81
     * --collection <collection>
82
     *
83
     * @var array
84
     */
85
    private $collections;
86
87
    /**
88
     * List of collections to ignore
89
     * --excludeCollections array of strings
90
     *
91
     * @var array
92
     */
93
    private $excludeCollections;
94
95
    /**
96
     * List of prefixes to exclude collections
97
     * --excludeCollectionWithPrefix array of strings
98
     *
99
     * @var array
100
     */
101
    private $excludeCollectionsWithPrefix;
102
103
    /**
104
     * (No PHPDoc)
105
     *
106
     * @see    \phpbu\App\Backup\Source
107
     * @param  array $conf
108
     * @throws \phpbu\App\Exception
109
     */
110
    public function setup(array $conf = [])
111
    {
112
        $this->setupSourceData($conf);
113
        $this->setupCredentials($conf);
114
115
        // environment settings, config & validation
116
        $this->pathToMongodump = Util\Arr::getValue($conf, 'pathToMongodump');
117
        $this->useIPv6         = Util\Str::toBoolean(Util\Arr::getValue($conf, 'ipv6', ''), false);
118 3
    }
119
120 3
    /**
121 3
     * Fetch databases and collections to backup.
122
     *
123
     * @param array $conf
124 3
     */
125 3 View Code Duplication
    protected function setupSourceData(array $conf)
126 3
    {
127 3
        $this->databases                    = Util\Str::toList(Util\Arr::getValue($conf, 'databases'));
128
        $this->collections                  = Util\Str::toList(Util\Arr::getValue($conf, 'collections'));
129
        $this->excludeCollections           = Util\Str::toList(Util\Arr::getValue($conf, 'excludeCollections'));
130
        $this->excludeCollectionsWithPrefix = Util\Str::toList(Util\Arr::getValue($conf, 'excludeCollectionsWithPrefix'));
131
    }
132
133
    /**
134 3
     * Fetch credential settings.
135
     *
136 3
     * @param array $conf
137 3
     */
138 3
    protected function setupCredentials(array $conf)
139 3
    {
140 3
        $this->host                   = Util\Arr::getValue($conf, 'host');
141
        $this->user                   = Util\Arr::getValue($conf, 'user');
142
        $this->password               = Util\Arr::getValue($conf, 'password');
143
        $this->authenticationDatabase = Util\Arr::getValue($conf, 'authenticationDatabase');
144
    }
145
146
    /**
147 3
     * (non-PHPDoc)
148
     *
149 3
     * @see    \phpbu\App\Backup\Source
150 3
     * @param  \phpbu\App\Backup\Target $target
151 3
     * @param  \phpbu\App\Result        $result
152 3
     * @return \phpbu\App\Backup\Source\Status
153 3
     * @throws \phpbu\App\Exception
154
     */
155 View Code Duplication
    public function backup(Target $target, Result $result)
156
    {
157
        // setup dump location and execute the dump
158
        $mongodump = $this->execute($target);
159
160
        $result->debug($mongodump->getCmd());
161
162
        if (!$mongodump->wasSuccessful()) {
163
            throw new Exception('Mongodump failed');
164 2
        }
165
166
        return $this->createStatus($target);
167 2
    }
168
169 2
    /**
170
     * Create the Executable to run the Mongodump command.
171 2
     *
172 1
     * @param  \phpbu\App\Backup\Target $target
173
     * @return \phpbu\App\Cli\Executable
174
     */
175 1 View Code Duplication
    public function getExecutable(Target $target)
176
    {
177
        if (null == $this->executable) {
178
            $this->executable = new Executable\Mongodump($this->pathToMongodump);
179
            $this->executable->dumpToDirectory($this->getDumpDir($target))
180
                             ->useIpv6($this->useIPv6)
181
                             ->useHost($this->host)
182
                             ->credentials($this->user, $this->password, $this->authenticationDatabase)
183
                             ->dumpDatabases($this->databases)
184 3
                             ->dumpCollections($this->collections)
185
                             ->excludeCollections($this->excludeCollections)
186 3
                             ->excludeCollectionsWithPrefix($this->excludeCollectionsWithPrefix);
187 1
        }
188 1
189 1
        return $this->executable;
190 1
    }
191 1
192 1
    /**
193 1
     * Create backup status.
194 1
     *
195 1
     * @param  \phpbu\App\Backup\Target
196 1
     * @return \phpbu\App\Backup\Source\Status
197 1
     */
198
    protected function createStatus(Target $target)
199 3
    {
200
        return Status::create()->uncompressed($this->getDumpDir($target));
201
    }
202
203
    /**
204
     * Get the MongoDB dump directory.
205
     *
206
     * @param  \phpbu\App\Backup\Target $target
207
     * @return string
208 2
     */
209
    public function getDumpDir(Target $target)
210 2
    {
211
        return $target->getPath() . '/dump';
212
    }
213
}
214