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

AFrameCollectionRevolver::offsetGet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

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