Passed
Push — develop ( 566459...cdebde )
by Alec
03:08
created

BenchmarkSymfonyPB   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 25
dl 0
loc 65
ccs 0
cts 22
cp 0
rs 10
c 0
b 0
f 0
wmc 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 30 1
A getProgressBar() 0 3 1
A getOutput() 0 3 1
A getProgressBarWidth() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Tools;
6
7
use Symfony\Component\Console\Helper\ProgressBar;
8
use Symfony\Component\Console\Output\ConsoleOutput;
9
10
// BenchmarkSymfonyProgressBar
11
class BenchmarkSymfonyPB extends Benchmark
12
{
13
    protected const PROGRESS_BAR_WIDTH = 80;
14
15
    /** @var ConsoleOutput */
16
    protected $output;
17
    /** @var ProgressBar */
18
    protected $progressBar;
19
    /** @var int */
20
    private $progressBarWidth;
21
22
    public function __construct(
23
        int $iterations = 1000,
24
        ?int $progressBarMax = null,
25
        ?int $progressBarWidth = null,
26
        ?ConsoleOutput $output = null
27
    ) {
28
        parent::__construct($iterations);
29
        $this->output = $output ?? new ConsoleOutput();
30
        $this->advanceSteps = $progressBarMax ?? $this->advanceSteps;
31
        $this->progressBarWidth = $progressBarWidth ?? self::PROGRESS_BAR_WIDTH;
32
33
        $this->progressBar = new ProgressBar($this->output, $this->advanceSteps);
34
        $this->progressBar->setBarWidth($this->progressBarWidth);
35
        $progressStart =
36
            function (): void {
37
                $this->progressBar->start();
38
            };
39
40
        $progressAdvance =
41
            function (): void {
42
                $this->progressBar->advance();
43
            };
44
45
        $progressFinish =
46
            function (): void {
47
                $this->progressBar->finish();
48
                $this->progressBar->clear();
49
            };
50
51
        $this->progressBar($progressStart, $progressAdvance, $progressFinish);
52
    }
53
54
    /**
55
     * @return ConsoleOutput
56
     */
57
    public function getOutput(): ConsoleOutput
58
    {
59
        return $this->output;
60
    }
61
62
    /**
63
     * @return ProgressBar
64
     */
65
    public function getProgressBar(): ProgressBar
66
    {
67
        return $this->progressBar;
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getProgressBarWidth(): int
74
    {
75
        return $this->progressBarWidth;
76
    }
77
}
78