Passed
Push — master ( 4505d6...4505d6 )
by Alec
02:17
created

ProgressJuggler::getFrame()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core\Jugglers;
4
5
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface;
6
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
7
use function AlecRabbit\Helpers\bounds;
8
9
class ProgressJuggler implements JugglerInterface
10
{
11
    /** @var float */
12
    protected $progress;
13
    /** @var string */
14
    protected $percentSpacer = Defaults::ONE_SPACE_SYMBOL;
15
    /** @var string */
16
    protected $currentFrame;
17
18 8
    public function __construct(float $percent)
19
    {
20 8
        $this->update($percent);
21 8
    }
22
23 7
    public function setProgress(float $percent): void
24
    {
25 7
        $this->update($percent);
26 7
    }
27
28 8
    public function getFrame(): string
29
    {
30 8
        return $this->currentFrame;
31
    }
32
33 8
    public function getFrameErasingLength(): int
34
    {
35
        return
36 8
            strlen($this->currentFrame);
37
    }
38
39
    /**
40
     * @param float $percent
41
     */
42 8
    protected function update(float $percent): void
43
    {
44 8
        $this->progress = bounds($percent, 0, 1);
45 8
        $this->currentFrame = $this->percentSpacer . (int)($this->progress * 100) . '% ';
46 8
    }
47
}
48