Printer   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 127
Duplicated Lines 0 %

Test Coverage

Coverage 97.62%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 37
c 1
b 0
f 0
dl 0
loc 127
ccs 41
cts 42
cp 0.9762
rs 10
wmc 17

8 Methods

Rating   Name   Duplication   Size   Complexity  
A actionSkipped() 0 3 1
A actionDeactivated() 0 3 1
A formatActionHeadline() 0 8 2
A actionFailed() 0 3 1
A hookEnded() 0 21 6
A __construct() 0 11 4
A actionHeadline() 0 3 1
A actionSucceeded() 0 3 1
1
<?php
2
3
/**
4
 * This file is part of CaptainHook.
5
 *
6
 * (c) Sebastian Feldmann <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CaptainHook\App\Runner\Hook;
13
14
use CaptainHook\App\Config\Action;
15
use CaptainHook\App\Console\IO;
16
use CaptainHook\App\Runner\Action\Log as ActionLog;
17
use CaptainHook\App\Runner\Hook;
18
19
/**
20
 * Class Printer
21
 *
22
 * Is handling the output for the hook execution
23
 *
24
 * @package CaptainHook
25
 * @author  Sebastian Feldmann <[email protected]>
26
 * @link    https://github.com/captainhook-git/captainhook
27
 * @since   Class available since Release 5.19.0
28
 */
29
class Printer
30
{
31
    /**
32
     * @var \CaptainHook\App\Console\IO
33
     */
34
    private IO $io;
35
36
    /**
37
     * Current verbosity
38
     *
39
     * @var int
40
     */
41
    private int $verbosity;
42
43
    /**
44
     * Constructor
45
     *
46
     * @param \CaptainHook\App\Console\IO $io
47
     */
48 48
    public function __construct(IO $io)
49
    {
50 48
        $this->io = $io;
51 48
        if ($io->isDebug()) {
52 1
            $this->verbosity = IO::DEBUG;
53 47
        } elseif ($io->isVeryVerbose()) {
54 1
            $this->verbosity = IO::VERY_VERBOSE;
55 46
        } elseif ($io->isVerbose()) {
56 1
            $this->verbosity = IO::VERBOSE;
57
        } else {
58 45
            $this->verbosity = IO::NORMAL;
59
        }
60
    }
61
62
    /**
63
     * Prints the action success line
64
     *
65
     * @param \CaptainHook\App\Config\Action $action
66
     * @return void
67
     */
68 13
    public function actionSucceeded(Action $action): void
69
    {
70 13
        $this->io->write($this->actionHeadline($action) . '<info>done</info>');
71
    }
72
73
    /**
74
     * Prints the action skipped line
75
     *
76
     * @param \CaptainHook\App\Config\Action $action
77
     * @return void
78
     */
79 1
    public function actionSkipped(Action $action): void
80
    {
81 1
        $this->io->write($this->actionHeadline($action) . '<comment>skipped</comment>');
82
    }
83
84
    /**
85
     * Prints the action failed line
86
     *
87
     * @param \CaptainHook\App\Config\Action $action
88
     * @return void
89
     */
90 6
    public function actionFailed(Action $action): void
91
    {
92 6
        $this->io->write($this->actionHeadline($action) . '<fg=red>failed</>');
93
    }
94
95
    /**
96
     * Prints the action deactivated line
97
     *
98
     * @param \CaptainHook\App\Config\Action $action
99
     * @return void
100
     */
101 2
    public function actionDeactivated(Action $action): void
102
    {
103 2
        $this->io->write($this->actionHeadline($action) . '<comment>deactivated</comment>');
104
    }
105
106 19
    private function actionHeadline(Action $action): string
107
    {
108 19
        return ' - <fg=blue>' . $this->formatActionHeadline($action->getLabel()) . '</> : ';
109
    }
110
111
    /**
112
     * Some fancy output formatting
113
     *
114
     * @param  string $action
115
     * @return string
116
     */
117 19
    private function formatActionHeadline(string $action): string
118
    {
119 19
        $actionLength = 65;
120 19
        if (mb_strlen($action) < $actionLength) {
121 19
            return str_pad($action, $actionLength, ' ');
122
        }
123
124
        return mb_substr($action, 0, $actionLength - 3) . '...';
125
    }
126
127
    /**
128
     * Prints the action log
129
     *
130
     * @param int                              $status
131
     * @param \CaptainHook\App\Runner\Hook\Log $log
132
     * @param float                            $seconds
133
     * @return void
134
     */
135 22
    public function hookEnded(int $status, Log $log, float $seconds): void
136
    {
137 22
        $msg = '<fg=green>captainhook executed all actions successfully, took: %01.2fs</>';
138 22
        if ($status === Hook::HOOK_FAILED) {
139 4
            $msg = '<fg=red>captainhook failed executing all actions, took: %01.2fs</>';
140
        }
141 22
        $this->io->write(sprintf($msg, $seconds));
142
143 22
        $tags = [
144 22
            ActionLog::ACTION_FAILED    => 'fg=red',
145 22
            ActionLog::ACTION_SKIPPED   => 'fg=yellow',
146 22
            ActionLog::ACTION_SUCCEEDED => 'fg=green',
147 22
        ];
148 22
        if ($log->hasMessageForVerbosity($this->verbosity)) {
149 9
            $this->io->write('');
150 9
            foreach ($log->logs() as $actionLog) {
151 9
                if ($actionLog->hasMessageForVerbosity($this->verbosity)) {
152 9
                    $tag = $tags[$actionLog->status()];
153 9
                    $this->io->write('<' . $tag . '>' . $actionLog->name() . '</>');
154 9
                    foreach ($actionLog->messages() as $msg) {
155 9
                        $this->io->write($msg->message(), $msg->newLine(), $msg->verbosity());
156
                    }
157
                }
158
            }
159
        }
160
    }
161
}
162