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

ProgressFrameProcedure::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Extras\Procedure;
6
7
use AlecRabbit\Spinner\Contract\IFrame;
8
use AlecRabbit\Spinner\Contract\IFrameCollection;
9
use AlecRabbit\Spinner\Core\Factory\FrameFactory;
10
use AlecRabbit\Spinner\Extras\Contract\IProgressValue;
11
use AlecRabbit\Spinner\Extras\Procedure\A\AProgressValueProcedure;
12
13
/** @psalm-suppress UnusedClass */
14
final class ProgressFrameProcedure extends AProgressValueProcedure
15
{
16
    private int $steps;
17
18
    public function __construct(
19
        IProgressValue $progressValue,
20
        protected IFrameCollection $frames,
21
    ) {
22
        parent::__construct($progressValue);
23
        $this->steps = $frames->lastIndex();
24
    }
25
26
    public function update(float $dt = null): IFrame
27
    {
28
        if ($this->progressValue->isFinished()) {
29
            if ($this->finishedDelay < 0) {
30
                return FrameFactory::createEmpty();
31
            }
32
            $this->finishedDelay -= $dt ?? 0.0;
33
        }
34
        return
35
            $this->getFrame($this->floatValue->getValue());
36
    }
37
38
    private function getFrame(float $progress): IFrame
39
    {
40
        $index = (int)($progress * $this->steps);
41
        return
42
            $this->frames->get($index);
43
    }
44
}
45