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

ProgressFrameProcedure   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 13
c 2
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

3 Methods

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