Completed
Push — master ( 9d2c7e...85264a )
by Jan Philipp
11s
created

ClimateLogger::finishScript()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
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
10
/**
11
 * A CLImate implementation of the runtime logger
12
 */
13
class ClimateLogger implements Logger
14
{
15
    /**
16
     * @var CLImate
17
     */
18
    private $cliMate;
19
20
    /**
21
     * @var \DateTime
22
     */
23
    private $scriptStartTime;
24
25
    /**
26
     * @var int
27
     */
28
    private $duration;
29
30
    /**
31
     * @param CLImate $cliMate
32
     * @param Duration $duration
33
     */
34
    public function __construct(CLImate $cliMate, Duration $duration)
35
    {
36
        $this->cliMate = $cliMate;
37
        $this->duration = $duration;
0 ignored issues
show
Documentation Bug introduced by
It seems like $duration of type object<Khill\Duration\Duration> is incompatible with the declared type integer of property $duration.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
38
    }
39
40
    /**
41
     * @param Script $script
42
     */
43
    public function startScript(Script $script)
44
    {
45
        $this->scriptStartTime = time();
0 ignored issues
show
Documentation Bug introduced by
It seems like time() of type integer is incompatible with the declared type object<DateTime> of property $scriptStartTime.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
46
        $this->cliMate->green()->out("<bold>Starting Execution of '" . $script->getName() . "'</bold> <dim>('" . $script->getPath() . "')</dim>\n");
47
    }
48
49
    /**
50
     * @param Script $script
51
     */
52
    public function finishScript(Script $script)
53
    {
54
        $durationInSeconds = time() - $this->scriptStartTime;
55
56
        if (!$durationInSeconds) {
57
            $durationInSeconds = 1;
58
        }
59
60
        $this->cliMate->green()->out("\n<bold>Duration: " . $this->duration->humanize($durationInSeconds) . "</bold>");
0 ignored issues
show
Bug introduced by
The method humanize cannot be called on $this->duration (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
61
    }
62
63
    /**
64
     * @param string $shellCommand
65
     * @param int $line
66
     * @param bool $isIgnoreError
67
     * @param int $index
68
     * @param int $max
69
     */
70
    public function logCommandStart(string $shellCommand, int $line, bool $isIgnoreError, int $index, int $max)
71
    {
72
        $index++;
73
        $this->cliMate->yellow()->inline("\n({$index}/{$max}) Starting\n<bold>> {$shellCommand}</bold>\n\t");
74
    }
75
76
77
    /**
78
     * @param string $destination
79
     * @param int $line
80
     * @param int $index
81
     * @param int $max
82
     */
83
    public function logTemplate(string $destination, int $line, int $index, int $max)
84
    {
85
        $index++;
86
        $this->cliMate->yellow()->inline("\n({$index}/{$max}) Rendering\n<bold>> {$destination}</bold>\n\t");
87
    }
88
89
    /**
90
     * @param string $response
91
     */
92
    public function err(string $response)
93
    {
94
        $this->cliMate->red()->inline($this->formatOutput($response));
95
    }
96
97
    /**
98
     * @param string $response
99
     */
100
    public function out(string $response)
101
    {
102
        $this->cliMate->green()->inline($this->formatOutput($response));
103
    }
104
105
    /**
106
     * @param string $response
107
     * @return string
108
     */
109
    private function formatOutput(string $response) :string
110
    {
111
        return str_replace(PHP_EOL, PHP_EOL . "\t", $response);
112
    }
113
}
114