Passed
Push — main ( 344315...55f929 )
by Andrey
02:39
created

BaseProcessor::wantsJson()   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
eloc 1
c 0
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
2
3
namespace Helldar\LaravelLangPublisher\Services\Processors;
4
5
use Helldar\LaravelLangPublisher\Constants\Status;
6
use Helldar\LaravelLangPublisher\Contracts\Pathable;
7
use Helldar\LaravelLangPublisher\Contracts\Processor;
8
use Helldar\LaravelLangPublisher\Facades\Config;
9
use Helldar\LaravelLangPublisher\Facades\File;
10
use Helldar\LaravelLangPublisher\Facades\Locale;
11
use Helldar\Support\Facades\Helpers\Arr;
12
use Illuminate\Support\Str;
13
use SplFileInfo;
14
15
abstract class BaseProcessor implements Processor
16
{
17
    /** @var \Helldar\LaravelLangPublisher\Contracts\Pathable */
18
    protected $path;
19
20
    /** @var string */
21
    protected $locale;
22
23
    /** @var bool */
24
    protected $is_force;
25
26
    /** @var bool */
27
    protected $is_full;
28
29
    /** @var string */
30
    protected $extension = 'php';
31
32
    /** @var array */
33
    protected $result = [];
34
35 48
    public function __construct(Pathable $path)
36
    {
37 48
        $this->path = $path;
38 48
    }
39
40 48
    public function locale(string $locale): Processor
41
    {
42 48
        $this->locale = $locale;
43
44 48
        return $this;
45
    }
46
47 48
    public function force(bool $is_force = true): Processor
48
    {
49 48
        $this->is_force = $is_force;
50
51 48
        return $this;
52
    }
53
54 48
    public function full(bool $is_full = true): Processor
55
    {
56 48
        $this->is_full = $is_full;
57
58 48
        return $this;
59
    }
60
61 48
    public function result(): array
62
    {
63 48
        return $this->result;
64
    }
65
66 48
    protected function push(string $filename, string $status): void
67
    {
68 48
        $locale = $this->locale;
69
70 48
        $this->result[] = compact('locale', 'filename', 'status');
71 48
    }
72
73 48
    protected function checkExists(string $path): void
74
    {
75 48
        $this->extension === 'php'
76 13
            ? File::directoryExist($path, $this->locale)
77 35
            : File::fileExist($path, $this->locale);
78 48
    }
79
80 48
    protected function isProtected(): bool
81
    {
82 48
        return Locale::isProtected($this->locale);
83
    }
84
85 48
    protected function publishFile(SplFileInfo $file): void
86
    {
87 48
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
88 13
            return;
89
        }
90
91 48
        $filename = $this->getTargetFilename($file);
92 48
        $src_file = $this->getSourceFilePath($file);
93 48
        $dst_file = $this->targetPath($filename);
94
95 48
        if ($this->is_force || ! File::exists($dst_file)) {
96 48
            $this->copy($src_file, $dst_file, $filename);
97 48
            $this->push($filename, Status::COPIED);
98
99 48
            return;
100
        }
101
102
        $this->push($filename, Status::SKIPPED);
103
    }
104
105 4
    protected function resetFile(SplFileInfo $file): void
106
    {
107 4
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
108 2
            return;
109
        }
110
111 4
        $filename = $this->getTargetFilename($file);
112 4
        $src_file = $this->getSourceFilePath($file);
113 4
        $dst_file = $this->targetPath($filename);
114
115 4
        if (File::exists($dst_file)) {
116 4
            $this->reset($src_file, $dst_file, $filename);
117 4
            $this->push($filename, Status::RESET);
118
119 4
            return;
120
        }
121
122
        $this->push($filename, Status::SKIPPED);
123
    }
124
125 48
    protected function sourcePath(): string
126
    {
127 48
        return $this->path->source($this->locale);
128
    }
129
130 48
    protected function targetPath(string $filename = null): string
131
    {
132 48
        return $this->path->target($this->locale, $filename);
133
    }
