Passed
Push — 10.x ( dd1917...7f7331 )
by Andrey
15:19
created

Processor::process()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 3
dl 0
loc 10
ccs 7
cts 7
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 14
36
    protected $full = false;
37 14
38
    public function package(string $package): Contract
39 14
    {
40
        $this->package = $package;
41
42 15
        return $this;
43
    }
44 15
45
    public function locale(string $locale): Contract
46 15
    {
47
        $this->locale = $locale;
48
49 14
        return $this;
50
    }
51 14
52 14
    public function sourceFilename(string $filename, bool $is_inline = true): Contract
53
    {
54 14
        $this->setSourcePath($filename, $is_inline);
55
56
        return $this;
57 15
    }
58
59 15
    public function targetFilename(string $filename): Contract
60
    {
61 15
        $this->setTargetPath($filename);
62
        $this->setMainPath($filename);
63
64 2
        return $this;
65
    }
66 2
67
    public function force(bool $force = false): Contract
68 2
    {
69
        $this->force = $force;
70
71 15
        return $this;
72
    }
73 15
74 14
    public function full(bool $full = false): Contract
75
    {
76
        $this->full = $full;
77 15
78
        return $this;
79
    }
80 15
81
    public function whenPackage(?string $package): Contract
82 15
    {
83 15
        if ($package) {
84
            $this->package($package);
85
        }
86 15
87
        return $this;
88
    }
89 15
90
    public function whenLocale(?string $locale): Contract
91 15
    {
92 14
        if ($locale) {
93
            $this->locale($locale);
94
        }
95 15
96
        return $this;
97
    }
98 14
99
    public function whenSourceFilename(?string $filename, bool $is_inline = true): Contract
100 14
    {
101
        if ($filename) {
102 14
            $this->sourceFilename($filename, $is_inline);
103 14
        }
104
105 14
        return $this;
106 14
    }
107
108 14
    public function whenTargetFilename(?string $filename): Contract
109 14
    {
110 14
        if ($filename) {
111
            $this->targetFilename($filename);
112
        }
113 14
114 14
        return $this;
115
    }
116 14
117
    protected function main(): void
118 14
    {
119
        $this->process($this->source_path, $this->target_path, $this->main_path);
120 14
    }
121
122 14
    protected function process(string $source_path, string $target_path, string $main_path = null): void
123 14
    {
124
        $this->log('The process of processing a file from', $source_path, 'to', $target_path, 'has begun.');
125 14
126
        $source = $this->load($source_path, $main_path);
127 14
        $target = $this->load($target_path);
128
129 14
        $result = $this->compare($source, $target);
130 14
131 14
        $this->store($target_path, $result);
132 14
    }
133 14
134 14
    protected function setSourcePath(string $filename, bool $is_inline): void
135 14
    {
136
        $this->log('Setting the path to the source file:', $filename);
137
138 14
        $path = $this->isJson($filename)
139
            ? $this->pathSource($this->package, Locales::ENGLISH)
140 14
            : $this->pathSource($this->package, $this->locale);
141
142 14
        if ($is_inline) {
143
            $this->log('The', $filename, '(is inline: ', $is_inline, ')', 'file is a collection of inline messages...');
144
145 14
            $directory = $this->pathDirectory($filename);
146
            $name      = $this->pathFilename($filename);
147 14
            $extension = $this->pathExtension($filename);
148
149 14
            $inline_file = $directory . '/' . $name . '-inline.' . $extension;
150 14
        }
151
152 14
        $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
            ? $path . '/' . $inline_file
154 14
            : $path . '/' . $filename;
155
    }
156 14
157
    protected function setTargetPath(string $filename): void
158
    {
159 7
        $this->log('Setting the path to the target file:', $filename);
160
161 7
        $this->target_path = $this->pathTargetFull($this->locale, $filename);
162
    }
163 7
164
    protected function setMainPath(string $filename): void
165
    {
166
        $this->log('Setting the path to the json main file with', $filename);
167
168
        if ($this->isJson($filename)) {
169
            $path = $this->pathSource($this->package, $this->locale);
170
171
            $file = $this->locale . '.json';
172
173
            $this->main_path = $path . '/' . $file;
174
        }
175
    }
176
177
    protected function compare(array $source, array $target): array
178
    {
179
        $this->log('Find an object and perform object comparison.');
180
181
        return Manage::make()
182
            ->filename($this->source_path)
183
            ->full($this->full)
184
            ->source($source)
185
            ->target($target)
186
            ->find()
187
            ->toArray();
188
    }
189
190
    protected function load(string $path, string $filename = null): array
191
    {
192
        $this->log('Loading an array:', $path, $filename);
193
194
        return $this->manager()->load($path, $filename);
195
    }
196
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