Completed
Push — master ( 1c55bf...58778d )
by Sebastian
04:16
created

PrinterCli   F

Complexity

Total Complexity 62

Size/Duplication

Total Lines 589
Duplicated Lines 0 %

Test Coverage

Coverage 99.55%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 196
dl 0
loc 589
ccs 221
cts 222
cp 0.9955
rs 3.44
c 3
b 0
f 0
wmc 62

31 Methods

Rating   Name   Duplication   Size   Complexity  
A printFooter() 0 54 4
A onBackupEnd() 0 4 2
A write() 0 6 2
A onCleanupFailed() 0 4 2
A writeWithColor() 0 6 2
A onCryptFailed() 0 4 2
A onCleanupSkipped() 0 4 2
A __construct() 0 7 2
A printBackupVerbose() 0 40 3
A onBackupFailed() 0 4 2
A onCleanupEnd() 0 4 2
A onCryptSkipped() 0 4 2
A onCryptEnd() 0 4 2
A printHeader() 0 3 1
A onBackupStart() 0 6 2
A onCheckEnd() 0 4 2
A onCheckFailed() 0 4 2
A printResult() 0 11 3
A onSyncStart() 0 6 2
A onPhpbuEnd() 0 4 1
A onSyncEnd() 0 4 2
A onCryptStart() 0 6 2
A printErrors() 0 11 2
A onPhpbuStart() 0 8 2
A onCleanupStart() 0 6 2
A writeWithAsterisk() 0 3 1
A onCheckStart() 0 7 2
A onDebug() 0 4 2
A onSyncFailed() 0 4 2
A getSubscribedEvents() 0 24 1
A onSyncSkipped() 0 4 2

How to fix   Complexity   

Complex Class

Complex classes like PrinterCli often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PrinterCli, and based on these observations, apply Extract Interface, too.

1
<?php
2
namespace phpbu\App\Result;
3
4
use phpbu\App\Cli\Statistics;
5
use phpbu\App\Event;
6
use phpbu\App\Listener;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
use SebastianBergmann\Environment\Console;
10
use SebastianBergmann\Environment\Runtime;
11
12
/**
13
 * Default app output.
14
 *
15
 * Heavily 'inspired' by Sebastian Bergmann's phpunit PHPUnit_TextUI_ResultPrinter.
16
 *
17
 * @package    phpbu
18
 * @subpackage Result
19
 * @author     Sebastian Feldmann <[email protected]>
20
 * @copyright  Sebastian Feldmann <[email protected]>
21
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
22
 * @link       http://phpbu.de/
23
 * @since      Class available since Release 1.0.0
24
 */
