Passed
Push — 9.x ( facc1a...80dc3b )
by Andrey
13:27
created

Info::hasPackages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
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
    public function same(): self
38
    {
39
        return $this;
40
    }
41
42
    public function package(?string $package): self
43
    {
44
        $this->package = $package;
45
46
        return $this;
47
    }
48
49
    public function locale(string $locale, int $length): self
50
    {
51
        $this->locale         = $locale;
52
        $this->locales_length = $length;
53
54
        return $this;
55
    }
56
57
    public function filename(string $filename, int $length): self
58
    {
59
        $this->filename     = $filename;
60
        $this->files_length = $length;
61
62
        return $this;
63
    }
64
65
    public function start(): string
66
    {
67
        $this->log('Return formatted message text for', $this->package, $this->locale, $this->filename);
68
69
        return $this->getPackage() . $this->getLocale() . $this->getFilename();
70
    }
71
72
    public function finish(string $status): string
73
    {
74
        $this->log('Returning the completed state for', $this->package, $this->locale, $this->filename, $status);
75
76
        return $this->getStatus($status);
77
    }
78
79
    protected function getPackage(): ?string
80
    {
81
        $this->log('Get rich text of a message with a package name:', $this->package);
82
83
        if (! $this->package || ! $this->hasPackages()) {
84
            $this->log('The package name was not transmitted or the number of processed packages is not more than one. Processing is skipped.');
85
86
            return null;
87
        }
88
89
        $value = $this->pad($this->package, $this->packagesLength());
90
91
        return $this->format($value, 'fg=#a6a6a6');
92
    }
93
94
    protected function getLocale(): string
95
    {
96
        $this->log('Get rich text of a message with a locale:', $this->locale);
97
98
        $value = $this->pad("[$this->locale]", $this->locales_length + 2);
99
100
        return $this->format($value, 'comment');
101
    }
102
103
    protected function getFilename(): string
104
    {
105
        $this->log('Get rich text of a message with a filename:', $this->filename);
106
107
        $value = $this->pad($this->filename . '...', $this->files_length + 3);
108
109
        return $this->format($value);
110
    }
111
112
    protected function getStatus(string $status): string
113
    {
114
        $this->log('Get rich text of a message with a status:', $status);
115
116
        $style = $this->styles[$status];
117
118
        return $this->format($status, $style);
119
    }
120
121
    protected function hasPackages(): bool
122
    {
123
        $this->log('Checking the number of packages available for processing...');
124
125
        return PackagesSupport::count() > 1;
126
    }
127
128
    protected function packagesLength(): int
129
    {
130
        $this->log('Retrieving the maximum number of packet characters...');
131
132
        if ($this->packages_length > 0) {
133
            return $this->packages_length;
134
        }
135
136
        $this->log('Calculating the maximum length of a packages names...');
137
138
        $packages = PackagesSupport::get();
139
140
        return $this->packages_length = ArrSupport::longestStringLength($packages);
141
    }
142
143
    protected function pad(string $value, int $length): string
144
    {
145
        $this->log('Pad a string to a certain length with another string:', $value, $length);
146
147
        return str_pad($value, $length + 1);
148
    }
149
150
    protected function format(string $value, string $style = null): string
151
    {
152
        $this->log('String formatting:', $value, $style);
153
154
        return $style ? "<$style>$value</>" : $value;
155
    }
156
}
157