Skip to content

Commit deb5711

Browse files
Merge pull request #234 from moebrowne
Additional Backup Event Context
2 parents 8113313 + cfff178 commit deb5711

File tree

6 files changed

+91
-35
lines changed

6 files changed

+91
-35
lines changed

src/Event/Backup/Abstraction.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace phpbu\App\Event\Backup;
33

4+
use phpbu\App\Backup\Source;
5+
use phpbu\App\Backup\Target;
46
use phpbu\App\Configuration\Backup;
57
use phpbu\App\Event\Action;
68

@@ -17,13 +19,36 @@
1719
*/
1820
abstract class Abstraction extends Action
1921
{
22+
private $target;
23+
private $source;
24+
2025
/**
2126
* Constructor.
2227
*
2328
* @param \phpbu\App\Configuration\Backup $backup
29+
* @param \phpbu\App\Backup\Target $target
30+
* @param \phpbu\App\Backup\Source $source
2431
*/
25-
public function __construct(Backup $backup)
32+
public function __construct(Backup $backup, Target $target, Source $source)
2633
{
2734
$this->configuration = $backup;
35+
$this->target = $target;
36+
$this->source = $source;
37+
}
38+
39+
/**
40+
* @return \phpbu\App\Backup\Target
41+
*/
42+
public function getTarget(): Target
43+
{
44+
return $this->target;
45+
}
46+
47+
/**
48+
* @return \phpbu\App\Backup\Source
49+
*/
50+
public function getSource(): Source
51+
{
52+
return $this->source;
2853
}
2954
}

src/Result.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace phpbu\App;
33

4+
use phpbu\App\Backup\Source;
5+
use phpbu\App\Backup\Target;
46
use phpbu\App\Event\Dispatcher;
57

68
/**
@@ -237,28 +239,32 @@ public function started() : float
237239
* Backup start event
238240
*
239241
* @param \phpbu\App\Configuration\Backup $backup
242+
* @param \phpbu\App\Backup\Target $target
243+
* @param \phpbu\App\Backup\Source $source
240244
* @throws \phpbu\App\Exception
241245
*/
242-
public function backupStart(Configuration\Backup $backup) : void
246+
public function backupStart(Configuration\Backup $backup, Target $target, Source $source) : void
243247
{
244248
$this->backupActive = new Result\Backup($backup->getName());
245249
$this->backups[] = $this->backupActive;
246250

247-
$event = new Event\Backup\Start($backup);
251+
$event = new Event\Backup\Start($backup, $target, $source);
248252
$this->eventDispatcher->dispatch(Event\Backup\Start::NAME, $event);
249253
}
250254

251255
/**
252256
* Backup failed event
253257
*
254258
* @param \phpbu\App\Configuration\Backup $backup
259+
* @param \phpbu\App\Backup\Target $target
260+
* @param \phpbu\App\Backup\Source $source
255261
*/
256-
public function backupFailed(Configuration\Backup $backup) : void
262+
public function backupFailed(Configuration\Backup $backup, Target $target, Source $source) : void
257263
{
258264
$this->backupsFailed++;
259265
$this->backupActive->fail();
260266

261-
$event = new Event\Backup\Failed($backup);
267+
$event = new Event\Backup\Failed($backup, $target, $source);
262268
$this->eventDispatcher->dispatch(Event\Backup\Failed::NAME, $event);
263269
}
264270

@@ -276,10 +282,12 @@ public function backupsFailedCount() : int
276282
* Backup end event
277283
*
278284
* @param \phpbu\App\Configuration\Backup $backup
285+
* @param \phpbu\App\Backup\Target $target
286+
* @param \phpbu\App\Backup\Source $source
279287
*/
280-
public function backupEnd(Configuration\Backup $backup) : void
288+
public function backupEnd(Configuration\Backup $backup, Target $target, Source $source) : void
281289
{
282-
$event = new Event\Backup\End($backup);
290+
$event = new Event\Backup\End($backup, $target, $source);
283291
$this->eventDispatcher->dispatch(Event\Backup\End::NAME, $event);
284292
}
285293

src/Runner/Backup.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace phpbu\App\Runner;
33

44
use phpbu\App\Backup\Compressor;
5+
use phpbu\App\Backup\Source;
56
use phpbu\App\Exception;
67
use phpbu\App\Backup\Cleaner;
78
use phpbu\App\Backup\Collector\Local;
@@ -58,6 +59,7 @@ public function run(Configuration $configuration) : Result
5859
}
5960
// setup target and collector, reset failure state
6061
$target = $this->factory->createTarget($backup->getTarget());
62+
$source = $this->factory->createSource($backup->getSource()->type, $backup->getSource()->options);
6163
$collector = new Local($target);
6264
$this->failure = false;
6365

@@ -67,7 +69,7 @@ public function run(Configuration $configuration) : Result
6769
* / _ / __ / /__/ ,< / /_/ / ___/
6870
* /____/_/ |_\___/_/|_|\____/_/
6971
*/
70-
$this->executeSource($backup, $target);
72+
$this->executeSource($backup, $target, $source);
7173