134
135 48
    protected function copy(string $source, string $target, string $filename): void
136
    {
137 48
        $this->isValidation($filename)
138 13
            ? $this->copyValidations($source, $target, $filename)
139 48
            : $this->copyOthers($source, $target, $filename);
140 48
    }
141
142 4
    protected function reset(string $src, string $dst, string $filename): void
143
    {
144 4
        $source = File::load($src);
145 4
        $target = File::load($dst, true);
146
147 4
        $result = $this->is_full
148 2
            ? $source
149 4
            : array_merge($source, $this->excluded($target, $filename));
0 ignored issues
show
Bug introduced by
The method excluded() does not exist on Helldar\LaravelLangPubli...rocessors\BaseProcessor. It seems like you code against a sub-type of said class. However, the method does not exist in Helldar\LaravelLangPubli...es\Processors\DeletePhp or Helldar\LaravelLangPubli...s\Processors\DeleteJson. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

149
            : array_merge($source, $this->/** @scrutinizer ignore-call */ excluded($target, $filename));
Loading history...
150
151 4
        File::save($dst, $result);
152 4
    }
153
154 13
    protected function copyValidations(string $src, string $dst, string $filename): void
155
    {
156 13
        $source = File::load($src);
157 13
        $target = File::load($dst, true);
158
159 13
        $source_custom     = Arr::get($source, 'custom', []);
160 13
        $source_attributes = Arr::get($source, 'attributes', []);
161
162 13
        $target_custom     = Arr::get($target, 'custom', []);
163 13
        $target_attributes = Arr::get($target, 'attributes', []);
164
165 13
        $source = Arr::except($source, ['custom', 'attributes']);
166 13
        $target = Arr::except($target, ['custom', 'attributes']);
167
168 13
        $excluded_target     = $this->excluded($target, $filename);
169 13
        $excluded_custom     = $this->excluded($target_custom, $filename);
170 13
        $excluded_attributes = $this->excluded($target_attributes, $filename);
171
172 13
        $custom     = Arr::ksort(array_merge($source_custom, $target_custom, $excluded_custom));
173 13
        $attributes = Arr::ksort(array_merge($source_attributes, $target_attributes, $excluded_attributes));
174
175 13
        $main = Arr::ksort(array_merge($target, $source, $excluded_target));
176
177 13
        $result = array_merge($main, compact('custom', 'attributes'));
178
179 13
        File::save($dst, $result);
180 13
    }
181
182 48
    protected function copyOthers(string $src, string $dst, string $filename): void
183
    {
184 48
        $source = File::load($src);
185 48
        $target = File::load($dst, true);
186
187 48
        $excluded = $this->excluded($target, $filename);
188
189 48
        $result = array_merge($target, $source, $excluded);
190
191 48
        File::save($dst, $result);
192 48
    }
193
194 48
    protected function isValidation(string $filename): bool
195
    {
196 48
        return Str::startsWith($filename, 'validation');
197
    }
198
199 48
    protected function isInline(string $filename): bool
200
    {
201 48
        return Str::contains($filename, 'inline');
202
    }
203
204 15
    protected function wantsJson(): bool
205
    {
206 15
        return $this->extension === 'json';
207
    }
208
209 48
    protected function getSourceFilePath(SplFileInfo $file): string
210
    {
211 48
        if (Config::isInline()) {
212
            $path      = $file->getPath();
213
            $extension = $file->getExtension();
214
            $basename  = $file->getBasename('.' . $extension);
215
216
            $inline = $path . '/' . $basename . '-inline.' . $extension;
217
218
            return file_exists($inline)
219
                ? $inline
220
                : $file->getRealPath();
221
        }
222
223 48
        return $file->getRealPath();
224
    }
225
226 48
    protected function getTargetFilename(SplFileInfo $file): string
227
    {
228 48
        if ($this->isInline($file->getFilename())) {
229
            return Str::replaceLast('-inline', '', $file->getFilename());
230
        }
231
232 48
        return $file->getFilename();
233
    }
234
}
235