Completed
Push — develop ( eb616e...328010 )
by Alec
03:31
created

BenchmarkSymfonyProgressBar::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 4
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 1
rs 9.6666
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
8
class BenchmarkSymfonyProgressBar extends Benchmark
9
{
10
    public const DEFAULT_PROGRESSBAR_FORMAT = '[%bar%] %percent:3s%% %elapsed:6s%/%estimated:-6s%';
11
    public const PROGRESS_BAR_WIDTH = 80;
12
13
    /** @var ConsoleOutput */
14
    protected $output;
15
16
    /** @var ProgressBar */
17
    protected $progressBar;
18
19
    /** @var int */
20
    private $progressBarWidth;
21
22 1
    public function __construct(
23
        int $iterations = 1000,
24
        ?int $progressBarMax = null,
25
        ?int $progressBarWidth = null,
26
        ?ConsoleOutput $output = null
27
    ) {
28 1
        parent::__construct($iterations);
29 1
        $this->output = $output ?? new ConsoleOutput();
30 1
        $this->advanceSteps = $progressBarMax ?? $this->advanceSteps;
31 1
        $this->progressBarWidth = $progressBarWidth ?? self::PROGRESS_BAR_WIDTH;
32
33 1
        $this->progressBar = new ProgressBar($this->output, $this->advanceSteps);
34 1
        $this->progressBar->setBarWidth($this->progressBarWidth);
35
        $progressStart =
36
            function (): void {
37 1
                $this->progressBar->start();
38 1
            };
39
40
        $progressAdvance =
41
            function (): void {
42 1
                $this->progressBar->advance();
43 1
            };
44
45
        $progressFinish =
46
            function (): void {
47 1
                $this->progressBar->finish();
48 1
                $this->progressBar->clear();
49 1
            };
50
51 1
        $this->progressBar->setFormat(static::DEFAULT_PROGRESSBAR_FORMAT);
52 1
        $this->showProgressBy($progressStart, $progressAdvance, $progressFinish);
53 1
    }
54
55
    /**
56
     * @return ConsoleOutput
57
     */
58 1
    public function getOutput(): ConsoleOutput
59
    {
60 1
        return $this->output;
61
    }
62
63
    /**
64
     * @return ProgressBar
65
     */
66 1
    public function getProgressBar(): ProgressBar
67
    {
68 1
        return $this->progressBar;
69
    }
70
71
    /**
72
     * @return int
73
     */
74 1
    public function getProgressBarWidth(): int
75
    {
76 1
        return $this->progressBarWidth;
77
    }
78
}
79