Test Failed
Push — master ( 77d86a...4c6a99 )
by Alec
03:20
created

BenchmarkSymfonyProgressBar   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 77.78%

Importance

Changes 0
Metric Value
eloc 40
dl 0
loc 106
ccs 28
cts 36
cp 0.7778
rs 10
c 0
b 0
f 0
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 29 1
A getOutput() 0 3 1
A refineProgressBarWidth() 0 7 1
A getProgressBar() 0 3 1
A sectionSeparator() 0 7 1
A showComment() 0 3 1
A createProgressBar() 0 16 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Tools;
4
5
use Symfony\Component\Console\Helper\ProgressBar;
6
use Symfony\Component\Console\Output\ConsoleOutput;
7
use function AlecRabbit\Helpers\bounds;
8
9
class BenchmarkSymfonyProgressBar extends Benchmark
10
{
11
    public const DEFAULT_PROGRESSBAR_FORMAT = ' %percent:3s%% [%bar%] %elapsed:6s%/%estimated:-6s%';
12
    public const PROGRESS_BAR_MIN_WIDTH = 60;
13
    public const PROGRESS_BAR_MAX_WIDTH = 80;
14
15
    /** @var ConsoleOutput */
16
    protected $output;
17
18
    /** @var ProgressBar */
19
    protected $progressBar;
20
21
    public function __construct(
22
        int $iterations = 1000,
23
        ?int $progressBarMax = null,
24
        ?ConsoleOutput $output = null,
25 1
        ?ProgressBar $progressBar = null
26
    ) {
27
        parent::__construct($iterations);
28
        $this->output = $output ?? new ConsoleOutput();
29
        $this->advanceSteps = $progressBarMax ?? $this->advanceSteps;
30
31 1
        $this->progressBar = $progressBar ?? $this->createProgressBar();
32 1
33 1
        $progressStart =
34
            function (): void {
35 1
                $this->progressBar->start();
36 1
            };
37 1
38 1
        $progressAdvance =
39
            function (): void {
40
                $this->progressBar->advance();
41
            };
42 1
43 1
        $progressFinish =
44
            function (): void {
45
                $this->progressBar->finish();
46
                $this->progressBar->clear();
47 1
            };
48 1
49
        $this->showProgressBy($progressStart, $progressAdvance, $progressFinish);
50
    }
51
52 1
    /**
53 1
     * @return ProgressBar
54 1
     */
55
    protected function createProgressBar(): ProgressBar
56 1
    {
57 1
        $progressBar = new ProgressBar($this->output, $this->advanceSteps);
58
        $progressBar->setBarWidth($this->terminalWidth());
59
        $progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT);
60
61
        // the finished part of the bar
62
        $progressBar->setBarCharacter('█');
63 1
64
        // the unfinished part of the bar
65
        $progressBar->setEmptyBarCharacter('░'); // ░ ▒ ▓
66 1
67 1
        // the progress character
68 1
        $progressBar->setProgressCharacter('');
69 1
70
        return $progressBar;
71
    }
72
73
    /**
74
     * @param null|int $progressBarWidth
75
     * @return int
76 1
     */
77
    protected function refineProgressBarWidth(?int $progressBarWidth): int
78 1
    {
79
        return
80
            (int)bounds(
81
                $progressBarWidth ?? $this->terminalWidth,
82
                static::PROGRESS_BAR_MIN_WIDTH,
83
                static::PROGRESS_BAR_MAX_WIDTH
84 1
            );
85
    }
86 1
87
    /**
88
     * @return ConsoleOutput
89
     */
90
    public function getOutput(): ConsoleOutput
91
    {
92 1
        return $this->output;
93
    }
94 1
95
    /**
96
     * @return ProgressBar
97
     */
98
    public function getProgressBar(): ProgressBar
99
    {
100
        return $this->progressBar;
101
    }
102
103
    protected function showComment(string $comment = ''): void
104
    {
105
        $this->output->writeln('<comment>' . $comment . '</>');
106
    }
107
108
    protected function sectionSeparator(?string $char): string
109
    {
110
        return
111
            ' ' . str_repeat(
112
                $char ?? static::DEFAULT_SEPARATOR_CHAR,
113
                $this->terminalWidth - 2
114
            ) . ' ' . PHP_EOL . PHP_EOL;
115
    }
116
}
117