Informer::taskException()   B
last analyzed

Complexity

Conditions 10
Paths 36

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
cc 10
nc 36
nop 2
dl 0
loc 43
ccs 0
cts 27
cp 0
crap 110
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

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
/* (c) Anton Medvedev <[email protected]>
3
 *
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace Deployer\Console\Output;
9
10
use Deployer\Deployer;
11
use Deployer\Host\Host;
12
use Deployer\Task\Task;
13
use Symfony\Component\Console\Helper\FormatterHelper;
14
use Symfony\Component\Console\Output\OutputInterface;
15
16
class Informer
17
{
18
    /**
19
     * @var OutputWatcher
20
     */
21
    private $output;
22
23
    /**
24
     * @var int|double
25
     */
26
    private $startTime;
27
28 20
    public function __construct(OutputWatcher $output)
29
    {
30 20
        $this->output = $output;
31 20
    }
32
33 17
    public function startTask(Task $task)
34
    {
35 17
        $this->startTime = round(microtime(true) * 1000);
36
        if (
37 17
            $this->output->getVerbosity() >= OutputInterface::VERBOSITY_NORMAL &&
38 17
            !$task->isShallow()
39
        ) {
40 17
            $this->output->writeln("➤ Executing task <info>{$task->getName()}</info>");
41 17
            $this->output->setWasWritten(false);
42
        }
43 17
    }
44
45
    /**
46
     * Print task was ok.
47
     */
48 18
    public function endTask(Task $task)
49
    {
50 18
        if ($task->isShallow()) {
51 3
            return;
52
        }
53
54 18
        $endTime = round(microtime(true) * 1000);
55 18
        $millis = $endTime - $this->startTime;
56 18
        $seconds = floor($millis / 1000);
57 18
        $millis = $millis - $seconds * 1000;
58 18
        $taskTime = ($seconds > 0 ? "{$seconds}s " : "") . "{$millis}ms";
59
60
        $shouldReplaceTaskMark =
61 18
            $this->output->isDecorated() &&
62 18
            $this->output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL &&
63 18
            !$this->output->getWasWritten();
64
65 18
        if ($shouldReplaceTaskMark) {
66
            $this->output->writeln("\r\033[K\033[1A\r<info>✔</info>");
67
        } else {
68 18
            if ($this->output->getVerbosity() == OutputInterface::VERBOSITY_NORMAL) {
69 13
                $this->output->writeln("<info>✔</info> Ok");
70
            } else {
71 5
                $this->output->writeln("<info>✔</info> Ok [$taskTime]");
72
            }
73
        }
74 18
    }
75
76 10
    public function endOnHost(string $hostname)
77
    {
78 10
        if ($this->output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
79 1
            $this->output->writeln("<info>•</info> done on [$hostname]");
80
        }
81 10
    }
82
83
    /**
84
     * Print error.
85
     *
86
     * @param bool $nonFatal
87
     */
88 2
    public function taskError($nonFatal = true)
89
    {
90 2
        if ($nonFatal) {
91 1
            $this->output->writeln("<fg=yellow>✘</fg=yellow> Some errors occurred!");
92
        } else {
93 1
            $this->output->writeln("<fg=red>✘</fg=red> <options=underscore>Some errors occurred!</options=underscore>");
94
        }
95 2
    }
96
97
    /**
98
     * @param \Throwable $exception
99
     * @param Host $host
100
     */
101
    public function taskException($exception, $host = null)
102
    {
103
        /** @var FormatterHelper $formatter */
104
        $formatter = Deployer::get()->getHelper('formatter');
105
        $messages = array_filter(array_map('trim', explode("\n", $exception->getMessage())), function ($line) {
106
            return !empty($line);
107
        });
108
        $exceptionClass = get_class($exception);
109
110
        if (empty($host)) {
111
            array_unshift($messages, "[$exceptionClass]");
112
        } else {
113
            array_unshift($messages, "[$exceptionClass] on [{$host->getHostname()}]");
114
        }
115
116
        $this->output->writeln($formatter->formatBlock($messages, 'error', true));
117
        $this->output->writeln('');
118
119
        if (OutputInterface::VERBOSITY_VERBOSE <= $this->output->getVerbosity()) {
120
            $this->output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET);
121
122
            // exception related properties
123
            $trace = $exception->getTrace();
124
            array_unshift($trace, [
125
                'function' => '',
126
                'file' => $exception->getFile() !== null ? $exception->getFile() : 'n/a',
127
                'line' => $exception->getLine() !== null ? $exception->getLine() : 'n/a',
128
                'args' => [],
129
            ]);
130
131
            for ($i = 0, $count = count($trace); $i < $count; ++$i) {
132
                $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : '';
133
                $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : '';
134
                $function = $trace[$i]['function'];
135
                $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a';
136
                $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a';
137
138
                $this->output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line), OutputInterface::VERBOSITY_QUIET);
139
            }
140
141
            $this->output->writeln('', OutputInterface::VERBOSITY_QUIET);
142
        }
143
    }
144
}
145