Passed
Push — master ( 095b07...d08e35 )
by Alec
02:42
created

ProgressJuggler::getFrameErasingLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 5
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 string */
12
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
13
    /** @var string */
14
    protected $currentFrame;
15
    /** @var int */
16
    protected $currentFrameErasingLength;
17
18 11
    public function __construct(float $percent)
19
    {
20 11
        $this->update($percent);
21 11
    }
22
23
    /**
24
     * @param float $percent
25
     */
26 11
    protected function update(float $percent): void
27
    {
28 11
        $progress = bounds($percent, 0, 1);
29 11
        $this->currentFrame = (int)($progress * 100) . '%' . $this->spacer;
30 11
        $this->currentFrameErasingLength = strlen($this->currentFrame);
31 11
    }
32
33
    /**
34
     * @param float $percent
35
     */
36 10
    public function setProgress(float $percent): void
37
    {
38 10
        $this->update($percent);
39 10
    }
40
41
    /** {@inheritDoc} */
42 11
    public function getFrame(): string
43
    {
44 11
        return $this->currentFrame;
45
    }
46
47
    /** {@inheritDoc} */
48 11
    public function getFrameErasingLength(): int
49
    {
50
        return
51
52 11
            $this->currentFrameErasingLength;
53
    }
54
}
55