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

Elasticdump::getExecutable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 9

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 12
loc 12
rs 9.4285
ccs 2
cts 2
cp 1
cc 2
eloc 9
nc 2
nop 1
crap 2
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