• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

eliashaeussler / cache-warmup / 12421831662

19 Dec 2024 10:29PM UTC coverage: 90.401% (-0.05%) from 90.453%
12421831662

push

github

web-flow
Merge pull request #426 from eliashaeussler/task/compact-progress

[TASK] Make CompactProgressHandler display a more compact progress

33 of 34 new or added lines in 1 file covered. (97.06%)

1 existing line in 1 file now uncovered.

1667 of 1844 relevant lines covered (90.4%)

9.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/src/Http/Message/Handler/CompactProgressHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
/*
6
 * This file is part of the Composer package "eliashaeussler/cache-warmup".
7
 *
8
 * Copyright (C) 2020-2024 Elias Häußler <elias@haeussler.dev>
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation, either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23

24
namespace EliasHaeussler\CacheWarmup\Http\Message\Handler;
25

26
use Psr\Http\Message;
27
use Symfony\Component\Console;
28
use Throwable;
29

30
use function min;
31
use function round;
32
use function sprintf;
33
use function str_pad;
34
use function str_repeat;
35
use function strlen;
36

37
/**
38
 * CompactProgressHandler.
39
 *
40
 * @author Elias Häußler <elias@haeussler.dev>
41
 * @license GPL-3.0-or-later
42
 */
43
final class CompactProgressHandler implements ResponseHandler
44
{
45
    private const MAX_LINE_LENGTH = 80;
46

47
    private readonly int $maxColumns;
48
    private int $columns = 0;
49
    private int $current = 0;
50

51
    public function __construct(
4✔
52
        private readonly Console\Output\OutputInterface $output,
53
        private readonly int $max,
54
    ) {
55
        $this->maxColumns = $this->calculateMaxColumns();
4✔
56
    }
57

58
    public function startProgressBar(): void
4✔
59
    {
60
        $this->reset();
4✔
61
    }
62

UNCOV
63
    public function finishProgressBar(): void
×
64
    {
NEW
65
        $this->reset();
×
66
    }
67

68
    public function onSuccess(Message\ResponseInterface $response, Message\UriInterface $uri): void
3✔
69
    {
70
        $this->advance('.');
3✔
71
    }
72

73
    public function onFailure(Throwable $exception, Message\UriInterface $uri): void
1✔
74
    {
75
        $this->advance('<error>F</error>');
1✔
76
    }
77

78
    private function advance(string $output): void
4✔
79
    {
80
        ++$this->columns;
4✔
81
        ++$this->current;
4✔
82

83
        if ($this->columns >= $this->maxColumns || $this->current === $this->max) {
4✔
84
            $this->output->writeln($output.$this->renderCurrentState());
2✔
85
            $this->columns = 0;
2✔
86
        } else {
87
            $this->output->write($output);
4✔
88
        }
89
    }
90

91
    private function renderCurrentState(): string
2✔
92
    {
93
        $percent = round(($this->current / $this->max) * 100);
2✔
94

95
        if ($this->max === $this->current) {
2✔
96
            $fill = str_repeat(' ', $this->maxColumns - $this->columns);
1✔
97
        } else {
98
            $fill = '';
2✔
99
        }
100

101
        return sprintf(
2✔
102
            '%s %s / %d (%s%%)',
2✔
103
            $fill,
2✔
104
            str_pad((string) $this->current, strlen((string) $this->max), ' ', STR_PAD_LEFT),
2✔
105
            $this->max,
2✔
106
            str_pad((string) $percent, 3, ' ', STR_PAD_LEFT),
2✔
107
        );
2✔
108
    }
109

110
    private function calculateMaxColumns(): int
4✔
111
    {
112
        // current / max (...%)
113
        $stateLength = 2 * strlen((string) $this->max) + 11;
4✔
114
        $lineLength = min(
4✔
115
            (new Console\Terminal())->getWidth(),
4✔
116
            self::MAX_LINE_LENGTH,
4✔
117
        );
4✔
118

119
        return $lineLength - $stateLength;
4✔
120
    }
121

122
    private function reset(): void
4✔
123
    {
124
        $this->columns = 0;
4✔
125
        $this->current = 0;
4✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc