Passed
Push — master ( 80875d...eb7214 )
by Alec
02:41
created

FrameJuggler   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 70.37%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 22
dl 0
loc 55
ccs 19
cts 27
cp 0.7037
rs 10
c 2
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A assertFrames() 0 9 3
A assertFrame() 0 11 3
A __construct() 0 8 1
A getCurrentFrame() 0 3 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\Coloring\Style;
8
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
9
use AlecRabbit\Spinner\Settings\Settings;
10
11
class FrameJuggler extends AbstractJuggler
12
{
13
    /** @var Circular */
14
    protected $frames;
15
16 19
    public function __construct(Settings $settings, Style $style)
17
    {
18 19
        $frames = $settings->getFrames();
19 19
        $this->assertFrames($frames);
20 19
        $this->frames = new Circular($frames);
21 19
        $this->init($style);
22 19
        $this->currentFrameErasingLength =
23 19
            Calculator::computeErasingLength($frames) + strlen($this->spacer) + $this->formatErasingShift;
24 19
    }
25
26
    /**
27
     * @param array $frames
28
     */
29 19
    protected function assertFrames(array $frames): void
30
    {
31 19
        if (Defaults::MAX_FRAMES_COUNT < $count = count($frames)) {
32
            throw new \InvalidArgumentException(
33
                sprintf('Frames count limit [%s] exceeded: [%s].', Defaults::MAX_FRAMES_COUNT, $count)
34
            );
35
        }
36 19
        foreach ($frames as $frame) {
37 19
            $this->assertFrame($frame);
38
        }
39 19
    }
40
41
    /**
42
     * @param mixed $frame
43
     */
44 19
    protected function assertFrame($frame): void
45
    {
46 19
        if (!\is_string($frame)) {
47
            throw new \InvalidArgumentException('All frames should be of string type.');
48
        }
49 19
        if (Defaults::MAX_FRAME_LENGTH < $length = mb_strlen($frame)) {
50
            throw new \InvalidArgumentException(
51
                sprintf(
52
                    'Single frame max length [%s] exceeded [%s]',
53
                    Defaults::MAX_FRAME_LENGTH,
54
                    $length
55
                )
56
            );
57
        }
58 19
    }
59
60
    /**
61
     * @return string
62
     */
63 18
    protected function getCurrentFrame(): string
64
    {
65 18
        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...
66
    }
67
}
68