Completed
Push — master ( c5e677...4df15a )
by Sebastian
02:56
created

Result::syncStart()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
namespace phpbu\App;
3
4
use Symfony\Component\EventDispatcher\EventDispatcher;
5
6
/**
7
 * Runner result.
8
 *
9
 * Heavily 'inspired' by Sebastian Bergmann's phpunit PHPUnit_Framework_TestResult.
10
 *
11
 * @package    phpbu
12
 * @subpackage App
13
 * @author     Sebastian Bergmann <[email protected]>
14
 * @author     Sebastian Feldmann <[email protected]>
15
 * @copyright  Sebastian Feldmann <[email protected]>
16
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
17
 * @link       http://phpbu.de/
18
 * @since      Class available since Release 1.0.0
19
 */
20
class Result
21
{
22
    /**
23
     * Symfony EventDispatcher.
24
     *
25
     * @var \Symfony\Component\EventDispatcher\EventDispatcher
26
     */
27
    protected $eventDispatcher;
28
29
    /**
30
     * List of executed Backups.
31
     *
32
     * @var array<\phpbu\App\Result\Backup>
33
     */
34
    protected $backups = [];
35
36
    /**
37
     * Currently running backup.
38
     *
39
     * @var \phpbu\App\Result\Backup
40
     */
41
    protected $backupActive;
42
43
    /**
44
     * List of errors.
45
     *
46
     * @var array
47
     */
48
    protected $errors = [];
49
50
    /**
51
     * Start timestamp
52
     *
53
     * @var float
54
     */
55
    protected $start;
56
57
    /**
58
     * @var integer
59
     */
60
    protected $backupsFailed = 0;
61
62
    /**
63
     * @var integer
64
     */
65
    protected $checksFailed = 0;
66
67
    /**
68
     * @var integer
69
     */
70
    protected $cryptsSkipped = 0;
71
72
    /**
73
     * @var integer
74
     */
75
    protected $cryptsFailed = 0;
76
77
    /**
78
     * @var integer
79
     */
80
    protected $syncsSkipped = 0;
81
82
    /**
83
     * @var integer
84
     */
85
    protected $syncsFailed = 0;
86
87
    /**
88
     * @var integer
89
     */
90
    protected $cleanupsSkipped = 0;
91
92
    /**
93
     * @var integer
94
     */
95
    protected $cleanupsFailed = 0;
96
97
    /**
98
     * Constructor
99
     */
100 22
    public function __construct()
101
    {
102 22
        $this->start           = microtime(true);
103 22
        $this->eventDispatcher = new EventDispatcher();
104 22
    }
105
106
    /**
107
     * Registers a Listener/Subscriber.
108
     *
109
     * @param \phpbu\App\Listener
110
     */
111 2
    public function addListener(Listener $subscriber)
112
    {
113 2
        $this->eventDispatcher->addSubscriber($subscriber);
114 2
    }
115
116
    /**
117
     * No errors at all?
118
     *
119
     * @return boolean
120
     */
121 7
    public function allOk()
122
    {
123 7
        return $this->wasSuccessful() && $this->noneSkipped() && $this->noneFailed();
124
    }
125
126
    /**
127
     * Backup without errors, but some tasks where skipped or failed.
128
     *
129
     * @return boolean
130
     */
131 5
    public function backupOkButSkipsOrFails()
132
    {
133 5
        return $this->wasSuccessful() && (!$this->noneSkipped() || !$this->noneFailed());
134
    }
135
136
    /**
137
     * Backup without errors?
138
     *
139
     * @return boolean
140
     */
141 7
    public function wasSuccessful()
142
    {
143 7
        return $this->backupsFailed === 0;
144
    }
145
146
    /**
147
     * Nothing skipped?
148
     *
149
     * @return boolean
150
     */
151 5
    public function noneSkipped()
152
    {
153 5
        return $this->cryptsSkipped + $this->syncsSkipped + $this->cleanupsSkipped === 0;
154
    }
155
156
    /**
157
     * Nothing failed?
158
     *
159
     * @return boolean
160
     */
161 4
    public function noneFailed()
162
    {
163 4
        return $this->checksFailed + $this->cryptsFailed + $this->syncsFailed + $this->cleanupsFailed === 0;
164
    }
165
166
    /**
167
     * Add Exception to error list
168
     *
169
     * @param \Exception $e
170
     */
171 8
    public function addError(\Exception $e)
172
    {
173 8
        $this->errors[] = $e;
174 8
    }
175
176
    /**
177
     * Return current error count.
178
     *
179
     * @return integer
180
     */
181 5
    public function errorCount()
182
    {
183 5
        return count($this->errors);
184
    }
185
186
    /**
187
     * Return list of errors.
188
     *
189
     * @return array<Exception>
190
     */
191 5
    public function getErrors()
192
    {
193 5
        return $this->errors;
194
    }
195
196
    /**
197
     * Return list of executed backups.
198
     *
199
     * @return array<\phpbu\App\Configuration\Backup>
200
     */
201 5
    public function getBackups()
202
    {
203 5
        return $this->backups;
204
    }
205
206
    /**
207
     * phpbu start event.
208
     *
209
     * @param \phpbu\App\Configuration $configuration
210
     */
211 20
    public function phpbuStart(Configuration $configuration)
212
    {
213 20
        $event = new Event\App\Start($configuration);
214 20
        $this->eventDispatcher->dispatch(Event\App\Start::NAME, $event);
215 20
    }
216
217
    /**
218
     * phpbu end event.
219
     */
220 20
    public function phpbuEnd()
221
    {
222 20
        $event = new Event\App\End($this);
223 20
        $this->eventDispatcher->dispatch(Event\App\End::NAME, $event);
224 20
    }
225
226
    /**
227
     * Return phpbu start micro time.
228
     *
229
     * @return float
230
     */
231 1
    public function started() : float
232
    {
233 1
        return $this->start;
234
    }
235
236
    /**
237
     * Backup start event.
238
     *
239
     * @param \phpbu\App\Configuration\Backup $backup
240
     */
241 20
    public function backupStart(Configuration\Backup $backup)
242
    {
243 20
        $this->backupActive = new Result\Backup($backup->getName());
244 20
        $this->backups[]    = $this->backupActive;
245
246 20
        $event = new Event\Backup\Start($backup);
247 20
        $this->eventDispatcher->dispatch(Event\Backup\Start::NAME, $event);
248 20
    }
249
250
    /**
251
     * Backup failed event.
252
     *
253
     * @param \phpbu\App\Configuration\Backup $backup
254
     */
255 2
    public function backupFailed(Configuration\Backup $backup)
256
    {
257 2
        $this->backupsFailed++;
258 2
        $this->backupActive->fail();
259
260 2
        $event = new Event\Backup\Failed($backup);
261 2
        $this->eventDispatcher->dispatch(Event\Backup\Failed::NAME, $event);
262 2
    }
263
264
    /**
265
     * Return amount of failed backups
266
     *
267
     * @return int
268
     */
269 3
    public function backupsFailedCount() : int
270
    {
271 3
        return $this->backupsFailed;
272
    }
273
274
    /**
275
     * Backup end event.
276
     *
277
     * @param \phpbu\App\Configuration\Backup $backup
278
     */
279 19
    public function backupEnd(Configuration\Backup $backup)
280
    {
281 19
        $event = new Event\Backup\End($backup);
282 19
        $this->eventDispatcher->dispatch(Event\Backup\End::NAME, $event);
283 19
    }
284
285
    /**
286
     * Check start event.
287
     *
288
     * @param \phpbu\App\Configuration\Backup\Check $check
289
     */
290 16
    public function checkStart(Configuration\Backup\Check $check)
291
    {
292 16
        $this->backupActive->checkAdd($check);
293
294 16
        $event = new Event\Check\Start($check);
295 16
        $this->eventDispatcher->dispatch(Event\Check\Start::NAME, $event);
296 16
    }
297
298
    /**
299
     * Check failed event.
300
     *
301
     * @param \phpbu\App\Configuration\Backup\Check $check
302
     */
303 3
    public function checkFailed(Configuration\Backup\Check $check)
304
    {
305
        // if this is the first check that fails
306 3
        if ($this->backupActive->wasSuccessful()) {
307 3
            $this->backupsFailed++;
308
        }
309
310 3
        $this->checksFailed++;
311 3
        $this->backupActive->fail();
312 3
        $this->backupActive->checkFailed($check);
313
314 3
        $event = new Event\Check\Failed($check);
315 3
        $this->eventDispatcher->dispatch(Event\Check\Failed::NAME, $event);
316 3
    }
317
318
    /**
319
     * Return amount of failed checks.
320
     *
321
     * @return int
322
     */
323 2
    public function checksFailedCount() : int
324
    {
325 2
        return $this->checksFailed;
326
    }
327
328
    /**
329
     * Check end event.
330
     *
331
     * @param \phpbu\App\Configuration\Backup\Check $check
332
     */
333 13
    public function checkEnd(Configuration\Backup\Check $check)
334
    {
335 13
        $event = new Event\Check\End($check);
336 13
        $this->eventDispatcher->dispatch(Event\Check\End::NAME, $event);
337 13
    }
338
339
    /**
340
     * Crypt start event.
341
     *
342
     * @param \phpbu\App\Configuration\Backup\Crypt $crypt
343
     */
344 14
    public function cryptStart(Configuration\Backup\Crypt $crypt)
345
    {
346 14
        $this->backupActive->cryptAdd($crypt);
347
348 14
        $event = new Event\Crypt\Start($crypt);
349 14
        $this->eventDispatcher->dispatch(Event\Crypt\Start::NAME, $event);
350 14
    }
351
352
    /**
353
     * Crypt skipped event.
354
     *
355
     * @param \phpbu\App\Configuration\Backup\Crypt $crypt
356
     */
357 3
    public function cryptSkipped(Configuration\Backup\Crypt $crypt)
358
    {
359 3
        $this->cryptsSkipped++;
360 3
        $this->backupActive->cryptSkipped($crypt);
361
362 3
        $event = new Event\Crypt\Skipped($crypt);
363 3
        $this->eventDispatcher->dispatch(Event\Crypt\Skipped::NAME, $event);
364 3
    }
365
366
    /**
367
     * Return amount of skipped crypts.
368
     *
369
     * @return int
370
     */
371 1
    public function cryptsSkippedCount() : int
372
    {
373 1
        return $this->cryptsSkipped;
374
    }
375
376
    /**
377
     * Crypt failed event.
378
     *
379
     * @param \phpbu\App\Configuration\Backup\Crypt $crypt
380
     */
381 2
    public function cryptFailed(Configuration\Backup\Crypt $crypt)
382
    {
383 2
        $this->cryptsFailed++;
384 2
        $this->backupActive->fail();
385 2
        $this->backupActive->cryptFailed($crypt);
386
387 2
        $event = new Event\Crypt\Failed($crypt);
388 2
        $this->eventDispatcher->dispatch(Event\Crypt\Failed::NAME, $event);
389 2
    }
390
391
    /**
392
     * Return amount of failed crypts.
393
     *
394
     * @return int
395
     */
396 1
    public function cryptsFailedCount() : int
397
    {
398 1
        return $this->cryptsFailed;
399
    }
400
401
    /**
402
     * Crypt end event.
403
     *
404
     * @param \phpbu\App\Configuration\Backup\Crypt $crypt
405
     */
406 10
    public function cryptEnd(Configuration\Backup\Crypt $crypt)
407
    {
408 10
        $event = new Event\Crypt\End($crypt);
409 10
        $this->eventDispatcher->dispatch(Event\Crypt\End::NAME, $event);
410 10
    }
411
412
    /**
413
     * Sync start event.
414
     *
415
     * @param \phpbu\App\Configuration\Backup\Sync $sync
416
     */
417 9
    public function syncStart(Configuration\Backup\Sync $sync)
418
    {
419 9
        $this->backupActive->syncAdd($sync);
420
421 9
        $event = new Event\Sync\Start($sync);
422 9
        $this->eventDispatcher->dispatch(Event\Sync\Start::NAME, $event);
423 9
    }
424
425
    /**
426
     * Sync skipped event.
427
     *
428
     * @param \phpbu\App\Configuration\Backup\Sync $sync
429
     */
430 4
    public function syncSkipped(Configuration\Backup\Sync $sync)
431
    {
432 4
        $this->syncsSkipped++;
433 4
        $this->backupActive->syncSkipped($sync);
434
435 4
        $event = new Event\Sync\Skipped($sync);
436 4
        $this->eventDispatcher->dispatch(Event\Sync\Skipped::NAME, $event);
437 4
    }
438
439
    /**
440
     * Return amount of skipped syncs.
441
     *
442
     * @return int
443
     */
444 3
    public function syncsSkippedCount() : int
445
    {
446 3
        return $this->syncsSkipped;
447
    }
448
449
    /**
450
     * Sync failed event.
451
     *
452
     * @param \phpbu\App\Configuration\Backup\Sync $sync
453
     */
454 2
    public function syncFailed(Configuration\Backup\Sync $sync)
455
    {
456 2
        $this->syncsFailed++;
457 2
        $this->backupActive->syncFailed($sync);
458
459 2
        $event = new Event\Sync\Failed($sync);
460 2
        $this->eventDispatcher->dispatch(Event\Sync\Failed::NAME, $event);
461 2
    }
462
463
    /**
464
     * Return amount of failed syncs.
465
     *
466
     * @return int
467
     */
468 3
    public function syncsFailedCount() : int
469
    {
470 3
        return $this->syncsFailed;
471
    }
472
473
    /**
474
     * Sync end event.
475
     *
476
     * @param \phpbu\App\Configuration\Backup\Sync $sync
477
     */
478 4
    public function syncEnd(Configuration\Backup\Sync $sync)
479
    {
480 4
        $event = new Event\Sync\End($sync);
481 4
        $this->eventDispatcher->dispatch(Event\Sync\End::NAME, $event);
482 4
    }
483
484
    /**
485
     * Cleanup start event.
486
     *
487
     * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup
488
     */
489 14
    public function cleanupStart(Configuration\Backup\Cleanup $cleanup)
490
    {
491 14
        $this->backupActive->cleanupAdd($cleanup);
492
493 14
        $event = new Event\Cleanup\Start($cleanup);
494 14
        $this->eventDispatcher->dispatch(Event\Cleanup\Start::NAME, $event);
495 14
    }
496
497
    /**
498
     * Cleanup skipped event.
499
     *
500
     * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup
501
     */
502 5
    public function cleanupSkipped(Configuration\Backup\Cleanup $cleanup)
503
    {
504 5
        $this->cleanupsSkipped++;
505 5
        $this->backupActive->cleanupSkipped($cleanup);
506
507 5
        $event = new Event\Cleanup\Skipped($cleanup);
508 5
        $this->eventDispatcher->dispatch(Event\Cleanup\Skipped::NAME, $event);
509 5
    }
510
511
    /**
512
     * Return amount of skipped cleanups.
513
     *
514
     * @return int
515
     */
516 3
    public function cleanupsSkippedCount() : int
517
    {
518 3
        return $this->cleanupsSkipped;
519
    }
520
521
    /**
522
     * Cleanup failed event.
523
     *
524
     * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup
525
     */
526 2
    public function cleanupFailed(Configuration\Backup\Cleanup $cleanup)
527
    {
528 2
        $this->cleanupsFailed++;
529 2
        $this->backupActive->cleanupFailed($cleanup);
530
531 2
        $event = new Event\Cleanup\Failed($cleanup);
532 2
        $this->eventDispatcher->dispatch(Event\Cleanup\Failed::NAME, $event);
533 2
    }
534
535
    /**
536
     * Return amount of failed cleanups.
537
     *
538
     * @return int
539
     */
540 3
    public function cleanupsFailedCount() : int
541
    {
542 3
        return $this->cleanupsFailed;
543
    }
544
545
    /**
546
     * Cleanup end event.
547
     *
548
     * @param \phpbu\App\Configuration\Backup\Cleanup $cleanup
549
     */
550 8
    public function cleanupEnd(Configuration\Backup\Cleanup $cleanup)
551
    {
552 8
        $event = new Event\Cleanup\End($cleanup);
553 8
        $this->eventDispatcher->dispatch(Event\Cleanup\End::NAME, $event);
554 8
    }
555
556
    /**
557
     * Debug.
558
     *
559
     * @param string $msg
560
     */
561 7
    public function debug($msg)
562
    {
563 7
        $event = new Event\Debug($msg);
564 7
        $this->eventDispatcher->dispatch(Event\Debug::NAME, $event);
565 7
    }
566
}
567