Passed
Push — main ( 9ad958...bccbba )
by Andrey
72:16 queued 69:33
created

Info::same()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Support;
4
5
use Helldar\LaravelLangPublisher\Concerns\Contains;
6
use Helldar\LaravelLangPublisher\Concerns\Keyable;
7
use Helldar\LaravelLangPublisher\Concerns\Logger;
8
use Helldar\LaravelLangPublisher\Constants\Status;
9
use Helldar\LaravelLangPublisher\Facades\Packages as PackagesSupport;
10
use Helldar\Support\Facades\Helpers\Arr as ArrSupport;
11
12
final class Info
13
{
14
    use Contains;
15
    use Keyable;
16
    use Logger;
0 ignored issues
show
Bug introduced by
The trait Helldar\LaravelLangPublisher\Concerns\Logger requires the property $output which is not provided by Helldar\LaravelLangPublisher\Support\Info.
Loading history...
17
18
    protected $styles = [
19
        Status::COPIED  => 'fg=green',
20
        Status::RESET   => 'fg=magenta',
21
        Status::SKIPPED => 'fg=default',
22
        Status::DELETED => 'fg=red',
23
    ];
24
25
    protected $package;
26
27
    protected $packages_length = 0;
28
29
    protected $locale;
30
31
    protected $locales_length = 0;
32
33
    protected $filename;
34
35
    protected $files_length = 0;
36
37 12
    public function same(): self
38
    {
39 12
        return $this;
40
    }
41
42 12
    public function package(?string $package): self
43
    {
44 12
        $this->package = $package;
45
46 12
        return $this;
47
    }
48
49 12
    public function locale(string $locale, int $length): self
50
    {
51 12
        $this->locale         = $locale;
52 12
        $this->locales_length = $length;
53
54 12
        return $this;
55
    }
56
57 12
    public function filename(string $filename, int $length): self
58
    {
59 12
        $this->filename     = $filename;
60 12
        $this->files_length = $length;
61
62 12
        return $this;
63
    }
64
65 12
    public function start(): string
66
    {
67 12
        $this->log('Return formatted message text for', $this->package, $this->locale, $this->filename);
68
69 12
        return $this->getPackage() . $this->getLocale() . $this->getFilename();
70
    }
71
72 12
    public function finish(string $status): string
73
    {
74 12
        $this->log('Returning the completed state for', $this->package, $this->locale, $this->filename, $status);
75
76 12
        return $this->getStatus($status);
77
    }
78
79 12
    protected function getPackage(): ?string
80
    {
81 12
        $this->log('Get rich text of a message with a package name:', $this->package);
82
83 12
        if (! $this->package || ! $this->hasPackages()) {
84 4
            $this->log('The package name was not transmitted or the number of processed packages is not more than one. Processing is skipped.');
85
86 4
            return null;
87
        }
88
89 11
        $value = $this->pad($this->package, $this->packagesLength());
90
91 11
        return $this->format($value, 'fg=#a6a6a6');
92
    }
93
94 12
    protected function getLocale(): string
95
    {
96 12
        $this->log('Get rich text of a message with a locale:', $this->locale);
97
98 12
        $value = $this->pad("[$this->locale]", $this->locales_length + 2);
99
100 12
        return $this->format($value, 'comment');
101
    }
102
103 12
    protected function getFilename(): string
104
    {
105 12
        $this->log('Get rich text of a message with a filename:', $this->filename);
106
107 12
        $value = $this->pad($this->filename . '...', $this->files_length + 3);
108
109 12
        return $this->format($value);
110
    }
111
112 12
    protected function getStatus(string $status): string
113
    {
114 12
        $this->log('Get rich text of a message with a status:', $status);
115
116 12
        $style = $this->styles[$status];
117
118 12
        return $this->format($status, $style);
119
    }
120
121 11
    protected function hasPackages(): bool
122
    {
123 11
        $this->log('Checking the number of packages available for processing...');
124
125 11
        return PackagesSupport::count() > 1;
126
    }
127
128 11
    protected function packagesLength(): int
129
    {
130 11
        $this->log('Retrieving the maximum number of packet characters...');
131
132 11
        if ($this->packages_length > 0) {
133 11
            return $this->packages_length;
134
        }
135
136 11
        $this->log('Calculating the maximum length of a packages names...');
137
138 11
        $packages = PackagesSupport::get();
139
140 11
        return $this->packages_length = ArrSupport::longestStringLength($packages);
141
    }
142
143 12
    protected function pad(string $value, int $length): string
144
    {
145 12
        $this->log('Pad a string to a certain length with another string:', $value, $length);
146
147 12
        return str_pad($value, $length + 1);
148
    }
149
150 12
    protected function format(string $value, string $style = null): string
151
    {
152 12
        $this->log('String formatting:', $value, $style);
153
154 12
        return $style ? "<$style>$value</>" : $value;
155
    }
156
}
157