Completed
Push — master ( 38c453...48a46d )
by Sebastian
05:03 queued 11s
created

Result::warn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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