Completed
Push — master ( 9c3b37...e42026 )
by Alec
03:16
created

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