Completed
Push — master ( 03739e...51f188 )
by Michał
14:48 queued 08:49
created

AdditionalStyles::getDateTimeFormat()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BlueConsole;
6
7
use Symfony\Component\Console\Helper\FormatterHelper;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
trait AdditionalStyles
12
{
13
    /**
14
     * @var int
15
     */
16
    protected $align = 12;
17
18
    /**
19
     * @var int
20
     */
21
    protected $timeCharLength = 14;
22
23
    /**
24
     * @var int|float
25
     */
26
    protected $time;
27
28
    /**
29
     * @var bool
30
     */
31
    protected $showTimer = false;
32
33
    /**
34
     * @var bool
35
     */
36
    protected $timerTypeDateTime = false;
37
38
    /**
39
     * @var FormatterHelper
40
     */
41
    protected $formatter;
42
43
    /**
44
     * @var string
45
     */
46
    protected $dateTimeFormat = 'c';
47
48
    /**
49
     * Style constructor.
50
     *
51
     * @param InputInterface $input
52
     * @param OutputInterface $output
53
     * @param FormatterHelper $formatter
54
     */
55 140
    public function __construct(InputInterface $input, OutputInterface $output, FormatterHelper $formatter)
56
    {
57 140
        $this->formatter = $formatter;
58
59 140
        parent::__construct($input, $output);
60
61 140
        $this->startTime();
62 140
    }
63
64
    /**
65
     * @param string $message
66
     * @param int $length
67
     * @param string $suffix
68
     * @return Style
69
     */
70 8
    public function truncate(string $message, int $length, string $suffix = '...'): self
71
    {
72 8
        $this->write($this->truncateMessage($message, $length, $suffix));
0 ignored issues
show
Bug introduced by
It seems like write() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

72
        $this->/** @scrutinizer ignore-call */ 
73
               write($this->truncateMessage($message, $length, $suffix));
Loading history...
73
74 8
        return $this;
75
    }
76
77
    /**
78
     * @param string $message
79
     * @param int $length
80
     * @param string $suffix
81
     * @return Style
82
     */
83 4
    public function truncateLn(string $message, int $length, string $suffix = '...'): self
84
    {
85 4
        $this->writeln($this->truncateMessage($message, $length, $suffix));
0 ignored issues
show
Bug introduced by
It seems like writeln() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
        $this->/** @scrutinizer ignore-call */ 
86
               writeln($this->truncateMessage($message, $length, $suffix));
Loading history...
86
87 4
        return $this;
88
    }
89
90
    /**
91
     * @param string $message
92
     * @param int $length
93
     * @param string $suffix
94
     * @return string
95
     */
96 16
    public function truncateMessage(string $message, int $length, string $suffix = '...'): string
97
    {
98 16
        return $this->formatter->truncate($message, $length, $suffix);
99
    }
100
101
    /**
102
     * Turn o/off show timer after info block
103
     */
104 36
    public function toggleShowTimer(): void
105
    {
106 36
        $this->showTimer = !$this->showTimer;
107 36
    }
108
109
    /**
110
     * Turn o/off show timer after info block
111
     */
112 16
    public function toggleTimerType(): void
113
    {
114 16
        $this->timerTypeDateTime = !$this->timerTypeDateTime;
115 16
    }
116
117
    /**
118
     * @return bool
119
     */
120 4
    public function isTimerOn(): bool
121
    {
122 4
        return $this->showTimer;
123
    }
124
125
    /**
126
     * @param int|null $time
127
     */
128 140
    public function startTime(?int $time = null): void
129
    {
130 140
        if ($time === null) {
131
            /** @noinspection CallableParameterUseCaseInTypeContextInspection */
132 140
            $time = \microtime(true);
133
        }
134
135 140
        $this->time = $time;
136 140
    }
137
138
    /**
139
     * @param bool $checkEnableForBlock
140
     * @return string|null
141
     * @throws \Exception
142
     */
143 92
    protected function getTimer(bool $checkEnableForBlock = false): ?string
144
    {
145 92
        if ($checkEnableForBlock && !$this->showTimer) {
146 56
            return null;
147
        }
148
149 36
        if ($this->timerTypeDateTime) {
150 16
            return $this->getTimerDateTime();
151
        }
152
153 20
        return $this->getTimerDefault();
154
    }
155
156
    /**
157
     * @return string
158
     * @throws \Exception
159
     */
160 16
    protected function getTimerDateTime(): string
161
    {
162 16
        $dateTime = (new \DateTime())->format($this->dateTimeFormat);
163
164 16
        return "[ <options=bold>$dateTime</> ]";
165
    }
166
167
    /**
168
     * @return string
169
     */
170 4
    public function getDateTimeFormat(): string
171
    {
172 4
        return $this->dateTimeFormat;
173
    }
174
175
    /**
176
     * @param string $format
177
     * @return void
178
     */
179 4
    public function setDateTimeFormat(string $format): void
180
    {
181 4
        $this->dateTimeFormat = $format;
182 4
    }
183
184
    /**
185
     * @return string
186
     */
187 20
    protected function getTimerDefault(): string
188
    {
189 20
        $days = null;
190 20
        $current = \microtime(true);
191 20
        $calc = $current - $this->time;
192
193 20
        if ($calc > Style::DAY_SECONDS) {
194
            $days = \round($calc / Style::DAY_SECONDS);
195
            $calc -= Style::DAY_SECONDS * $days;
196
            $days .= 'd ';
197
198
            $this->timeCharLength -= \strlen($days);
199
        }
200
201 20
        $formatted = \sprintf("%0{$this->timeCharLength}.4f", $calc);
202
203 20
        return "[ <options=bold>$days$formatted</> ]";
204
    }
205
206
    /**
207
     * @param int $align
208
     * @return $this
209
     */
210 4
    public function setAlign(int $align): self
211
    {
212 4
        $this->align = $align;
213
214 4
        return $this;
215
    }
216
217
    /**
218
     * @return int
219
     */
220 4
    public function getAlign(): int
221
    {
222 4
        return $this->align;
223
    }
224
225
    /**
226
     * @param int $align
227
     * @return $this
228
     */
229 4
    public function setTimeCharLength(int $align): self
230
    {
231 4
        $this->timeCharLength = $align;
232
233 4
        return $this;
234
    }
235
236
    /**
237
     * @return int
238
     */
239 4
    public function getTimeCharLength(): int
240
    {
241 4
        return $this->timeCharLength;
242
    }
243
244
    /**
245
     * add some spaces after string depends of it length
246
     * $string can be numeric/string/array, depends of type it work different
247
     * if its int inform about string length
248
     * if its string or object (__toString) it calculate string length for alignment
249
     * if its array, sum all strings in array length nad add +1for delimiter
250
     *
251
     * @param int|string|array|object $string
252
     * @param int $align
253
     * @return string
254
     * @noinspection MissingParameterTypeDeclarationInspection
255
     */
256 88
    public function align($string, int $align): string
257
    {
258 88
        $strLength = 0;
259
260
        switch (true) {
261 88
            case \is_int($string):
262 76
                $strLength = $string;
263 76
                break;
264
265 36
            case \is_string($string) || \is_object($string):
266 28
                $strLength = \mb_strlen((string)$string);
267 28
                break;
268
269 8
            case \is_array($string):
270 4
                foreach ($string as $message) {
271 4
                    $strLength += \mb_strlen($message) + 1;
272
                }
273 4
                break;
274
275
            default:
276 4
                break;
277
        }
278
279 88
        $newAlign = ' ';
280 88
        $spaces = $align - $strLength;
281
282 88
        for ($i = 1; $i <= $spaces; $i++) {
283 88
            $newAlign .= ' ';
284
        }
285
286 88
        return $newAlign;
287
    }
288
}
289