Passed
Push — master ( d08e35...139360 )
by Alec
02:47
created

FrameJuggler::getFrameErasingLength()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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 Circular */
15
    protected $style;
16
    /** @var int */
17
    protected $erasingLength;
18
    /** @var string */
19
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
20
21 24
    public function __construct(array $frames, Circular $style = null)
22
    {
23 24
        $this->assertFrames($frames);
24 19
        $this->frames = new Circular($frames);
25 19
        $this->style = $style ?? new Circular(['%s',]);
26 19
        $this->erasingLength = Calculator::computeErasingLength($frames) + strlen($this->spacer);
27 19
    }
28
29
    /**
30
     * @param array $frames
31
     */
32 24
    protected function assertFrames(array $frames): void
33
    {
34 24
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
35 1
            throw new \InvalidArgumentException(
36 1
                sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
37
            );
38
        }
39 23
        foreach ($frames as $frame) {
40 23
            $this->assertFrame($frame);
41
        }
42 19
    }
43
44
    /** {@inheritDoc} */
45 18
    public function getStyledFrame(): string
46
    {
47
        return
48 18
            sprintf((string)$this->style->value(), $this->frames->value() . $this->spacer);
0 ignored issues
show
Bug introduced by
Are you sure $this->frames->value() of type AlecRabbit\Accessories\Rewindable|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            sprintf((string)$this->style->value(), /** @scrutinizer ignore-type */ $this->frames->value() . $this->spacer);
Loading history...
49
    }
50
51
    /** {@inheritDoc} */
52 18
    public function getFrameErasingLength(): int
53
    {
54 18
        return $this->erasingLength;
55
    }
56
57
    /**
58
     * @param mixed $frame
59
     */
60 23
    protected function assertFrame($frame): void
61
    {
62 23
        if (!\is_string($frame)) {
63 3
            throw new \InvalidArgumentException('All frames should be of string type.');
64
        }
65 23
        if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
66 1
            throw new \InvalidArgumentException(
67 1
                sprintf(
68 1
                    'Single frame max length [%s] exceeded [%s]',
69 1
                    Defaults::MAX_FRAME_LENGTH,
70 1
                    $length
71
                )
72
            );
73
        }
74 23
    }
75
}
76