Passed
Pull Request — main (#123)
by Andrey
26:46 queued 11:34
created

Processor   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 80
dl 0
loc 200
ccs 83
cts 83
cp 1
rs 10
c 4
b 0
f 0
wmc 29

20 Methods

Rating   Name   Duplication   Size   Complexity  
A package() 0 5 1
A locale() 0 5 1
A full() 0 5 1
A main() 0 3 1
A force() 0 5 1
A store() 0 5 1
A manager() 0 5 1
A sourceFilename() 0 5 1
A setTargetPath() 0 5 1
A doesntExists() 0 5 1
A whenPackage() 0 7 2
A setSourcePath() 0 21 5
A compare() 0 11 1
A setMainPath() 0 10 2
A whenSourceFilename() 0 7 2
A whenLocale() 0 7 2
A targetFilename() 0 6 1
A process() 0 10 1
A whenTargetFilename() 0 7 2
A load() 0 5 1
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Processors;
4
5
use Helldar\LaravelLangPublisher\Concerns\Containable;
6
use Helldar\LaravelLangPublisher\Concerns\Contains;
7
use Helldar\LaravelLangPublisher\Concerns\Logger;
8
use Helldar\LaravelLangPublisher\Concerns\Pathable;
9
use Helldar\LaravelLangPublisher\Constants\Locales;
10
use Helldar\LaravelLangPublisher\Contracts\Processor as Contract;
11
use Helldar\LaravelLangPublisher\Services\Comparators\Manage;
12
use Helldar\LaravelLangPublisher\Services\Filesystem\Manager;
13
use Helldar\Support\Concerns\Makeable;
14
use Helldar\Support\Facades\Helpers\Filesystem\File;
15
16
abstract class Processor implements Contract
17
{
18
    use Containable;
19
    use Contains;
20
    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\LaravelLangPubli...es\Processors\Processor.
Loading history...
21
    use Makeable;
22
    use Pathable;
23
24
    protected $package;
25
26
    protected $locale;
27
28
    protected $source_path;
29
30
    protected $target_path;
31
32
    protected $main_path;
33
34
    protected $force;
35 28
36
    protected $full = false;
37 28
38
    public function package(string $package): Contract
39 28
    {
40
        $this->package = $package;
41
42 30
        return $this;
43
    }
44 30
45
    public function locale(string $locale): Contract
46 30
    {
47
        $this->locale = $locale;
48
49 28
        return $this;
50
    }
51 28
52
    public function sourceFilename(string $filename, bool $is_inline = true): Contract
53 28
    {
54
        $this->setSourcePath($filename, $is_inline);
55
56 28
        return $this;
57
    }
58 28
59
    public function targetFilename(string $filename): Contract
60 28
    {
61
        $this->setTargetPath($filename);
62
        $this->setMainPath($filename);
63 30
64
        return $this;
65 30
    }
66
67 30
    public function force(bool $force = false): Contract
68
    {
69
        $this->force = $force;
70 4
71
        return $this;
72 4
    }
73
74 4
    public function full(bool $full = false): Contract
75
    {
76
        $this->full = $full;
77 30
78
        return $this;
79 30
    }
80 28
81
    public function whenPackage(?string $package): Contract
82
    {
83 30
        if ($package) {
84
            $this->package($package);
85
        }
86 30
87
        return $this;
88 30
    }
89 30
90
    public function whenLocale(?string $locale): Contract
91
    {
92 30
        if ($locale) {
93
            $this->locale($locale);
94
        }
95 30
96
        return $this;
97 30
    }
98 28
99
    public function whenSourceFilename(?string $filename, bool $is_inline = true): Contract
100
    {
101 30
        if ($filename) {
102
            $this->sourceFilename($filename, $is_inline);
103
        }
104 30
105
        return $this;
106 30
    }
107 28
108
    public function whenTargetFilename(?string $filename): Contract
109
    {
110 30
        if ($filename) {
111
            $this->targetFilename($filename);
112
        }
113 28
114
        return $this;
115 28
    }
116 28
117
    protected function main(): void
118 28
    {
119
        $this->process($this->source_path, $this->target_path, $this->main_path);
120 28
    }
121
122 28
    protected function process(string $source_path, string $target_path, string $main_path = null): void
123 28
    {
124
        $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.');
125 28
126
        $source = $this->load($source_path, $main_path);
127 28
        $target = $this->load($target_path);
128 28
129
        $result = $this->compare($source, $target);
130 28
131
        $this->store($target_path, $result);
132 28
    }
133
134 28
    protected function setSourcePath(string $filename, bool $is_inline): void
135
    {
136 28
        $this->log('Setting the path to the source file:', $filename);
137 15
138
        $path = $this->isJson($filename)
139 15
            ? $this->pathSource($this->package, Locales::ENGLISH)
140 15
            : $this->pathSource($this->package, $this->locale);
141 15
142
        if ($is_inline) {
143 15
            $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline messages...');
144
145
            $directory = $this->pathDirectory($filename);
146 28
            $name      = $this->pathFilename($filename);
147 15
            $extension = $this->pathExtension($filename);
148 28
149 28
            $inline_file = $directory . '/' . $name . '-inline.' . $extension;
150
        }
151 28
152
        $this->source_path = $is_inline && File::exists($path . '/' . $inline_file)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $inline_file does not seem to be defined for all execution paths leading up to this point.
Loading history...
153 28
            ? $path . '/' . $inline_file
154
            : $path . '/' . $filename;
155 28
    }
156 28
157
    protected function setTargetPath(string $filename): void
158 28
    {
159
        $this->log('Setting the path to the target file:', $filename);
160 28
161
        $this->target_path = $this->pathTargetFull($this->locale, $filename);
162 28
    }
163 28
164 28
    protected function setMainPath(string $filename): void
165 28
    {
166 28
        $this->log('Setting the path to the json main file with', $filename);
167 28
168 28
        if ($this->isJson($filename)) {
169
            $path = $this->pathSource($this->package, $this->locale);
170
171 28
            $file = $this->locale . '.json';
172
173 28
            $this->main_path = $path . '/' . $file;
174
        }
175 28
    }
176
177
    protected function compare(array $source, array $target): array
178 28
    {
179
        $this->log('Find an object and perform object comparison.');
180 28
181
        return Manage::make()
182 28
            ->filename($this->source_path)
183 28
            ->full($this->full)
184
            ->source($source)
185 28
            ->target($target)
186
            ->find()
187 28
            ->toArray();
188
    }
189 28
190
    protected function load(string $path, string $filename = null): array
191
    {
192 12
        $this->log('Loading an array:', $path, $filename);
193
194 12
        return $this->manager()->load($path, $filename);
195
    }
196 12
197
    protected function store(string $path, array $content): void
198
    {
199
        $this->log('Saving an array to a file:', $path);
200
201
        $this->manager()->store($path, $content);
202
    }
203
204
    protected function manager(): Manager
205
    {
206
        $this->log('Getting a comparison object...');
207
208
        return $this->container(Manager::class);
209
    }
210
211
    protected function doesntExists(): bool
212
    {
213
        $this->log('Checking for the existence of a file:', $this->target_path);
214
215
        return ! File::exists($this->target_path);
216
    }
217
}
218