Test Failed
Push — master ( 58858d...095b07 )
by Alec
03:13
created

MessageJuggler::firstInLine()   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 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Spinner\Core\Jugglers;
4
5
use AlecRabbit\Spinner\Core\Calculator;
6
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface;
7
use AlecRabbit\Spinner\Settings\Contracts\Defaults;
8
9
class MessageJuggler implements JugglerInterface
10
{
11
    /** @var string */
12
    protected $spacer = Defaults::ONE_SPACE_SYMBOL;
13
    /** @var string */
14
    protected $messagePrefix = Defaults::EMPTY_STRING;
15
    /** @var string */
16
    protected $message;
17
    /** @var string */
18
    protected $messageSuffix = Defaults::EMPTY_STRING;
19
    /** @var int */
20
    protected $erasingLength;
21
    /** @var string */
22
    protected $frameString;
23
    /** @var int */
24
    protected $frameStringErasingLength;
25
    /** @var int */
26
    protected $erasingLengthDelta;
27
28
    public function __construct(string $message, int $erasingLength = null)
29
    {
30 20
        $this->updateMessage($message, $erasingLength);
31
    }
32 20
33 20
    /**
34
     * @param string $message
35
     * @param null|int $erasingLength
36
     */
37
    protected function updateMessage(string $message, ?int $erasingLength): void
38
    {
39 20
        $this->message = $message;
40
        if (Defaults::EMPTY_STRING === $message) {
41 20
            $this->erasingLengthDelta = $this->getMessageFullLength();
42 20
            $this->erasingLength = 0;
43 1
            $this->spacer = Defaults::EMPTY_STRING;
44 1
            $this->messagePrefix = Defaults::DEFAULT_PREFIX;
45 1
            $this->messageSuffix = Defaults::EMPTY_STRING;
46 1
        } else {
47 1
            $erasingLength = $this->refineErasingLen($message, $erasingLength);
48
            $this->erasingLengthDelta = $this->getMessageFullLength() - $erasingLength;
49 20
            $this->erasingLength = $erasingLength;
50 20
            $this->spacer = Defaults::ONE_SPACE_SYMBOL;
51 20
            $this->messagePrefix = Defaults::DEFAULT_PREFIX;
52 20
            $this->messageSuffix = Defaults::DEFAULT_SUFFIX;
53 20
        }
54 20
        $this->frameString =
55
            $this->messagePrefix . $this->message . $this->messageSuffix  . $this->spacer;
56 20
57 1
        $this->frameStringErasingLength =
58
            strlen($this->spacer . $this->messagePrefix . $this->messageSuffix) + $this->erasingLength;
59 20
//        strlen($this->spacer) + $this->getMessageFullLength();
60 20
    }
61
62 20
    /**
63 20
     * @return int
64
     */
65 20
    protected function getMessageFullLength(): int
66
    {
67
        return strlen($this->messagePrefix) + $this->erasingLength + strlen($this->messageSuffix);
68
    }
69
70 20
    /**
71
     * @param string $message
72 20
     * @param null|int $erasingLength
73
     * @return int
74
     */
75
    protected function refineErasingLen(string $message, ?int $erasingLength): int
76
    {
77
        if (null === $erasingLength) {
78
            return Calculator::computeErasingLength([$message]);
79
        }
80 20
        return $erasingLength;
81
    }
82 20
83 3
    /**
84
     * @param string $message
85 19
     * @param null|int $erasingLength
86
     */
87
    public function setMessage(string $message, ?int $erasingLength = null): void
88
    {
89
        $message = ucfirst($message);
90
        $this->updateMessage($message, $erasingLength);
91
    }
92 3
93
    public function getFrame(): string
94 3
    {
95 3
        return $this->frameString;
96
    }
97 19
98
    public function getFrameErasingLength(): int
99 19
    {
100
        return $this->frameStringErasingLength;
101
    }
102 19
103
    /**
104 19
     * @return int
105
     */
106
    public function getErasingLengthDelta(): int
107
    {
108
        return $this->erasingLengthDelta;
109
    }
110
}
111