7274
/* _______ _____________ ______
7375
* / ___/ // / __/ ___/ //_/ __/
@@ -99,7 +101,7 @@ public function run(Configuration $configuration) : Result
99101
} catch (\Exception $e) {
100102
$this->result->debug('exception: ' . $e->getMessage());
101103
$this->result->addError($e);
102-
$this->result->backupFailed($backup);
104+
$this->result->backupFailed($backup, $target, $source);
103105
if ($backup->stopOnFailure()) {
104106
$stop = true;
105107
}
@@ -113,17 +115,17 @@ public function run(Configuration $configuration) : Result
113115
/**
114116
* Execute the backup.
115117
*
116-
* @param \phpbu\App\Configuration\Backup $conf
117-
* @param \phpbu\App\Backup\Target $target
118-
* @throws \Exception
118+
* @param \phpbu\App\Configuration\Backup $conf
119+
* @param \phpbu\App\Backup\Target $target
120+
* @param Source $source
121+
* @throws Exception
119122
*/
120-
protected function executeSource(Configuration\Backup $conf, Target $target)
123+
protected function executeSource(Configuration\Backup $conf, Target $target, Source $source)
121124
{
122-
$this->result->backupStart($conf);
123-
$source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
125+
$this->result->backupStart($conf, $target, $source);
124126
$status = $source->backup($target, $this->result);
125127
$this->compress($status, $target, $this->result);
126-
$this->result->backupEnd($conf);
128+
$this->result->backupEnd($conf, $target, $source);
127129
}
128130

129131
/**

src/Runner/Simulate.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ public function run(Configuration $configuration) : Result
7171
*/
7272
protected function simulateSource(Configuration\Backup $conf, Target $target)
7373
{
74-
$this->result->backupStart($conf);
7574
/* @var \phpbu\App\Backup\Source $runner */
7675
$source = $this->factory->createSource($conf->getSource()->type, $conf->getSource()->options);
7776

77+
$this->result->backupStart($conf, $target, $source);
78+
7879
if ($source instanceof Source\Simulator) {
7980
$status = $source->simulate($target, $this->result);
8081
$this->compress($status, $target, $this->result);
8182
}
82-
$this->result->backupEnd($conf);
83+
$this->result->backupEnd($conf, $target, $source);
8384
}
8485

