Completed
Push — develop ( f5c859...666980 )
by Alec
02:44
created

BenchmarkSymfonyProgressBar::getProgressBarWidth()   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
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 1
    public function __construct(
22
        int $iterations = 1000,
23
        ?int $progressBarMax = null,
24
        ?ConsoleOutput $output = null,
25
        ?ProgressBar $progressBar = null
26
    ) {
27 1
        parent::__construct($iterations);
28 1
        $this->output = $output ?? new ConsoleOutput();
29 1
        $this->advanceSteps = $progressBarMax ?? $this->advanceSteps;
30
31 1
        $this->progressBar = $progressBar ?? $this->createProgressBar();
32
33
        $progressStart =
34
            function (): void {
35 1
                $this->progressBar->start();
36 1
            };
37
38
        $progressAdvance =
39
            function (): void {
40 1
                $this->progressBar->advance();
41 1
            };
42
43
        $progressFinish =
44
            function (): void {
45 1
                $this->progressBar->finish();
46 1
                $this->progressBar->clear();
47 1
            };
48
49 1
        $this->showProgressBy($progressStart, $progressAdvance, $progressFinish);
50 1
    }
51
52
    /**
53
     * @return ProgressBar
54
     */
55 1
    protected function createProgressBar(): ProgressBar
56
    {
57 1
        $progressBar = new ProgressBar($this->output, $this->advanceSteps);
58 1
        $progressBar->setBarWidth($this->terminalWidth());
59 1
        $progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT);
60
61
        // the finished part of the bar
62 1
        $progressBar->setBarCharacter('█');
63
64
        // the unfinished part of the bar
65 1
        $progressBar->setEmptyBarCharacter('░'); // ░ ▒ ▓
66
67
        // the progress character
68 1
        $progressBar->setProgressCharacter('');
69
70 1
        return $progressBar;
71
    }
72
73
    /**
74
     * @param null|int $progressBarWidth
75
     * @return int
76
     */
77
    protected function refineProgressBarWidth(?int $progressBarWidth): int
78
    {
79
        return
80
            (int)bounds(
81
                $progressBarWidth ?? $this->terminalWidth,
82
                static::PROGRESS_BAR_MIN_WIDTH,
83
                static::PROGRESS_BAR_MAX_WIDTH
84
            );
85
    }
86
87
    /**
88
     * @return ConsoleOutput
89
     */
90 1
    public function getOutput(): ConsoleOutput
91
    {
92 1
        return $this->output;
93
    }
94
95
    /**
96
     * @return ProgressBar
97
     */
98 1
    public function getProgressBar(): ProgressBar
99
    {
100 1
        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