Passed
Push — master ( 4505d6...4505d6 )
by Alec
02:17
created

FrameJuggler::assertFrames()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 17
ccs 13
cts 13
cp 1
rs 9.5555
cc 5
nc 5
nop 1
crap 5
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core\Jugglers;
4
5
use AlecRabbit\Accessories\Circular;
6
use AlecRabbit\Spinner\Core\Calculator;
7
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface;
8
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
9
10
class FrameJuggler implements JugglerInterface
11
{
12
    /** @var Circular */
13
    protected $frames;
14
    /** @var int */
15
    protected $erasingLength;
16
17 23
    public function __construct(array $frames)
18
    {
19 23
        $this->assertFrames($frames);
20 18
        $this->frames = new Circular($frames);
21 18
        $this->erasingLength = Calculator::computeErasingLength($frames);
22 18
    }
23
24 23
    protected function assertFrames(array $frames): void
25
    {
26 23
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
27 1
            throw new \InvalidArgumentException(
28 1
                sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
29
            );
30
        }
31 22
        foreach ($frames as $frame) {
32 22
            if (!\is_string($frame)) {
33 3
                throw new \InvalidArgumentException('All frames should be of string type.');
34
            }
35 22
            if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
36 1
                throw new \InvalidArgumentException(
37 1
                    sprintf(
38 1
                        'Single frame max length [%s] exceeded [%s]',
39 1
                        Defaults::MAX_FRAME_LENGTH,
40 1
                        $length
41
                    )
42
                );
43
            }
44
        }
45 18
    }
46
47
    /**
48
     * @return string
49
     */
50 16
    public function getFrame(): string
51
    {
52 16
        return $this->frames->value();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->frames->value() could return the type AlecRabbit\Accessories\Rewindable which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
53
    }
54
55 16
    public function getFrameErasingLength(): int
56
    {
57 16
        return $this->erasingLength;
58
    }
59
}
60