1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Backup package, an RunOpenCode project. |
4
|
|
|
* |
5
|
|
|
* (c) 2015 RunOpenCode |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* This project is fork of "kbond/php-backup", for full credits info, please |
11
|
|
|
* view CREDITS file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
namespace RunOpenCode\Backup\Destination; |
14
|
|
|
|
15
|
|
|
use RunOpenCode\Backup\Contract\BackupInterface; |
16
|
|
|
use RunOpenCode\Backup\Contract\DestinationInterface; |
17
|
|
|
use RunOpenCode\Backup\Log\LoggerAwareTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ReplicatedDestination |
21
|
|
|
* |
22
|
|
|
* Replicated destination enables to you to backup on one master and one slave destination where failed backup attempt |
23
|
|
|
* to slave destination will not fail backup process. |
24
|
|
|
* |
25
|
|
|
* @package RunOpenCode\Backup\Destination |
26
|
|
|
*/ |
27
|
|
|
final class ReplicatedDestination implements DestinationInterface |
28
|
|
|
{ |
29
|
|
|
use LoggerAwareTrait; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var DestinationInterface |
33
|
|
|
*/ |
34
|
|
|
private $master; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var DestinationInterface |
38
|
|
|
*/ |
39
|
|
|
private $slave; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var bool |
43
|
|
|
*/ |
44
|
|
|
private $atomic; |
45
|
|
|
|
46
|
6 |
|
public function __construct(DestinationInterface $master, DestinationInterface $slave, $atomic = false) |
47
|
|
|
{ |
48
|
6 |
|
$this->master = $master; |
49
|
6 |
|
$this->slave = $slave; |
50
|
6 |
|
$this->atomic = $atomic; |
51
|
6 |
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
6 |
|
public function push(BackupInterface $backup) |
57
|
|
|
{ |
58
|
6 |
|
$this->master->push($backup); |
59
|
|
|
|
60
|
|
|
try { |
61
|
6 |
|
$this->slave->push($backup); |
62
|
6 |
|
} catch (\Exception $e) { |
63
|
|
|
|
64
|
4 |
|
if ($this->atomic) { |
65
|
4 |
|
throw $e; |
66
|
|
|
} else { |
67
|
2 |
|
$this->getLogger()->error('Unable to backup to slave destination.', array( |
68
|
2 |
|
'message' => $e->getMessage(), |
69
|
2 |
|
'code' => $e->getCode(), |
70
|
2 |
|
'file' => $e->getFile(), |
71
|
2 |
|
'line' => $e->getLine(), |
72
|
2 |
|
'trace' => $e->getTrace() |
73
|
2 |
|
)); |
74
|
|
|
} |
75
|
|
|
} |
76
|
4 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function get($name) |
82
|
|
|
{ |
83
|
|
|
return $this->master->get($name); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritdoc} |
88
|
|
|
*/ |
89
|
|
|
public function has($name) |
90
|
|
|
{ |
91
|
|
|
return $this->master->has($name); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
|
|
public function delete($name) |
|
|
|
|
98
|
|
|
{ |
99
|
|
|
$this->master->delete($name); |
100
|
|
|
|
101
|
|
|
try { |
102
|
|
|
$this->slave->delete($name); |
103
|
|
|
} catch (\Exception $e) { |
104
|
|
|
|
105
|
|
|
if ($this->atomic) { |
106
|
|
|
throw $e; |
107
|
|
|
} elseif ($this->getLogger()) { |
108
|
|
|
$this->getLogger()->error(sprintf('Unable to delete backup "%s" from slave destination.', $name), array( |
109
|
|
|
'message' => $e->getMessage(), |
110
|
|
|
'code' => $e->getCode(), |
111
|
|
|
'file' => $e->getFile(), |
112
|
|
|
'line' => $e->getLine(), |
113
|
|
|
'trace' => $e->getTrace() |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* {@inheritdoc} |
121
|
|
|
*/ |
122
|
|
|
public function all() |
123
|
|
|
{ |
124
|
|
|
return $this->master->all(); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* {@inheritdoc} |
129
|
|
|
*/ |
130
|
|
|
public function getIterator() |
131
|
|
|
{ |
132
|
|
|
return $this->master->getIterator(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* {@inheritdoc} |
137
|
|
|
*/ |
138
|
|
|
public function count() |
139
|
|
|
{ |
140
|
|
|
return $this->master->count(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
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.