8586
/**

tests/phpbu/Event/Backup/StartTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace phpbu\App\Event\Backup;
33

4+
use phpbu\App\Backup\Source\FakeSource;
5+
use phpbu\App\Backup\Target;
46
use phpbu\App\Configuration;
57

68
/**
@@ -22,7 +24,9 @@ class StartTest extends \PHPUnit\Framework\TestCase
2224
public function testGetConfiguration()
2325
{
2426
$config = new Configuration\Backup('dummy', false);
25-
$start = new Start($config);
27+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
28+
$source = new FakeSource();
29+
$start = new Start($config, $target, $source);
2630
$this->assertEquals($config, $start->getConfiguration());
2731
}
2832
}

tests/phpbu/ResultTest.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
namespace phpbu\App;
33

44
use phpbu\App\Backup\Check\Exception;
5+
use phpbu\App\Backup\Source\FakeSource;
6+
use phpbu\App\Backup\Target;
57

68
/**
79
* Version test
@@ -48,10 +50,12 @@ public function testBackupMinimal()
4850
{
4951
$conf = new Configuration('/tmp/foo.xml');
5052
$backup = new Configuration\Backup('test-backup', false);
53+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
54+
$source = new FakeSource();
5155
$result = new Result();
5256
$result->phpbuStart($conf);
53-
$result->backupStart($backup);
54-
$result->backupEnd($backup);
57+
$result->backupStart($backup, $target, $source);
58+
$result->backupEnd($backup, $target, $source);
5559
$result->phpbuEnd();
5660

5761
$this->assertTrue($result->wasSuccessful(), 'should be successful');
@@ -66,13 +70,15 @@ public function testBackupMaximalAllOk()
6670
{
6771
$conf = new Configuration('/tmp/foo.xml');
6872
$backup = new Configuration\Backup('test-backup', false);
73+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
74+
$source = new FakeSource();
6975
$check = new Configuration\Backup\Check('sizemin', '10M');
7076
$crypt = new Configuration\Backup\Crypt('mcrypt', false);
7177
$sync = new Configuration\Backup\Sync('rsync', false);
7278
$cleanup = new Configuration\Backup\Cleanup('capacity', false);
7379
$result = new Result();
7480
$result->phpbuStart($conf);
75-
$result->backupStart($backup);
81+
$result->backupStart($backup, $target, $source);
7682
$result->checkStart($check);
7783
$result->checkEnd($check);
7884
$result->cryptStart($crypt);
@@ -81,7 +87,7 @@ public function testBackupMaximalAllOk()
8187
$result->syncEnd($sync);
8288
$result->cleanupStart($cleanup);
8389
$result->cleanupEnd($cleanup);
84-
$result->backupEnd($backup);
90+
$result->backupEnd($backup, $target, $source);
8591
$result->phpbuEnd();
8692

8793
$this->assertTrue($result->wasSuccessful(), 'should be successful');
@@ -105,19 +111,21 @@ public function testBackupMaximalWithSkips()
105111
{
106112
$conf = new Configuration('/tmp/foo.xml');
107113
$backup = new Configuration\Backup('test-backup', false);
114+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
115+
$source = new FakeSource();
108116
$check = new Configuration\Backup\Check('sizemin', '10M');
109117
$crypt = new Configuration\Backup\Crypt('mcrypt', false);
110118
$sync = new Configuration\Backup\Sync('rsync', false);
111119
$cleanup = new Configuration\Backup\Cleanup('capacity', false);
112120
$result = new Result();
113121
$result->phpbuStart($conf);
114-
$result->backupStart($backup);
122+
$result->backupStart($backup, $target, $source);
115123
$result->checkStart($check);
116124
$result->checkEnd($check);
117125
$result->cryptSkipped($crypt);
118126
$result->syncSkipped($sync);
119127
$result->cleanupSkipped($cleanup);
120-
$result->backupEnd($backup);
128+
$result->backupEnd($backup, $target, $source);
121129
$result->phpbuEnd();
122130

123131
$this->assertTrue($result->wasSuccessful(), 'should be successful');
@@ -139,13 +147,15 @@ public function testBackupMaximalWithErrors()
139147
{
140148
$conf = new Configuration('/tmp/foo.xml');
141149
$backup = new Configuration\Backup('test-backup', false);
150+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
151+
$source = new FakeSource();
142152
$check = new Configuration\Backup\Check('sizemin', '10M');
143153
$crypt = new Configuration\Backup\Crypt('mcrypt', false);
144154
$sync = new Configuration\Backup\Sync('rsync', false);
145155
$cleanup = new Configuration\Backup\Cleanup('capacity', false);
146156
$result = new Result();
147157
$result->phpbuStart($conf);
148-
$result->backupStart($backup);
158+
$result->backupStart($backup, $target, $source);
149159
$result->checkStart($check);
150160
$result->checkEnd($check);
151161
$result->cryptStart($crypt);
@@ -157,7 +167,7 @@ public function testBackupMaximalWithErrors()
157167
$result->cleanupStart($cleanup);
158168
$result->addError(new Exception('failed'));
159169
$result->cleanupFailed($cleanup);
160-
$result->backupEnd($backup);
170+
$result->backupEnd($backup, $target, $source);
161171
$result->phpbuEnd();
162172

163173
$this->assertTrue($result->wasSuccessful(), 'should be successful');
@@ -180,13 +190,15 @@ public function testBackupMaximalWithErrors()
180190
public function testBackupFailed()
181191
{
182192
$conf = new Configuration('/tmp/foo.xml');
193+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
194+
$source = new FakeSource();
183195
$backup = new Configuration\Backup('test-backup', false);
184196
$result = new Result();
185197
$result->phpbuStart($conf);
186-
$result->backupStart($backup);
198+
$result->backupStart($backup, $target, $source);
187199
$result->addError(new Exception('failed'));
188-
$result->backupFailed($backup);
189-
$result->backupEnd($backup);
200+
$result->backupFailed($backup, $target, $source);
201+
$result->backupEnd($backup, $target, $source);
190202
$result->phpbuEnd();
191203

192204
$this->assertFalse($result->wasSuccessful(), 'should be successful');
@@ -204,15 +216,17 @@ public function testBackupFailedOnFailedCheck()
204216
{
205217
$conf = new Configuration('/tmp/foo.xml');
206218
$backup = new Configuration\Backup('test-backup', false);
219+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
220+
$source = new FakeSource();
207221
$check = new Configuration\Backup\Check('sizemin', '10M');
208222
$result = new Result();
209223
$result->phpbuStart($conf);
210-
$result->backupStart($backup);
211-
$result->backupEnd($backup);
224+
$result->backupStart($backup, $target, $source);
225+
$result->backupEnd($backup, $target, $source);
212226
$result->checkStart($check);
213227
$result->addError(new Exception('failed'));
214228
$result->checkFailed($check);
215-
$result->backupEnd($backup);
229+
$result->backupEnd($backup, $target, $source);
216230
$result->phpbuEnd();
217231

218232
$this->assertFalse($result->wasSuccessful(), 'should be successful');
@@ -231,11 +245,13 @@ public function testDebug()
231245
{
232246
$conf = new Configuration('/tmp/foo.xml');
233247
$backup = new Configuration\Backup('test-backup', false);
248+
$target = new Target(sys_get_temp_dir() . '/test', 'targetFile');
249+
$source = new FakeSource();
234250
$result = new Result();
235251
$result->phpbuStart($conf);
236252
$result->debug('debug');
237-
$result->backupStart($backup);
238-
$result->backupEnd($backup);
253+
$result->backupStart($backup, $target, $source);
254+
$result->backupEnd($backup, $target, $source);
239255
$result->phpbuEnd();
240256

241257
// no exception party

0 commit comments

Comments
 (0)