Test Failed
Push — master ( 58858d...095b07 )
by Alec
03:13
created

FrameJuggler::assertFrames()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
ccs 7
cts 7
cp 1
rs 10
cc 3
nc 3
nop 1
crap 3
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
    /** @var string */
17 23
//    protected $spacer = Defaults::EMPTY_STRING;
18
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
19 23
20 18
    public function __construct(array $frames)
21 18
    {
22 18
        $this->assertFrames($frames);
23
        $this->frames = new Circular($frames);
24 23
        $this->erasingLength = Calculator::computeErasingLength($frames) + strlen($this->spacer);
25
    }
26 23
27 1
    /**
28 1
     * @param array $frames
29
     */
30
    protected function assertFrames(array $frames): void
31 22
    {
32 22
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
33 3
            throw new \InvalidArgumentException(
34
                sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
35 22
            );
36 1
        }
37 1
        foreach ($frames as $frame) {
38 1
            $this->assertFrame($frame);
39 1
        }
40 1
    }
41
42
    /** {@inheritDoc} */
43
    public function getFrame(): string
44
    {
45 18
        return (string)$this->frames->value() . $this->spacer;
46
    }
47
48
    /** {@inheritDoc} */
49
    public function getFrameErasingLength(): int
50 16
    {
51
        return $this->erasingLength;
52 16
    }
53
54
    /**
55 16
     * @param $frame
56
     */
57 16
    protected function assertFrame($frame): void
58
    {
59
        if (!\is_string($frame)) {
60
            throw new \InvalidArgumentException('All frames should be of string type.');
61
        }
62
        if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
63
            throw new \InvalidArgumentException(
64
                sprintf(
65
                    'Single frame max length [%s] exceeded [%s]',
66
                    Defaults::MAX_FRAME_LENGTH,
67
                    $length
68
                )
69
            );
70
        }
71
    }
72
}
73