25
class PrinterCli implements Listener
26
{
27
    /**
28
     * Verbose output
29
     *
30
     * @var bool
31
     */
32
    protected $verbose = false;
33
34
    /**
35
     * Output with colors
36
     *
37
     * @var bool
38
     */
39
    protected $colors = false;
40
41
    /**
42
     * Is debug active
43
     *
44
     * @var bool
45
     */
46
    protected $debug = false;
47
48
    /**
49
     * Amount of executed backups
50
     *
51
     * @var integer
52
     */
53
    private $numBackups = 0;
54
55
    /**
56
     * Amount of executed checks
57
     *
58
     * @var integer
59
     */
60
    private $numChecks = 0;
61
62
    /**
63
     * Amount of executed crypts
64
     *
65
     * @var integer
66
     */
67
    private $numCrypts = 0;
68
69
    /**
70
     * Amount of executed Syncs
71
     *
72
     * @var integer
73
     */
74
    private $numSyncs = 0;
75
76
    /**
77
     * Amount of executed Cleanups
78
     *
79
     * @var integer
80
     */
81
    private $numCleanups = 0;
82
83
    /**
84
     * Console
85
     *
86
     * @var \SebastianBergmann\Environment\Console
87
     */
88
    private $console;
89
90
    /**
91
     * PHP Runtime
92
     *
93
     * @var \SebastianBergmann\Environment\Runtime
94
     */
95
    private $runtime;
96
97
    /**
98
     * Returns an array of event names this subscriber wants to listen to.
99
     *
100
     * The array keys are event names and the value can be:
101
     *
102
     *  * The method name to call (priority defaults to 0)
103
     *  * An array composed of the method name to call and the priority
104
     *  * An array of arrays composed of the method names to call and respective
105
     *    priorities, or 0 if unset
106
     *
107
     * @return array The event names to listen to
108
     */
109 3
    public static function getSubscribedEvents(): array
110
    {
111
        return [
112 3
            'phpbu.debug'           => 'onDebug',
113
            'phpbu.app_start'       => 'onPhpbuStart',
114
            'phpbu.backup_start'    => 'onBackupStart',
115
            'phpbu.backup_failed'   => 'onBackupFailed',
116
            'phpbu.backup_end'      => 'onBackupEnd',
117
            'phpbu.check_start'     => 'onCheckStart',
118
            'phpbu.check_failed'    => 'onCheckFailed',
119
            'phpbu.check_end'       => 'onCheckEnd',
120
            'phpbu.crypt_start'     => 'onCryptStart',
121
            'phpbu.crypt_skipped'   => 'onCryptSkipped',
122
            'phpbu.crypt_failed'    => 'onCryptFailed',
123
            'phpbu.crypt_end'       => 'onCryptEnd',
124
            'phpbu.sync_start'      => 'onSyncStart',
125
            'phpbu.sync_skipped'    => 'onSyncSkipped',
126
            'phpbu.sync_failed'     => 'onSyncFailed',
127
            'phpbu.sync_end'        => 'onSyncEnd',
128
            'phpbu.cleanup_start'   => 'onCleanupStart',
129
            'phpbu.cleanup_skipped' => 'onCleanupSkipped',
130
            'phpbu.cleanup_failed'  => 'onCleanupFailed',
131
            'phpbu.cleanup_end'     => 'onCleanupEnd',
132
            'phpbu.app_end'         => 'onPhpbuEnd',
133
        ];
134
    }
135
136
    /**
137
     * Constructor
138
     *
139
     * @param  bool $verbose
140
     * @param  bool $colors
141
     * @param  bool $debug
142
     * @throws \InvalidArgumentException
143
     */
144 29
    public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false)
145
    {
146 29
        $this->console = new Console;
147 29
        $this->runtime = new Runtime;
148 29
        $this->debug   = $debug;
149 29
        $this->verbose = $verbose;
150 29
        $this->colors  = $colors && $this->console->hasColorSupport();
151 29
    }
152
153
    /**
154
     * phpbu start event.
155
     *
156
     * @param \phpbu\App\Event\App\Start $event
157
     */
158 2
    public function onPhpbuStart(Event\App\Start $event)
159
    {
160 2
        $configuration = $event->getConfiguration();
161 2
        if ($this->verbose) {
162 1
            $this->write(
163 1
                'Runtime:       ' . $this->runtime->getNameWithVersion() . PHP_EOL .
164 1
                'Configuration: ' . $configuration->getFilename() . PHP_EOL .
165 1
                PHP_EOL
166
            );
167
        }
168 2
    }
169
170
    /**
171
     * Backup start event.
172
     *
173
     * @param \phpbu\App\Event\Backup\Start $event
174
     */
175 3
    public function onBackupStart(Event\Backup\Start $event)
176
    {
177 3
        $this->numBackups++;
178 3
        if ($this->debug) {
179 2
            $backup = $event->getConfiguration();
180 2
            $this->writeWithAsterisk('backup: [' . $backup->getSource()->type . '] ');
181
        }
182 3
    }
183
184
    /**
185
     * Backup failed event.
186
     *
187
     * @param \phpbu\App\Event\Backup\Failed $event
188
     */
