Completed
Push — master ( 46b8eb...4f6d0f )
by Sebastian
02:54
created

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