Passed
Push — master ( 823aee...b9b37f )
by Alec
13:15 queued 12s
created

AProgressValueProcedure   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 27
rs 10
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A update() 0 14 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Extras\Procedure\A;
6
7
use AlecRabbit\Spinner\Contract\IFrame;
8
use AlecRabbit\Spinner\Core\Factory\FrameFactory;
9
use AlecRabbit\Spinner\Extras\Contract\IProgressValue;
10
11
abstract class AProgressValueProcedure extends AFloatValueProcedure
12
{
13
    protected const FORMAT = "%' 3.0f%%"; // "%' 5.1f%%";
14
    protected const FINISHED_DELAY = 500;
15
16
    public function __construct(
17
        protected readonly IProgressValue $progressValue,
18
        string $format = null,
19
        protected float $finishedDelay = self::FINISHED_DELAY,
20
    ) {
21
        parent::__construct($progressValue, $format);
22
    }
23
24
    public function update(float $dt = null): IFrame
25
    {
26
        if ($this->progressValue->isFinished()) {
27
            if ($this->finishedDelay < 0) {
28
                return FrameFactory::createEmpty();
29
            }
30
            $this->finishedDelay -= $dt ?? 0.0;
31
        }
32
        $v = sprintf(
33
            $this->format,
34
            $this->progressValue->getValue() * 100
35
        );
36
        return
37
            FrameFactory::create($v);
38
    }
39
}
40