Completed
Push — master ( 644e5f...9a3147 )
by Webysther
02:09
created

ProgressBar::progress()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 15
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 7
nc 3
nop 1
dl 15
loc 15
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Packagist Mirror.
7
 *
8
 * For the full license information, please view the LICENSE.md
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webs\Mirror;
13
14
use Dariuszp\CliProgressBar;
15
16
/**
17
 * Progress bar for console.
18
 *
19
 * @author Webysther Nunes <[email protected]>
20
 */
21
class ProgressBar implements IProgressBar
22
{
23
    use Console;
24
25
    /**
26
     * @var bool
27
     */
28
    protected $disabled;
29
30
    /**
31
     * @var CliProgressBar
32
     */
33
    protected $progressBar;
34
35
    /**s
36
     * @var int
37
     */
38
    protected $total;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function isDisabled():bool
44
    {
45
        if (isset($this->disabled)) {
46
            return $this->disabled;
47
        }
48
49
        $isQuiet = $this->output->isQuiet();
50
        $noProgress = $this->input->getOption('no-progress');
51
        $noAnsi = $this->input->getOption('no-ansi');
52
53
        $this->disabled = $isQuiet || $noProgress || $noAnsi;
54
55
        return $this->disabled;
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function start(int $total):IProgressBar
62
    {
63
        if ($this->isDisabled()) {
64
            return $this;
65
        }
66
67
        $this->total = $total;
68
        $this->progressBar = new CliProgressBar($total, 0);
69
70
        return $this;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76 View Code Duplication
    public function progress(int $current = 0):IProgressBar
77
    {
78
        if ($this->isDisabled()) {
79
            return $this;
80
        }
81
82
        if ($current) {
83
            $this->progressBar->progress($current);
84
85
            return $this;
86
        }
87
88
        $this->progressBar->progress();
89
90
        return $this;
91
    }
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 View Code Duplication
    public function end():IProgressBar
97
    {
98
        if ($this->isDisabled()) {
99
            return $this;
100
        }
101
102
        $this->progressBar->progress($this->total);
103
        $this->progressBar->end();
104
105
        return $this;
106
    }
107
}
108