189 1
    public function onBackupFailed(Event\Backup\Failed $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

189
    public function onBackupFailed(/** @scrutinizer ignore-unused */ Event\Backup\Failed $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
190
    {
191 1
        if ($this->debug) {
192 1
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
193
        }
194 1
    }
195
196
    /**
197
     * Backup end event.
198
     *
199
     * @param \phpbu\App\Event\Backup\End $event
200
     */
201 2
    public function onBackupEnd(Event\Backup\End $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

201
    public function onBackupEnd(/** @scrutinizer ignore-unused */ Event\Backup\End $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
202
    {
203 2
        if ($this->debug) {
204 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
205
        }
206 2
    }
207
208
    /**
209
     * Check start event.
210
     *
211
     * @param \phpbu\App\Event\Check\Start $event
212
     */
213 3
    public function onCheckStart(Event\Check\Start $event)
214
    {
215 3
        $this->numChecks++;
216 3
        if ($this->debug) {
217 2
            $check = $event->getConfiguration();
218 2
            $this->writeWithAsterisk('check: [' . $check->type . '] ');
219 2
            $this->write('checking: [' . $check->value . '] ');
220
        }
221 3
    }
222
223
    /**
224
     * Check failed event.
225
     *
226
     * @param \phpbu\App\Event\Check\Failed $event
227
     */
228 1
    public function onCheckFailed(Event\Check\Failed $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

228
    public function onCheckFailed(/** @scrutinizer ignore-unused */ Event\Check\Failed $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
229
    {
230 1
        if ($this->debug) {
231 1
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
232
        }
233 1
    }
234
235
    /**
236
     * Check end event.
237
     *
238
     * @param \phpbu\App\Event\Check\End $event
239
     */
240 2
    public function onCheckEnd(Event\Check\End $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

240
    public function onCheckEnd(/** @scrutinizer ignore-unused */ Event\Check\End $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
241
    {
242 2
        if ($this->debug) {
243 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
244
        }
245 2
    }
246
247
    /**
248
     * Crypt start event.
249
     *
250
     * @param \phpbu\App\Event\Crypt\Start $event
251
     */
252 4
    public function onCryptStart(Event\Crypt\Start $event)
253
    {
254 4
        $this->numCrypts++;
255 4
        if ($this->debug) {
256 3
            $crypt = $event->getConfiguration();
257 3
            $this->writeWithAsterisk('crypt: [' . $crypt->type . '] ');
258
        }
259 4
    }
260
261
    /**
262
     * Crypt skipped event.
263
     *
264
     * @param \phpbu\App\Event\Crypt\Skipped $event
265
     */
266 1
    public function onCryptSkipped(Event\Crypt\Skipped $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

266
    public function onCryptSkipped(/** @scrutinizer ignore-unused */ Event\Crypt\Skipped $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
267
    {
268 1
        if ($this->debug) {
269 1
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
270
        }
271 1
    }
272
273
    /**
274
     * Crypt failed event.
275
     *
276
     * @param \phpbu\App\Event\Crypt\Failed $event
277
     */
278 1
    public function onCryptFailed(Event\Crypt\Failed $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

278
    public function onCryptFailed(/** @scrutinizer ignore-unused */ Event\Crypt\Failed $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
279
    {
280 1
        if ($this->debug) {
281 1
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
282
        }
283 1
    }
284
285
    /**
286
     * Crypt end event.
287
     *
288
     * @param \phpbu\App\Event\Crypt\End $event
289
     */
290 2
    public function onCryptEnd(Event\Crypt\End $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

290
    public function onCryptEnd(/** @scrutinizer ignore-unused */ Event\Crypt\End $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
291
    {
292 2
        if ($this->debug) {
293 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
294
        }
295 2
    }
296
297
    /**
298
     * Sync start event.
299
     *
300
     * @param \phpbu\App\Event\Sync\Start $event
301
     */
302 4
    public function onSyncStart(Event\Sync\Start $event)
303
    {
304 4
        $this->numSyncs++;
305 4
        if ($this->debug) {
306 3
            $sync = $event->getConfiguration();
307 3
            $this->writeWithAsterisk('sync: [' . $sync->type . '] ');
308
        }
309 4
    }
310
311
    /**
312
     * Sync skipped event.
313
     *
314
     * @param \phpbu\App\Event\Sync\Skipped $event
315
     */
316 1
    public function onSyncSkipped(Event\Sync\Skipped $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

316
    public function onSyncSkipped(/** @scrutinizer ignore-unused */ Event\Sync\Skipped $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
317
    {
318 1
        if ($this->debug) {
319 1
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
320
        }
321 1
    }
322
323
    /**
324
     * Sync failed event.
325
     *
326
     * @param \phpbu\App\Event\Sync\Failed $event
327
     */
328 1
    public function onSyncFailed(Event\Sync\Failed $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

328
    public function onSyncFailed(/** @scrutinizer ignore-unused */ Event\Sync\Failed $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
329
    {
330 1
        if ($this->debug) {
331 1
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
332
        }
333 1
    }
334
335
    /**
336
     * Sync end event.
337
     *
338
     * @param \phpbu\App\Event\Sync\End $event
339
     */
340 2
    public function onSyncEnd(Event\Sync\End $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

340
    public function onSyncEnd(/** @scrutinizer ignore-unused */ Event\Sync\End $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
341
    {
342 2
        if ($this->debug) {
343 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
344
        }
345 2
    }
346
347
    /**
348
     * Cleanup start event.
349
     *
350
     * @param \phpbu\App\Event\Cleanup\Start $event
351
     */
352 4
    public function onCleanupStart(Event\Cleanup\Start $event)
353
    {
354 4
        $this->numCleanups++;
355 4
        if ($this->debug) {
356 3
            $cleanup = $event->getConfiguration();
357 3
            $this->writeWithAsterisk('cleanup: [' . $cleanup->type . '] ');
358
        }
359 4
    }
360
361
    /**
362
     * Cleanup skipped event.
363
     *
364
     * @param \phpbu\App\Event\Cleanup\Skipped $event
365
     */
366 1
    public function onCleanupSkipped(Event\Cleanup\Skipped $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

366
    public function onCleanupSkipped(/** @scrutinizer ignore-unused */ Event\Cleanup\Skipped $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
367
    {
368 1
        if ($this->debug) {
369 1
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
370
        }
371 1
    }
372
373
    /**
374
     * Cleanup failed event.
375
     *
376
     * @param \phpbu\App\Event\Cleanup\Failed $event
377
     */
378 1
    public function onCleanupFailed(Event\Cleanup\Failed $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

378
    public function onCleanupFailed(/** @scrutinizer ignore-unused */ Event\Cleanup\Failed $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
379
    {
380 1
        if ($this->debug) {
381 1
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
382
        }
383 1
    }
384
385
    /**
386
     * Cleanup end event.
387
     *
388
     * @param \phpbu\App\Event\Cleanup\End $event
389
     */
390 2
    public function onCleanupEnd(Event\Cleanup\End $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

390
    public function onCleanupEnd(/** @scrutinizer ignore-unused */ Event\Cleanup\End $event)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
391
    {
392 2
        if ($this->debug) {
393 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
394
        }
395 2
    }
396
397
    /**
398
     * Debugging.
399
     *
400
     * @param \phpbu\App\Event\Debug $event
401
     */
402 1
    public function onDebug(Event\Debug $event)
403
    {
404 1
        if ($this->debug) {
405 1
            $this->write($event->getMessage() . PHP_EOL);
406
        }
407 1
    }
408
409
    /**
410
     * phpbu end event.
411
     *
412
     * @param \phpbu\App\Event\App\End $event
413
     */
414 1
    public function onPhpbuEnd(Event\App\End $event)
415
    {
416 1
        $result = $event->getResult();
417 1
        $this->printResult($result);
418 1
    }
419
420
    /**
421
     * Prints a result summary.
422
     *
423
     * @param \phpbu\App\Result $result
424
     */
425 5
    public function printResult(Result $result)
426
    {
427 5
        $this->printHeader();
428 5
        $this->printErrors($result);
429
430 5
        if ($this->verbose) {
431 3
            foreach ($result->getBackups() as $backup) {
432 3
                $this->printBackupVerbose($backup);
433
            }
434
        }
435 5
        $this->printFooter($result);
436 5
    }
437
438
    /**
439
     * Prints the result header with memory usage info.
440
     */
441 5
    protected function printHeader()
442
    {
443 5
        $this->write(Statistics::resourceUsage() . PHP_EOL . PHP_EOL);
444 5
    }
445
446
    /**
447
     * Print error information.
448
     *
449
     * @param \phpbu\App\Result $result
450
     */
451 5
    protected function printErrors(Result $result)
452
    {
453
        /* @var $e \Exception */
454 5
        foreach ($result->getErrors() as $e) {
455 1
            $this->write(
456 1
                sprintf(
457 1
                    "Exception '%s' with message '%s'\nin %s:%d\n\n",
458 1
                    get_class($e),
459 1
                    $e->getMessage(),
460 1
                    $e->getFile(),
461 1
                    $e->getLine()
462
                )
463
            );
464
        }
465 5
    }
466
467
    /**
468
     * Prints verbose backup information.
469
     *
470
     * @param \phpbu\App\Result\Backup $backup
471
     */
472 3
    protected function printBackupVerbose(Result\Backup $backup)
473
    {
474 3
        $this->write(sprintf('backup %s: ', $backup->getName()));
475 3
        if ($backup->allOk()) {
476 1
            $this->writeWithColor(
477 1
                'fg-green',
478 1
                'OK'
479
            );
480 2
        } elseif ($backup->okButSkipsOrFails()) {
481 1
            $this->writeWithColor(
482 1
                'fg-yellow',
483 1
                'OK, but skipped or failed Crypts, Syncs or Cleanups!'
484
            );
485
        } else {
486 1
            $this->writeWithColor(
487 1
                'fg-red',
488 1
                'FAILED'
489
            );
490
        }
491 3
        $chExecuted = str_pad($backup->checkCount(), 8, ' ', STR_PAD_LEFT);
492 3
        $chFailed   = str_pad($backup->checkCountFailed(), 6, ' ', STR_PAD_LEFT);
493 3
        $crExecuted = str_pad($backup->cryptCount(), 8, ' ', STR_PAD_LEFT);
494 3
        $crSkipped  = str_pad($backup->cryptCountSkipped(), 7, ' ', STR_PAD_LEFT);
495 3
        $crFailed   = str_pad($backup->cryptCountFailed(), 6, ' ', STR_PAD_LEFT);
496 3
        $syExecuted = str_pad($backup->syncCount(), 8, ' ', STR_PAD_LEFT);
497 3
        $sySkipped  = str_pad($backup->syncCountSkipped(), 7, ' ', STR_PAD_LEFT);
498 3
        $syFailed   = str_pad($backup->syncCountFailed(), 6, ' ', STR_PAD_LEFT);
499 3
        $clExecuted = str_pad($backup->cleanupCount(), 8, ' ', STR_PAD_LEFT);
500 3
        $clSkipped  = str_pad($backup->cleanupCountSkipped(), 7, ' ', STR_PAD_LEFT);
501 3
        $clFailed   = str_pad($backup->cleanupCountFailed(), 6, ' ', STR_PAD_LEFT);
502
503 3
        $out = PHP_EOL . '          | executed | skipped | failed |' . PHP_EOL
504 3
            . '----------+----------+---------+--------+' . PHP_EOL
505 3
            . ' checks   | ' . $chExecuted . ' |         | ' . $chFailed . ' |' . PHP_EOL
506 3
            . ' crypts   | ' . $crExecuted . ' | ' . $crSkipped . ' | ' . $crFailed . ' |' . PHP_EOL
507 3
            . ' syncs    | ' . $syExecuted . ' | ' . $sySkipped . ' | ' . $syFailed . ' |' . PHP_EOL
508 3
            . ' cleanups | ' . $clExecuted . ' | ' . $clSkipped . ' | ' . $clFailed . ' |' . PHP_EOL
509 3
            . '----------+----------+---------+--------+' . PHP_EOL . PHP_EOL;
510
511 3
        $this->write($out);
512 3
    }
513
514
    /**
515
     * Prints 'OK' or 'FAILURE' footer.
516
     *
517
     * @param Result $result
518
     */
519 5
    protected function printFooter(Result $result)
520
    {
521 5
        if (count($result->getBackups()) === 0) {
522 2
            $this->writeWithColor(
523 2
                'fg-yellow',
524 2
                'No backups executed!'
525
            );
526 3
        } elseif ($result->allOk()) {
527 1
            $this->writeWithColor(
528 1
                'fg-green',
529 1
                sprintf(
530 1
                    'OK (%d %s, %d %s, %d %s, %d %s, %d %s)' . PHP_EOL,
531 1
                    count($result->getBackups()),
532 1
                    Util\Str::appendPluralS('backup', count($result->getBackups())),
533 1
                    $this->numChecks,
534 1
                    Util\Str::appendPluralS('check', $this->numChecks),
535 1
                    $this->numCrypts,
536 1
                    Util\Str::appendPluralS('crypt', $this->numCrypts),
537 1
                    $this->numSyncs,
538 1
                    Util\Str::appendPluralS('sync', $this->numSyncs),
539 1
                    $this->numCleanups,
540 1
                    Util\Str::appendPluralS('cleanup', $this->numCleanups)
541
                )
542
            );
543 2
        } elseif ($result->backupOkButSkipsOrFails()) {
544 1
            $this->writeWithColor(
545 1
                'fg-yellow',
546 1
                sprintf(
547 1
                    "WARNING, skipped|failed Crypts, Syncs or Cleanups!" . PHP_EOL .
548 1
                    'Backups: %d, Crypts: %d|%d, Syncs: %d|%d, Cleanups: %d|%d ' . PHP_EOL,
549 1
                    count($result->getBackups()),
550 1
                    $result->cryptsSkippedCount(),
551 1
                    $result->cryptsFailedCount(),
552 1
                    $result->syncsSkippedCount(),
553 1
                    $result->syncsFailedCount(),
554 1
                    $result->cleanupsSkippedCount(),
555 1
                    $result->cleanupsFailedCount()
556
                )
557
            );
558
        } else {
559 1
            $this->writeWithColor(
560 1
                'fg-red',
561 1
                sprintf(
562 1
                    "FAILURE!" . PHP_EOL .
563 1
                    'Backups: %d, '
564 1
                    . 'failed Checks: %d, '
565 1
                    . 'failed Crypts: %d, '
566 1
                    . 'failed Syncs: %d, '
567 1
                    . 'failed Cleanups: %d.' . PHP_EOL,
568 1
                    count($result->getBackups()),
569 1
                    $result->checksFailedCount(),
570 1
                    $result->cryptsFailedCount(),
571 1
                    $result->syncsFailedCount(),
572 1
                    $result->cleanupsFailedCount()
573
                )
574
            );
575
        }
576 5
    }
577
578
    /**
579
     * Writes a buffer out with a color sequence if colors are enabled.
580
     *
581
     * @author Sebastian Bergmann <[email protected]>
582
     * @param  string $color
583
     * @param  string $buffer
584
     */
585 18
    protected function writeWithColor($color, $buffer)
586
    {
587 18
        if ($this->colors) {
588 1
            $buffer = Util\Cli::formatWithColor($color, $buffer);
589
        }
590 18
        $this->write($buffer . PHP_EOL);
591 18
    }
592
593
    /**
594
     * Writes a buffer with Ansible like asterisk decoration.
595
     *
596
     * @param string $buffer
597
     */
598 13
    protected function writeWithAsterisk($buffer)
599
    {
600 13
        $this->write(Util\Cli::formatWithAsterisk($buffer));
601 13
    }
602
603
    /**
604
     * Writes a buffer.
605
     *
606
     * @param string $buffer
607
     */
608 20
    public function write($buffer)
609
    {
610 20
        if (PHP_SAPI != 'cli') {
611
            $buffer = htmlspecialchars($buffer);
612
        }
613 20
        echo $buffer;
614 20
    }
615
}
616