FrameCollectionRevolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Spinner\Core\Revolver;
6
7
use AlecRabbit\Spinner\Contract\IFrame;
8
use AlecRabbit\Spinner\Contract\IInterval;
9
use AlecRabbit\Spinner\Core\Contract\IFrameCollection;
10
use AlecRabbit\Spinner\Core\Contract\ITolerance;
11
use AlecRabbit\Spinner\Core\Revolver\A\ARevolver;
12
use AlecRabbit\Spinner\Core\Revolver\Contract\IFrameCollectionRevolver;
13
use AlecRabbit\Spinner\Exception\InvalidArgument;
14
15
final class FrameCollectionRevolver extends ARevolver implements IFrameCollectionRevolver
16
{
17
    protected int $count = 0;
18
    protected int $offset = 0;
19
20
    /**
21
     * @throws InvalidArgument
22
     */
23
    public function __construct(
24
        protected IFrameCollection $frameCollection,
25
        IInterval $interval,
26
        ITolerance $tolerance,
27
    ) {
28
        parent::__construct($interval, $tolerance);
29
        $this->count = $this->frameCollection->count();
30
        $this->assertIsNotEmpty();
31
    }
32
33
    /**
34
     * @throws InvalidArgument
35
     */
36
    protected function assertIsNotEmpty(): void
37
    {
38
        if ($this->count === 0) {
39
            throw new InvalidArgument('Frame collection is empty.');
40
        }
41
    }
42
43
    protected function next(?float $dt = null): void
44
    {
45
        if ($this->count === 1 || ++$this->offset === $this->count) {
46
            $this->offset = 0;
47
        }
48
    }
49
50
    protected function current(): IFrame
51
    {
52
        return $this->frameCollection->get($this->offset);
53
    }
54
}
55