Completed
Push — master ( be53ba...d8ac27 )
by Jan Philipp
11s
created

ClimateLogger::log()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php declare(strict_types=1);
2
3
namespace Shopware\Psh\Application;
4
5
use Khill\Duration\Duration;
6
use League\CLImate\CLImate;
7
use Shopware\Psh\Listing\Script;
8
use Shopware\Psh\ScriptRuntime\Logger;
9
use Shopware\Psh\ScriptRuntime\LogMessage;
10
11
/**
12
 * A CLImate implementation of the runtime logger
13
 */
14
class ClimateLogger implements Logger
15
{
16
    /**
17
     * @var CLImate
18
     */
19
    public $cliMate;
20
21
    /**
22
     * @var int
23
     */
24
    private $scriptStartTime;
25
26
    /**
27
     * @var Duration
28
     */
29
    private $duration;
30
31
    /**
32
     * @param CLImate $cliMate
33
     * @param Duration $duration
34
     */
35
    public function __construct(CLImate $cliMate, Duration $duration)
36
    {
37
        $this->cliMate = $cliMate;
38
        $this->duration = $duration;
39
    }
40
41
    /**
42
     * @param Script $script
43
     */
44
    public function startScript(Script $script)
45
    {
46
        $this->scriptStartTime = time();
47
        $this->cliMate->green()->out("<bold>Starting Execution of '" . $script->getName() . "'</bold> <dim>('" . $script->getPath() . "')</dim>\n");
48
    }
49
50
    /**
51
     * @param Script $script
52
     */
53
    public function finishScript(Script $script)
54
    {
55
        $durationInSeconds = time() - $this->scriptStartTime;
56
57
        if (!$durationInSeconds) {
58
            $durationInSeconds = 1;
59
        }
60
61
        $this->cliMate->green()->out("\n<bold>Duration: " . $this->duration->humanize($durationInSeconds) . "</bold>");
62
    }
63
64
    /**
65
     * @param string $response
66
     * @return string
67
     */
68
    private function formatOutput(string $response) :string
69
    {
70
        return str_replace(PHP_EOL, PHP_EOL . "\t", $response);
71
    }
72
73
    /**
74
     * @return void
75
     */
76
    public function logWait()
77
    {
78
        $this->cliMate->green()->bold()->inline("\nWAITING...\n\t");
79
    }
80
81
    public function log(LogMessage $logMessage)
82
    {
83
        if ($logMessage->isError()) {
84
            $this->err($logMessage->getMessage());
85
        } else {
86
            $this->out($logMessage->getMessage());
87
        }
88
    }
89
90
    /**
91
     * @param string $response
92
     */
93
    private function err(string $response)
94
    {
95
        $this->cliMate->red()->inline($this->formatOutput($response));
96
    }
97
98
    /**
99
     * @param string $response
100
     */
101
    private function out(string $response)
102
    {
103
        $this->cliMate->green()->inline($this->formatOutput($response));
104
    }
105
106
    /**
107
     * @param string $headline
108
     * @param string $subject
109
     * @param int $line
110
     * @param bool $isIgnoreError
111
     * @param int $index
112
     * @param int $max
113
     */
114
    public function logStart(string $headline, string $subject, int $line, bool $isIgnoreError, int $index, int $max)
115
    {
116
        $index++;
117
        $this->cliMate->yellow()->inline("\n({$index}/{$max}) $headline\n<bold>> {$subject}</bold>\n\t");
118
    }
119
120
    public function logSuccess()
121
    {
122
        $this->cliMate->green()->bold()->out('Executed Successfully');
123
    }
124
125
    public function LogFailure()
126
    {
127
        $this->cliMate->green()->red()->out('Executed with failure');
128
    }
129
}
130