Completed
Pull Request — master (#99)
by
unknown
01:41
created

ClimateLogger::logFailure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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