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

ProgressJuggler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 5
eloc 11
c 2
b 0
f 0
dl 0
loc 44
ccs 15
cts 15
cp 1
rs 10

5 Methods

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