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

MessageJuggler   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 39
c 3
b 0
f 0
dl 0
loc 105
ccs 38
cts 38
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setMessage() 0 3 1
A refineErasingLen() 0 6 2
A __construct() 0 4 1
A getStyledFrame() 0 4 1
A getFrameErasingLength() 0 3 1
A refineMessage() 0 3 1
A getMessageFullLength() 0 3 1
A updateMessage() 0 22 2
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 MessageJuggler implements JugglerInterface
11
{
12
    /** @var string */
13
    protected $messagePrefix = Defaults::EMPTY_STRING;
14
    /** @var string */
15
    protected $message;
16
    /** @var string */
17
    protected $messageSuffix = Defaults::EMPTY_STRING;
18
    /** @var string */
19
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
20
    /** @var int */
21
    protected $erasingLength;
22
    /** @var string */
23
    protected $frameString;
24
    /** @var int */
25
    protected $frameStringErasingLength;
26
    /** @var int */
27
    protected $erasingLengthDelta;
28
    /** @var Circular */
29
    protected $style;
30
31 22
    public function __construct(string $message, int $erasingLength = null, Circular $style = null)
32
    {
33 22
        $this->style = $style ?? new Circular(['%s',]);
34 22
        $this->updateMessage($message, $erasingLength);
35 22
    }
36
37
    /**
38
     * @param string $message
39
     * @param null|int $erasingLength
40
     */
41 22
    protected function updateMessage(string $message, ?int $erasingLength): void
42
    {
43 22
        $this->message = $this->refineMessage($message);
44 22
        if (Defaults::EMPTY_STRING === $this->message) {
45 1
            $this->erasingLengthDelta = $this->getMessageFullLength();
46 1
            $this->erasingLength = 0;
47 1
            $this->spacer = Defaults::EMPTY_STRING;
48 1
            $this->messagePrefix = Defaults::DEFAULT_PREFIX;
49 1
            $this->messageSuffix = Defaults::EMPTY_STRING;
50
        } else {
51 22
            $erasingLength = $this->refineErasingLen($this->message, $erasingLength);
52 22
            $this->erasingLengthDelta = $this->getMessageFullLength() - $erasingLength;
53 22
            $this->erasingLength = $erasingLength;
54 22
            $this->spacer = Defaults::ONE_SPACE_SYMBOL;
55 22
            $this->messagePrefix = Defaults::DEFAULT_PREFIX;
56 22
            $this->messageSuffix = Defaults::DEFAULT_SUFFIX;
57
        }
58 22
        $this->frameString =
59 22
            $this->messagePrefix . $this->message . $this->messageSuffix . $this->spacer;
60
61 22
        $this->frameStringErasingLength =
62 22
            strlen($this->spacer . $this->messagePrefix . $this->messageSuffix) + $this->erasingLength;
63 22
    }
64
65
    /**
66
     * @param string $message
67
     * @return string
68
     */
69 22
    protected function refineMessage(string $message): string
70
    {
71 22
        return ucfirst($message);
72
    }
73
74
    /**
75
     * @return int
76
     */
77 22
    protected function getMessageFullLength(): int
78
    {
79 22
        return strlen($this->messagePrefix) + $this->erasingLength + strlen($this->messageSuffix);
80
    }
81
82
    /**
83
     * @param string $message
84
     * @param null|int $erasingLength
85
     * @return int
86
     */
87 22
    protected function refineErasingLen(string $message, ?int $erasingLength): int
88
    {
89 22
        if (null === $erasingLength) {
90 5
            return Calculator::computeErasingLength([$message]);
91
        }
92 21
        return $erasingLength;
93
    }
94
95
    /**
96
     * @param string $message
97
     * @param null|int $erasingLength
98
     */
99 5
    public function setMessage(string $message, ?int $erasingLength = null): void
100
    {
101 5
        $this->updateMessage($message, $erasingLength);
102 5
    }
103
104
    /** {@inheritDoc} */
105 21
    public function getStyledFrame(): string
106
    {
107
        return
108 21
            sprintf((string)$this->style->value(), $this->frameString);
109
    }
110
111
    /** {@inheritDoc} */
112 21
    public function getFrameErasingLength(): int
113
    {
114 21
        return $this->frameStringErasingLength;
115
    }
116
}
117