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

Elasticdump   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 128
Duplicated Lines 27.34 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 1
Metric Value
wmc 7
c 5
b 0
f 1
lcom 1
cbo 8
dl 35
loc 128
rs 10
ccs 29
cts 29
cp 1

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setup() 11 11 1
A setupSourceData() 0 5 1
A backup() 12 12 2
A getExecutable() 12 12 2
A createStatus() 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
 * Elasticdump 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.1
22
 */
23
class Elasticdump extends SimulatorExecutable implements Simulator
24
{
25
    /**
26
     * Path to elasticdump binary.
27
     *
28
     * @var string
29
     */
30
    private $pathToElasticdump;
31
32
    /**
33
     * Host to connect to
34
     *
35
     * @var string
36
     */
37
    private $host;
38
39
    /**
40
     * User to connect with
41
     *
42
     * @var string
43
     */
44
    private $user;
45
46
    /**
47
     * Password to authenticate with
48
     *
49
     * @var string
50
     */
51
    private $password;
52
53
    /**
54
     * Specific index to backup
55
     *
56
     * @var string
57
     */
58
    private $index;
59
60
    /**
61
     * Whether to backup the mapping or data
62
     * --type
63
     *
64
     * @var string
65
     */
66
    private $type;
67
68
    /**
69
     * Setup.
70
     *
71
     * @see    \phpbu\App\Backup\Source
72
     * @param  array $conf
73
     * @throws \phpbu\App\Exception
74
     */
75 View Code Duplication
    public function setup(array $conf = array())
76
    {
77
        $this->setupSourceData($conf);
78
79
        // environment settings
80
        $this->pathToElasticdump = Util\Arr::getValue($conf, 'pathToElasticdump');
81
82
        $this->host       = Util\Arr::getValue($conf, 'host', 'http://localhost:9200');
83 5
        $this->user       = Util\Arr::getValue($conf, 'user');
84
        $this->password   = Util\Arr::getValue($conf, 'password');
85 5
    }
86
87
    /**
88 5
     * Get index and type.
89
     *
90 5
     * @param array $conf
91 5
     */
92 5
    protected function setupSourceData(array $conf)
93 5
    {
94 5
        $this->index = Util\Arr::getValue($conf, 'index');
95
        $this->type  = Util\Arr::getValue($conf, 'type');
96
    }
97
98
    /**
99
     * (non-PHPDoc)
100
     *
101 5
     * @see    \phpbu\App\Backup\Source
102
     * @param  \phpbu\App\Backup\Target $target
103 5
     * @param  \phpbu\App\Result        $result
104 5
     * @return \phpbu\App\Backup\Source\Status
105 5
     * @throws \phpbu\App\Exception
106
     */
107 View Code Duplication
    public function backup(Target $target, Result $result)
108
    {
109
        $elasticdump = $this->execute($target);
110
111
        $result->debug($elasticdump->getCmd());
112
113
        if (!$elasticdump->wasSuccessful()) {
114
            throw new Exception('elasticdump failed: ' . $elasticdump->getStdErr());
115
        }
116 2
117
        return $this->createStatus($target);
118 2
    }
119
120 2
    /**
121
     * Create the Executable to run the elasticdump command.
122 2
     *
123 1
     * @param  \phpbu\App\Backup\Target $target
124
     * @return \phpbu\App\Cli\Executable
125
     * @throws \phpbu\App\Exception
126 1
     */
127 View Code Duplication
    public function getExecutable(Target $target)
128
    {
129
        if (null == $this->executable) {
130
            $this->executable = new Executable\Elasticdump($this->pathToElasticdump);
131
            $this->executable->useHost($this->host)
132
                             ->credentials($this->user, $this->password)
133
                             ->dumpIndex($this->index)
134
                             ->dumpType($this->type)
135
                             ->dumpTo($target->getPathnamePlain());
136 5
        }
137
        return $this->executable;
138 5
    }
139 3
140 3
    /**
141 3
     * Create backup status.
142 3
     *
143 3
     * @param  \phpbu\App\Backup\Target $target
144 3
     * @return \phpbu\App\Backup\Source\Status
145 3
     */
146 3
    protected function createStatus(Target $target)
147 5
    {
148
        return Status::create()->uncompressed($target->getPathnamePlain());
149
    }
150
}
151