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

eliashaeussler / cache-warmup / 12421663382

19 Dec 2024 10:16PM UTC coverage: 89.837% (-0.6%) from 90.453%
12421663382

Pull #426

github

web-flow
Merge d280a78db into f6b3e2878
Pull Request #426: [TASK] Make CompactProgressHandler display a more compact progress

19 of 30 new or added lines in 1 file covered. (63.33%)

1 existing line in 1 file now uncovered.

1653 of 1840 relevant lines covered (89.84%)

9.94 hits per line

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

65.71
/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 strlen;
35

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

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

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

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

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

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

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

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

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

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

NEW
94
        return sprintf(
×
NEW
95
            ' %s / %d (%s%%)',
×
NEW
96
            str_pad((string) $this->current, strlen((string) $this->max), ' ', STR_PAD_LEFT),
×
NEW
97
            $this->max,
×
NEW
98
            str_pad((string) $percent, 3, ' ', STR_PAD_LEFT),
×
NEW
99
        );
×
100
    }
101

102
    private function calculateMaxColumns(): int
2✔
103
    {
104
        // current / max (...%)
105
        $stateLength = 2 * strlen((string) $this->max) + 11;
2✔
106
        $lineLength = min(
2✔
107
            (new Console\Terminal())->getWidth(),
2✔
108
            self::MAX_LINE_LENGTH,
2✔
109
        );
2✔
110

111
        return $lineLength - $stateLength;
2✔
112
    }
113

114
    private function reset(): void
2✔
115
    {
116
        $this->columns = 0;
2✔
117
        $this->current = 0;
2✔
118
    }
119
}
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

© 2026 Coveralls, Inc