Completed
Push — master ( 9d2605...9c3b37 )
by Alec
02:41
created

BenchmarkSymfonyProgressBar   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 94
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 94
ccs 28
cts 30
cp 0.9333
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 33 1
A getOutput() 0 3 1
A refineProgressBarWidth() 0 7 1
A getProgressBar() 0 3 1
A showComment() 0 6 1
A getProgressBarWidth() 0 3 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 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
16
    /** @var ConsoleOutput */
17
    protected $output;
18
19
    /** @var ProgressBar */
20
    protected $progressBar;
21
22
    /** @var int */
23
    private $progressBarWidth;
24
25 1
    public function __construct(
26
        int $iterations = 1000,
27
        ?int $progressBarMax = null,
28
        ?int $progressBarWidth = null,
29
        ?ConsoleOutput $output = null
30
    ) {
31 1
        parent::__construct($iterations);
32 1
        $this->output = $output ?? new ConsoleOutput();
33 1
        $this->advanceSteps = $progressBarMax ?? $this->advanceSteps;
34
35
36 1
        $this->progressBar = new ProgressBar($this->output, $this->advanceSteps);
37 1
        $this->progressBarWidth = $this->refineProgressBarWidth($progressBarWidth);
38 1
        $this->progressBar->setBarWidth($this->progressBarWidth);
39 1
        $this->progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT);
40
41
        $progressStart =
42
            function (): void {
43 1
                $this->progressBar->start();
44 1
            };
45
46
        $progressAdvance =
47
            function (): void {
48 1
                $this->progressBar->advance();
49 1
            };
50
51
        $progressFinish =
52
            function (): void {
53 1
                $this->progressBar->finish();
54 1
                $this->progressBar->clear();
55 1
            };
56
57 1
        $this->showProgressBy($progressStart, $progressAdvance, $progressFinish);
58 1
    }
59
60
    /**
61
     * @param null|int $progressBarWidth
62
     * @return int
63
     */
64 1
    protected function refineProgressBarWidth(?int $progressBarWidth): int
65
    {
66
        return
67 1
            (int)bounds(
68 1
                $progressBarWidth ?? (int)((new Terminal())->getWidth() * 0.8),
69 1
                static::PROGRESS_BAR_MIN_WIDTH,
70 1
                static::PROGRESS_BAR_MAX_WIDTH
71
            );
72
    }
73
74
    /**
75
     * @return ConsoleOutput
76
     */
77 1
    public function getOutput(): ConsoleOutput
78
    {
79 1
        return $this->output;
80
    }
81
82
    /**
83
     * @return ProgressBar
84
     */
85 1
    public function getProgressBar(): ProgressBar
86
    {
87 1
        return $this->progressBar;
88
    }
89
90
    /**
91
     * @return int
92
     */
93 1
    public function getProgressBarWidth(): int
94
    {
95 1
        return $this->progressBarWidth;
96
    }
97
98
    protected function showComment(string $comment = ''): void
99
    {
100
//        $outputStyle = new OutputFormatterStyle('red', 'yellow', ['bold', 'blink']);
101
//        $this->output->getFormatter()->setStyle('fire', $outputStyle);
102
//
103
        $this->output->writeln('<comment>' . $comment . '</>');
104
    }
105
}
106