Completed
Push — master ( 98071e...5af57d )
by Sebastian
02:55
created

PrinterCli::printFooter()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 54
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 41
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 54
ccs 41
cts 41
cp 1
rs 9.0306
c 1
b 0
f 0
cc 4
eloc 44
nc 4
nop 1
crap 4

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace phpbu\App\Result;
3
4
use InvalidArgumentException;
5
use phpbu\App\Event;
6
use phpbu\App\Listener;
7
use phpbu\App\Result;
8
use phpbu\App\Util;
9
use phpbu\App\Version;
10
use PHP_Timer;
11
use SebastianBergmann\Environment\Console;
12
use SebastianBergmann\Environment\Runtime;
13
14
/**
15
 * Default app output.
16
 *
17
 * Heavily 'inspired' by Sebastian Bergmann's phpunit PHPUnit_TextUI_ResultPrinter.
18
 *
19
 * @package    phpbu
20
 * @subpackage Result
21
 * @author     Sebastian Feldmann <[email protected]>
22
 * @copyright  Sebastian Feldmann <[email protected]>
23
 * @license    https://opensource.org/licenses/MIT The MIT License (MIT)
24
 * @link       http://phpbu.de/
25
 * @since      Class available since Release 1.0.0
26
 */
27
class PrinterCli implements Listener
28
{
29
    /**
30
     * Verbose output
31
     *
32
     * @var bool
33
     */
34
    protected $verbose = false;
35
36
    /**
37
     * Output with colors
38
     *
39
     * @var bool
40
     */
41
    protected $colors = false;
42
43
    /**
44
     * Is debug active
45
     *
46
     * @var bool
47
     */
48
    protected $debug = false;
49
50
    /**
51
     * Amount of executed backups
52
     *
53
     * @var integer
54
     */
55
    private $numBackups = 0;
56
57
    /**
58
     * Amount of executed checks
59
     *
60
     * @var integer
61
     */
62
    private $numChecks = 0;
63
64
    /**
65
     * Amount of executed crypts
66
     *
67
     * @var integer
68
     */
69
    private $numCrypts = 0;
70
71
    /**
72
     * Amount of executed Syncs
73
     *
74
     * @var integer
75
     */
76
    private $numSyncs = 0;
77
78
    /**
79
     * Amount of executed Cleanups
80
     *
81
     * @var integer
82
     */
83
    private $numCleanups = 0;
84
85
    /**
86
     * Console
87
     *
88
     * @var \SebastianBergmann\Environment\Console
89
     */
90
    private $console;
91
92
    /**
93
     * PHP Runtime
94
     *
95
     * @var \SebastianBergmann\Environment\Runtime
96
     */
97
    private $runtime;
98
99
    /**
100
     * Returns an array of event names this subscriber wants to listen to.
101
     *
102
     * The array keys are event names and the value can be:
103
     *
104
     *  * The method name to call (priority defaults to 0)
105
     *  * An array composed of the method name to call and the priority
106
     *  * An array of arrays composed of the method names to call and respective
107
     *    priorities, or 0 if unset
108
     *
109
     * @return array The event names to listen to
110
     */
111
    public static function getSubscribedEvents()
112
    {
113 2
        return [
114
            'phpbu.debug'           => 'onDebug',
115
            'phpbu.app_start'       => 'onPhpbuStart',
116 2
            'phpbu.backup_start'    => 'onBackupStart',
117 2
            'phpbu.backup_failed'   => 'onBackupFailed',
118 2
            'phpbu.backup_end'      => 'onBackupEnd',
119 2
            'phpbu.check_start'     => 'onCheckStart',
120 2
            'phpbu.check_failed'    => 'onCheckFailed',
121 2
            'phpbu.check_end'       => 'onCheckEnd',
122 2
            'phpbu.crypt_start'     => 'onCryptStart',
123 2
            'phpbu.crypt_skipped'   => 'onCryptSkipped',
124 2
            'phpbu.crypt_failed'    => 'onCryptFailed',
125 2
            'phpbu.crypt_end'       => 'onCryptEnd',
126 2
            'phpbu.sync_start'      => 'onSyncStart',
127 2
            'phpbu.sync_skipped'    => 'onSyncSkipped',
128 2
            'phpbu.sync_failed'     => 'onSyncFailed',
129 2
            'phpbu.sync_end'        => 'onSyncEnd',
130 2
            'phpbu.cleanup_start'   => 'onCleanupStart',
131 2
            'phpbu.cleanup_skipped' => 'onCleanupSkipped',
132 2
            'phpbu.cleanup_failed'  => 'onCleanupFailed',
133 2
            'phpbu.cleanup_end'     => 'onCleanupEnd',
134 2
            'phpbu.app_end'         => 'onPhpbuEnd',
135 2
        ];
136 2
    }
137 2
138
    /**
139
     * Constructor
140
     *
141
     * @param  bool $verbose
142
     * @param  bool $colors
143
     * @param  bool $debug
144
     * @throws \InvalidArgumentException
145
     */
146
    public function __construct(bool $verbose = false, bool $colors = false, bool $debug = false)
147
    {
148 30
        $this->console = new Console;
149
        $this->runtime = new Runtime;
150 30
        $this->debug   = $debug;
151 29
        $this->verbose = $verbose;
152 29
        $this->colors  = $colors && $this->console->hasColorSupport();
153 1
    }
154
155
    /**
156 29
     * phpbu start event.
157 28
     *
158 28
     * @param \phpbu\App\Event\App\Start $event
159 28
     */
160 1
    public function onPhpbuStart(Event\App\Start $event)
161
    {
162
        $configuration = $event->getConfiguration();
163 28
        if ($this->verbose) {
164 27
            $this->write(
165 27
                'Runtime:       ' . $this->runtime->getNameWithVersion() . PHP_EOL .
166 1
                'Configuration: ' . $configuration->getFilename() . PHP_EOL .
167
                PHP_EOL
168 27
            );
169
        }
170
    }
171
172
    /**
173
     * Backup start event.
174
     *
175 1
     * @param \phpbu\App\Event\Backup\Start $event
176
     */
177 1
    public function onBackupStart(Event\Backup\Start $event)
178 1
    {
179 1
        $this->numBackups++;
180 1
        if ($this->debug) {
181 1
            $backup = $event->getConfiguration();
182
            $this->writeWithAsterisk('backup: [' . $backup->getSource()->type . '] ');
183 1
        }
184 1
    }
185
186
    /**
187
     * Backup failed event.
188
     *
189
     * @param \phpbu\App\Event\Backup\Failed $event
190
     */
191 3
    public function onBackupFailed(Event\Backup\Failed $event)
192
    {
193 3
        if ($this->debug) {
194 3
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
195 3
        }
196 2
    }
197 2
198 3
    /**
199
     * Backup end event.
200
     *
201
     * @param \phpbu\App\Event\Backup\End $event
202
     */
203
    public function onBackupEnd(Event\Backup\End $event)
204
    {
205 1
        if ($this->debug) {
206
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
207 1
        }
208 1
    }
209 1
210
    /**
211 1
     * Check start event.
212 1
     *
213 1
     * @param \phpbu\App\Event\Check\Start $event
214
     */
215
    public function onCheckStart(Event\Check\Start $event)
216
    {
217
        $this->numChecks++;
218
        if ($this->debug) {
219
            $check = $event->getConfiguration();
220 2
            $this->writeWithAsterisk('check: [' . $check->type . '] ');
221
            $this->write('checking: [' . $check->value . '] ');
222 2
        }
223 1
    }
224 1
225 2
    /**
226
     * Check failed event.
227
     *
228
     * @param \phpbu\App\Event\Check\Failed $event
229
     */
230
    public function onCheckFailed(Event\Check\Failed $event)
231
    {
232 3
        if ($this->debug) {
233
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
234 3
        }
235 3
    }
236 3
237 2
    /**
238 2
     * Check end event.
239 3
     *
240
     * @param \phpbu\App\Event\Check\End $event
241
     */
242
    public function onCheckEnd(Event\Check\End $event)
243
    {
244
        if ($this->debug) {
245
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
246 1
        }
247
    }
248 1
249 1
    /**
250 1
     * Crypt start event.
251
     *
252 1
     * @param \phpbu\App\Event\Crypt\Start $event
253 1
     */
254 1
    public function onCryptStart(Event\Crypt\Start $event)
255
    {
256
        $this->numCrypts++;
257
        if ($this->debug) {
258
            $crypt = $event->getConfiguration();
259
            $this->writeWithAsterisk('crypt: [' . $crypt->type . '] ');
260
        }
261 2
    }
262
263 2
    /**
264 1
     * Crypt skipped event.
265 1
     *
266 2
     * @param \phpbu\App\Event\Crypt\Skipped $event
267
     */
268
    public function onCryptSkipped(Event\Crypt\Skipped $event)
269
    {
270
        if ($this->debug) {
271
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
272
        }
273 4
    }
274
275 4
    /**
276 4
     * Crypt failed event.
277 4
     *
278 3
     * @param \phpbu\App\Event\Crypt\Failed $event
279 3
     */
280 4
    public function onCryptFailed(Event\Crypt\Failed $event)
281
    {
282
        if ($this->debug) {
283
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
284
        }
285
    }
286
287 1
    /**
288
     * Crypt end event.
289 1
     *
290 1
     * @param \phpbu\App\Event\Crypt\End $event
291 1
     */
292
    public function onCryptEnd(Event\Crypt\End $event)
293 1
    {
294 1
        if ($this->debug) {
295 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
296
        }
297
    }
298
299
    /**
300
     * Sync start event.
301
     *
302 1
     * @param \phpbu\App\Event\Sync\Start $event
303
     */
304 1
    public function onSyncStart(Event\Sync\Start $event)
305 1
    {
306 1
        $this->numSyncs++;
307
        if ($this->debug) {
308 1
            $sync = $event->getConfiguration();
309 1
            $this->writeWithAsterisk('sync: [' . $sync->type . '] ');
310 1
        }
311
    }
312
313
    /**
314
     * Sync skipped event.
315
     *
316
     * @param \phpbu\App\Event\Sync\Skipped $event
317 2
     */
318
    public function onSyncSkipped(Event\Sync\Skipped $event)
319 2
    {
320 1
        if ($this->debug) {
321 1
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
322 2
        }
323
    }
324
325
    /**
326
     * Sync failed event.
327
     *
328
     * @param \phpbu\App\Event\Sync\Failed $event
329 4
     */
330
    public function onSyncFailed(Event\Sync\Failed $event)
331 4
    {
332 4
        if ($this->debug) {
333 4
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
334 3
        }
335 3
    }
336 4
337
    /**
338
     * Sync end event.
339
     *
340
     * @param \phpbu\App\Event\Sync\End $event
341
     */
342
    public function onSyncEnd(Event\Sync\End $event)
343 1
    {
344
        if ($this->debug) {
345 1
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
346 1
        }
347 1
    }
348
349 1
    /**
350 1
     * Cleanup start event.
351 1
     *
352
     * @param \phpbu\App\Event\Cleanup\Start $event
353
     */
354
    public function onCleanupStart(Event\Cleanup\Start $event)
355
    {
356
        $this->numCleanups++;
357
        if ($this->debug) {
358 1
            $cleanup = $event->getConfiguration();
359
            $this->writeWithAsterisk('cleanup: [' . $cleanup->type . '] ');
360 1
        }
361 1
    }
362 1
363
    /**
364 1
     * Cleanup skipped event.
365 1
     *
366 1
     * @param \phpbu\App\Event\Cleanup\Skipped $event
367
     */
368
    public function onCleanupSkipped(Event\Cleanup\Skipped $event)
369
    {
370
        if ($this->debug) {
371
            $this->writeWithColor('fg-black, bg-yellow', 'skipped' . PHP_EOL);
372
        }
373 2
    }
374
375 2
    /**
376 1
     * Cleanup failed event.
377 1
     *
378 2
     * @param \phpbu\App\Event\Cleanup\Failed $event
379
     */
380
    public function onCleanupFailed(Event\Cleanup\Failed $event)
381
    {
382
        if ($this->debug) {
383
            $this->writeWithColor('fg-white, bg-red, bold', 'failed' . PHP_EOL);
384
        }
385 4
    }
386
387 4
    /**
388 4
     * Cleanup end event.
389 4
     *
390 3
     * @param \phpbu\App\Event\Cleanup\End $event
391 3
     */
392 4
    public function onCleanupEnd(Event\Cleanup\End $event)
393
    {
394
        if ($this->debug) {
395
            $this->writeWithColor('fg-black, bg-green', 'ok' . PHP_EOL);
396
        }
397
    }
398
399 1
    /**
400
     * Debugging.
401 1
     *
402 1
     * @param \phpbu\App\Event\Debug $event
403 1
     */
404
    public function onDebug(Event\Debug $event)
405 1
    {
406 1
        if ($this->debug) {
407 1
            $this->write($event->getMessage() . PHP_EOL);
408
        }
409
    }
410
411
    /**
412
     * phpbu end event.
413
     *
414 1
     * @param \phpbu\App\Event\App\End $event
415
     */
416 1
    public function onPhpbuEnd(Event\App\End $event)
417 1
    {
418 1
        $result = $event->getResult();
419
        $this->printResult($result);
420 1
    }
421 1
422 1
    /**
423
     * Prints a result summary.
424
     *
425
     * @param \phpbu\App\Result $result
426
     */
427
    public function printResult(Result $result)
428
    {
429 2
        $this->printHeader();
430
        $this->printErrors($result);
431 2
432 1
        if ($this->verbose) {
433 1
            foreach ($result->getBackups() as $backup) {
434 2
                $this->printBackupVerbose($backup);
435
            }
436
        }
437
        $this->printFooter($result);
438
    }
439
440
    /**
441 1
     * Prints the result header with memory usage info.
442
     */
443 1
    protected function printHeader()
444 1
    {
445 1
        $this->write(PHP_Timer::resourceUsage() . PHP_EOL . PHP_EOL);
446 1
    }
447
448
    /**
449
     * Print error information.
450
     *
451
     * @param \phpbu\App\Result $result
452
     */
453 1
    protected function printErrors(Result $result)
454
    {
455 1
        /* @var $e \Exception */
456 1
        foreach ($result->getErrors() as $e) {
457 1
            $this->write(
458
                sprintf(
459
                    "Exception '%s' with message '%s'\nin %s:%d\n\n",
460
                    get_class($e),
461
                    $e->getMessage(),
462
                    $e->getFile(),
463
                    $e->getLine()
464 5
                )
465
            );
466 5
        }
467 5
    }
468
469 5
    /**
470 3
     * Prints verbose backup information.
471 3
     *
472 3
     * @param \phpbu\App\Result\Backup $backup
473 3
     */
474 5
    protected function printBackupVerbose(Result\Backup $backup)
475 5
    {
476
        $this->write(sprintf('backup %s: ', $backup->getName()));
477
        if ($backup->allOk()) {
478
            $this->writeWithColor(
479
                'fg-black, bg-green',
480 5
                'OK'
481
            );
482 5
        } elseif ($backup->okButSkipsOrFails()) {
483 5
                $this->writeWithColor(
484
                    'fg-black, bg-yellow',
485
                    'OK, but skipped or failed Crypts, Syncs or Cleanups!'
486
                );
487
        } else {
488
            $this->writeWithColor(
489
                'fg-white, bg-red, bold',
490 5
                'FAILED'
491
            );
492
        }
493 5
        $chExecuted = str_pad($backup->checkCount(), 8, ' ', STR_PAD_LEFT);
494 1
        $chFailed   = str_pad($backup->checkCountFailed(), 6, ' ', STR_PAD_LEFT);
495 1
        $crExecuted = str_pad($backup->cryptCount(), 8, ' ', STR_PAD_LEFT);
496 1
        $crSkipped  = str_pad($backup->cryptCountSkipped(), 7, ' ', STR_PAD_LEFT);
497 1
        $crFailed   = str_pad($backup->cryptCountFailed(), 6, ' ', STR_PAD_LEFT);
498 1
        $syExecuted = str_pad($backup->syncCount(), 8, ' ', STR_PAD_LEFT);
499 1
        $sySkipped  = str_pad($backup->syncCountSkipped(), 7, ' ', STR_PAD_LEFT);
500 1
        $syFailed   = str_pad($backup->syncCountFailed(), 6, ' ', STR_PAD_LEFT);
501 1
        $clExecuted = str_pad($backup->cleanupCount(), 8, ' ', STR_PAD_LEFT);
502 1
        $clSkipped  = str_pad($backup->cleanupCountSkipped(), 7, ' ', STR_PAD_LEFT);
503 5
        $clFailed   = str_pad($backup->cleanupCountFailed(), 6, ' ', STR_PAD_LEFT);
504 5
505
        $out = PHP_EOL . '          | executed | skipped | failed |' . PHP_EOL
506
            . '----------+----------+---------+--------+' . PHP_EOL
507
            . ' checks   | ' . $chExecuted . ' |         | ' . $chFailed . ' |' . PHP_EOL
508
            . ' crypts   | ' . $crExecuted . ' | ' . $crSkipped . ' | ' . $crFailed . ' |' . PHP_EOL
509
            . ' syncs    | ' . $syExecuted . ' | ' . $sySkipped . ' | ' . $syFailed . ' |' . PHP_EOL
510
            . ' cleanups | ' . $clExecuted . ' | ' . $clSkipped . ' | ' . $clFailed . ' |' . PHP_EOL
511 3
            . '----------+----------+---------+--------+' . PHP_EOL . PHP_EOL;
512
513 3
        $this->write($out);
514 3
    }
515 1
516 1
    /**
517
     * Prints 'OK' or 'FAILURE' footer.
518 1
     *
519 3
     * @param Result $result
520 1
     */
521 1
    protected function printFooter(Result $result)
522
    {
523 1
        if (count($result->getBackups()) === 0) {
524 1
            $this->writeWithColor(
525 1
                'fg-black, bg-yellow',
526 1
                'No backups executed!'
527
            );
528 1
        } elseif ($result->allOk()) {
529
            $this->writeWithColor(
530 3
                'fg-black, bg-green',
531 3
                sprintf(
532 3
                    'OK (%d %s, %d %s, %d %s, %d %s, %d %s)' . PHP_EOL,
533 3
                    count($result->getBackups()),
534 3
                    Util\Str::appendPluralS('backup', count($result->getBackups())),
535 3
                    $this->numChecks,
536 3
                    Util\Str::appendPluralS('check', $this->numChecks),
537 3
                    $this->numCrypts,
538 3
                    Util\Str::appendPluralS('crypt', $this->numCrypts),
539 3
                    $this->numSyncs,
540 3
                    Util\Str::appendPluralS('sync', $this->numSyncs),
541
                    $this->numCleanups,
542 3
                    Util\Str::appendPluralS('cleanup', $this->numCleanups)
543 3
                )
544 3
            );
545 3
        } elseif ($result->backupOkButSkipsOrFails()) {
546 3
            $this->writeWithColor(
547 3
                'fg-black, bg-yellow',
548 3
                sprintf(
549
                    "WARNING, skipped|failed Crypts, Syncs or Cleanups!" . PHP_EOL .
550 3
                    'Backups: %d, Crypts: %d|%d, Syncs: %d|%d, Cleanups: %d|%d ' . PHP_EOL,
551 3
                    count($result->getBackups()),
552
                    $result->cryptsSkippedCount(),
553
                    $result->cryptsFailedCount(),
554
                    $result->syncsSkippedCount(),
555
                    $result->syncsFailedCount(),
556
                    $result->cleanupsSkippedCount(),
557
                    $result->cleanupsFailedCount()
558 5
                )
559
            );
560 5
        } else {
561 2
            $this->writeWithColor(
562 2
                'fg-white, bg-red',
563
                sprintf(
564 2
                    "FAILURE!" . PHP_EOL .
565 5
                    'Backups: %d, failed Checks: %d, failed Crypts: %d, failed Syncs: %d, failed Cleanups: %d.' . PHP_EOL,
566 1
                    count($result->getBackups()),
567 1
                    $result->checksFailedCount(),
568 1
                    $result->cryptsFailedCount(),
569 1
                    $result->syncsFailedCount(),
570 1
                    $result->cleanupsFailedCount()
571 1
                )
572 1
            );
573 1
        }
574 1
    }
575 1
576 1
    /**
577 1
     * Writes a buffer out with a color sequence if colors are enabled.
578 1
     *
579 1
     * @author Sebastian Bergmann <[email protected]>
580 1
     * @param  string $color
581 1
     * @param  string $buffer
582 3
     */
583 1
    protected function writeWithColor($color, $buffer)
584 1
    {
585 1
        if ($this->colors) {
586
            $buffer = Util\Cli::formatWithColor($color, $buffer);
587 1
        }
588 1
        $this->write($buffer . PHP_EOL);
589 1
    }
590 1
591 1
    /**
592 1
     * Writes a buffer with Ansible like asterisk decoration.
593 1
     *
594 1
     * @param string $buffer
595 1
     */
596 1
    protected function writeWithAsterisk($buffer)
597 1
    {
598 1
        $this->write(Util\Cli::formatWithAsterisk($buffer));
599 1
    }
600 1
601
    /**
602 1
     * Writes a buffer.
603 1
     *
604 1
     * @param string $buffer
605 1
     */
606 1
    public function write($buffer)
607 1
    {
608 1
        if (PHP_SAPI != 'cli') {
609 1
            $buffer = htmlspecialchars($buffer);
610
        }
611 5
        echo $buffer;
612
    }
613
}
614