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

ProgressJuggler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 5
eloc 14
c 4
b 0
f 0
dl 0
loc 48
ccs 16
cts 16
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getStyledFrame() 0 4 1
A getFrameErasingLength() 0 5 1
A update() 0 5 1
A setProgress() 0 3 1
A __construct() 0 4 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