Completed
Push — master ( 6a1fad...afb84c )
by Sebastian
05:39
created

Arangodump::createExecutable()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 13
loc 13
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
nc 1
nop 1
crap 1
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
 * Arangodump source class.
13
 *
14
 * @package    phpbu
15
 * @subpackage Backup
16
 * @author     Francis Chuang <[email protected]>
17
 * @author     Sebastian Feldmann <[email protected]>
18
 * @copyright  Sebastian Feldmann <[email protected]>
19
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
20
 * @link       http://phpbu.de/
21
 * @since      Class available since Release 2.0.0
22
 */
23
class Arangodump extends SimulatorExecutable implements Simulator
24
{
25
    /**
26
     * Path to arangodump command.
27
     *
28
     * @var string
29
     */
30
    private $pathToArangodump;
31
32
    /**
33
     * Endpoint to connect to
34
     * --server.endpoint <endpoint>
35
     *
36
     * @var string
37
     */
38
    private $endpoint;
39
40
    /**
41
     * Username to connect with
42
     * --server.username <username>
43
     *
44
     * @var string
45
     */
46
    private $username;
47
48
    /**
49
     * Password to authenticate with
50
     * --server.password <password>
51
     *
52
     * @var string
53
     */
54
    private $password;
55
56
    /**
57
     * The database to backup
58
     * --server.database <database>
59
     *
60
     * @var string
61
     */
62
    private $database;
63
64
    /**
65
     * Whether the data should be dumped or not
66
     * --dump-data
67
     *
68
     * @var boolean
69
     */
70
    private $dumpData;
71
72
    /**
73
     * Include system collections
74
     * --include-system-collections
75
     *
76
     * @var bool
77
     */
78
    private $includeSystemCollections;
79
80
    /**
81
     * Restrict the dump to these collections
82
     * --collection
83
     *
84
     * @var array
85
     */
86
    private $collections;
87
88
    /**
89
     * Do not ask for the username and password when connecting to the server.
90
     * This does not control whether the server requires authentication.
91
     * -- disable-authentication
92
     *
93
     * @var bool
94
     */
95
    private $disableAuthentication;
96
97
    /**
98
     * Setup.
99
     *
100
     * @see    \phpbu\App\Backup\Source
101
     * @param  array $conf
102
     * @throws \phpbu\App\Exception
103
     */
104
    public function setup(array $conf = [])
105
    {
106
        $this->setupSourceData($conf);
107
108
        $this->pathToArangodump      = Util\Arr::getValue($conf, 'pathToArangodump', '');
109
        $this->endpoint              = Util\Arr::getValue($conf, 'endpoint', '');
110
        $this->username              = Util\Arr::getValue($conf, 'username', '');
111
        $this->password              = Util\Arr::getValue($conf, 'password', '');
112 5
        $this->database              = Util\Arr::getValue($conf, 'database', '');
113
        $this->disableAuthentication = Util\Str::toBoolean(Util\Arr::getValue($conf, 'disableAuthentication'), false);
114 5
    }
115
116 5
    /**
117 5
     * Get collections and data to backup.
118 5
     *
119 5
     * @param array $conf
120 5
     */
121 5
    protected function setupSourceData(array $conf)
122 5
    {
123 5
        $this->dumpData                  = Util\Str::toBoolean(Util\Arr::getValue($conf, 'dumpData'), false);
124
        $this->collections               = Util\Str::toList(Util\Arr::getValue($conf, 'collections'));
125
        $this->includeSystemCollections  = Util\Str::toBoolean(
126
            Util\Arr::getValue($conf, 'includeSystemCollections'),
127
            false
128
        );
129
    }
130 5
131
    /**
132 5
     * Execute the backup.
133 5
     *
134 5
     * @see    \phpbu\App\Backup\Source
135 5
     * @param  \phpbu\App\Backup\Target $target
136
     * @param  \phpbu\App\Result        $result
137
     * @return \phpbu\App\Backup\Source\Status
138
     * @throws \phpbu\App\Exception
139
     */
140
    public function backup(Target $target, Result $result) : Status
141
    {
142
        $arangodump = $this->execute($target);
143
144
        $result->debug($arangodump->getCmdPrintable());
145
146 2
        if (!$arangodump->isSuccessful()) {
147
            throw new Exception('arangodump failed: ' . $arangodump->getStdErr());
148 2
        }
149
150 2
        return $this->createStatus($target);
151
    }
152 2
153 1
    /**
154
     * Create the Executable to run the arangodump command.
155
     *
156 1
     * @param  \phpbu\App\Backup\Target $target
157
     * @return \phpbu\App\Cli\Executable
158
     * @throws \phpbu\App\Exception
159
     */
160 View Code Duplication
    protected function createExecutable(Target $target) : Executable
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
161
    {
162
        $executable = new Executable\Arangodump($this->pathToArangodump);
163
        $executable->useEndpoint($this->endpoint)
164
                   ->credentials($this->username, $this->password)
165
                   ->dumpDatabase($this->database)
166 5
                   ->dumpCollections($this->collections)
167
                   ->disableAuthentication($this->disableAuthentication)
168 5
                   ->includeSystemCollections($this->includeSystemCollections)
169 3
                   ->dumpData($this->dumpData)
170 3
                   ->dumpTo($this->getDumpDir($target));
171 3
        return $executable;
172 3
    }
173 3
174 3
    /**
175 3
     * Create backup status.
176 3
     *
177 3
     * @param  \phpbu\App\Backup\Target
178 3
     * @return \phpbu\App\Backup\Source\Status
179 3
     */
180
    protected function createStatus(Target $target) : Status
181 5
    {
182
        return Status::create()->uncompressedDirectory($this->getDumpDir($target));
183
    }
184
185
    /**
186
     * Get the ArangoDB dump directory.
187
     *
188
     * @param  \phpbu\App\Backup\Target $target
189
     * @return string
190 4
     */
191
    public function getDumpDir(Target $target) : string
192 4
    {
193
        return $target->getPath() . '/dump';
194
    }
195
}
196