Passed
Push — master ( 433f17...2d5bb4 )
by Alec
03:00
created

AbstractJuggler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 11
c 2
b 0
f 0
dl 0
loc 58
ccs 11
cts 11
cp 1
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A computeFormatErasingWidthShift() 0 3 1
A getFrameErasingLength() 0 3 1
A getStyledFrame() 0 4 1
A init() 0 5 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\Coloring\Style;
7
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface;
8
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
9
use AlecRabbit\Spinner\Settings\Settings;
10
use function AlecRabbit\Helpers\wcswidth;
11
12
abstract class AbstractJuggler implements JugglerInterface
13
{
14
    /** @var string */
15
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
16
    /**
17
     * @var int
18
     * @psalm-suppress PropertyNotSetInConstructor
19
     */
20
    protected $currentFrameErasingWidth;
21
    /**
22
     * @var Circular
23
     * @psalm-suppress PropertyNotSetInConstructor
24
     */
25
    protected $style;
26
    /** @var int */
27
    protected $formatErasingWidthShift = 0;
28
29
    /**
30
     * @param Settings $settings
31
     * @param Style $style
32
     */
33
    abstract public function __construct(Settings $settings, Style $style);
34
35
    /** {@inheritDoc} */
36 25
    public function getFrameErasingLength(): int
37
    {
38 25
        return $this->currentFrameErasingWidth;
39
    }
40
41
    /** {@inheritDoc} */
42 25
    public function getStyledFrame(): string
43
    {
44
        return
45 25
            sprintf((string)$this->style->value(), $this->getCurrentFrame()) . $this->spacer;
46
    }
47
48
    /**
49
     * @return string
50
     */
51
    abstract protected function getCurrentFrame(): string;
52
53
    /**
54
     * @param Style $style
55
     */
56 27
    protected function init(Style $style): void
57
    {
58 27
        $this->style = $style->getStyle();
59 27
        $this->formatErasingWidthShift = $this->computeFormatErasingWidthShift($style->getFormat());
60 27
        $this->spacer = $style->getSpacer();
61 27
    }
62
63
    /**
64
     * @param string $format
65
     * @return int
66
     */
67 27
    protected function computeFormatErasingWidthShift(string $format): int
68
    {
69 27
        return wcswidth(sprintf($format, ''));
70
    }
71
}
72