Passed
Push — master ( 7b0af7...e2bdc6 )
by Alec
03:09
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\Settings\Contracts\Defaults;
8
9
class FrameJuggler extends AbstractJuggler
10
{
11
    /** @var Circular */
12
    protected $frames;
13
    /** @var string */
14
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
15
16 24
    public function __construct(array $frames, Circular $style = null)
17
    {
18 24
        $this->assertFrames($frames);
19 19
        $this->frames = new Circular($frames);
20 19
        $this->style = $style ?? new Circular(['%s',]);
21 19
        $this->currentFrameErasingLength = Calculator::computeErasingLength($frames) + strlen($this->spacer);
22 19
    }
23
24
    /**
25
     * @param array $frames
26
     */
27 24
    protected function assertFrames(array $frames): void
28
    {
29 24
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
30 1
            throw new \InvalidArgumentException(
31 1
                sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
32
            );
33
        }
34 23
        foreach ($frames as $frame) {
35 23
            $this->assertFrame($frame);
36
        }
37 19
    }
38
39
    /**
40
     * @param mixed $frame
41
     */
42 23
    protected function assertFrame($frame): void
43
    {
44 23
        if (!\is_string($frame)) {
45 3
            throw new \InvalidArgumentException('All frames should be of string type.');
46
        }
47 23
        if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
48 1
            throw new \InvalidArgumentException(
49 1
                sprintf(
50 1
                    'Single frame max length [%s] exceeded [%s]',
51 1
                    Defaults::MAX_FRAME_LENGTH,
52 1
                    $length
53
                )
54
            );
55
        }
56 23
    }
57
58
    /**
59
     * @return string
60
     */
61 18
    protected function getCurrentFrame(): string
62
    {
63 18
        return $this->prefix . $this->frames->value() . $this->suffix . $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

63
        return $this->prefix . /** @scrutinizer ignore-type */ $this->frames->value() . $this->suffix . $this->spacer;
Loading history...
64
    }
65
}
66