Passed
Push — master ( 4505d6...4505d6 )
by Alec
02:17
created

MessageJuggler::getMessageFullLength()   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 0
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;
15
    /** @var string */
16
    protected $message;
17
    /** @var string */
18
    protected $messageSuffix = Defaults::EMPTY;
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
    /** @var bool */
28
    protected $firstInLine = false;
29
30 20
    public function __construct(string $message, int $erasingLength = null)
31
    {
32 20
        $this->updateMessage($message, $erasingLength);
33 20
    }
34
35
    /**
36
     * @param string $message
37
     * @param null|int $erasingLength
38
     */
39 20
    protected function updateMessage(string $message, ?int $erasingLength): void
40
    {
41 20
        $this->message = $message;
42 20
        if (Defaults::EMPTY === $message) {
43 1
            $this->erasingLengthDelta = $this->getMessageFullLength();
44 1
            $this->erasingLength = 0;
45 1
            $this->spacer = Defaults::EMPTY;
46 1
            $this->messagePrefix = Defaults::EMPTY;
47 1
            $this->messageSuffix = Defaults::EMPTY;
48
        } else {
49 20
            $erasingLength = $this->refineErasingLen($message, $erasingLength);
50 20
            $this->erasingLengthDelta = $this->getMessageFullLength() - $erasingLength;
51 20
            $this->erasingLength = $erasingLength;
52 20
            $this->spacer = Defaults::ONE_SPACE_SYMBOL;
53 20
            $this->messagePrefix = Defaults::EMPTY;
54 20
            $this->messageSuffix = Defaults::DEFAULT_SUFFIX;
55
        }
56 20
        if ($this->firstInLine) {
57 1
            $this->spacer = Defaults::EMPTY;
58
        }
59 20
        $this->frameString =
60 20
            $this->spacer . $this->messagePrefix . $this->message . $this->messageSuffix;
61
62 20
        $this->frameStringErasingLength =
63 20
            strlen($this->spacer . $this->messagePrefix . $this->messageSuffix) + $this->erasingLength;
64
//        strlen($this->spacer) + $this->getMessageFullLength();
65 20
    }
66
67
    /**
68
     * @return int
69
     */
70 20
    protected function getMessageFullLength(): int
71
    {
72 20
        return strlen($this->messagePrefix) + $this->erasingLength + strlen($this->messageSuffix);
73
    }
74
75
    /**
76
     * @param string $message
77
     * @param null|int $erasingLength
78
     * @return int
79
     */
80 20
    protected function refineErasingLen(string $message, ?int $erasingLength): int
81
    {
82 20
        if (null === $erasingLength) {
83 3
            return Calculator::computeErasingLength([$message]);
84
        }
85 19
        return $erasingLength;
86
    }
87
88
    /**
89
     * @param string $message
90
     * @param null|int $erasingLength
91
     */
92 3
    public function setMessage(string $message, ?int $erasingLength = null): void
93
    {
94 3
        $this->updateMessage($message, $erasingLength);
95 3
    }
96
97 19
    public function getFrame(): string
98
    {
99 19
        return $this->frameString;
100
    }
101
102 19
    public function getFrameErasingLength(): int
103
    {
104 19
        return $this->frameStringErasingLength;
105
    }
106
107
    /**
108
     * @return int
109
     */
110 19
    public function getErasingLengthDelta(): int
111
    {
112 19
        return $this->erasingLengthDelta;
113
    }
114
115 20
    public function firstInLine(bool $firstInLine): void
116
    {
117 20
        $this->firstInLine = $firstInLine;
118 20
    }
119
}
120