Passed
Push — master ( d08e35...139360 )
by Alec
02:47
created

ProgressJuggler::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 3
c 0
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core\Jugglers;
4
5
use AlecRabbit\Accessories\Circular;
6
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface;
7
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
8
use function AlecRabbit\Helpers\bounds;
9
10
class ProgressJuggler implements JugglerInterface
11
{
12
    /** @var string */
13
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
14
    /** @var string */
15
    protected $currentFrame;
16
    /** @var int */
17
    protected $currentFrameErasingLength;
18
    /** @var Circular */
19
    protected $style;
20
21 11
    public function __construct(float $percent, Circular $style = null)
22
    {
23 11
        $this->style = $style ?? new Circular(['%s',]);
24 11
        $this->update($percent);
25 11
    }
26
27
    /**
28
     * @param float $percent
29
     */
30 11
    protected function update(float $percent): void
31
    {
32 11
        $progress = bounds($percent, 0, 1);
33 11
        $this->currentFrame = (int)($progress * 100) . '%' . $this->spacer;
34 11
        $this->currentFrameErasingLength = strlen($this->currentFrame);
35 11
    }
36
37
    /**
38
     * @param float $percent
39
     */
40 10
    public function setProgress(float $percent): void
41
    {
42 10
        $this->update($percent);
43 10
    }
44
45
    /** {@inheritDoc} */
46 11
    public function getStyledFrame(): string
47
    {
48
        return
49 11
            sprintf((string)$this->style->value(), $this->currentFrame);
50
    }
51
52
    /** {@inheritDoc} */
53 11
    public function getFrameErasingLength(): int
54
    {
55
        return
56
57 11
            $this->currentFrameErasingLength;
58
    }
59
}
60