Passed
Push — main ( 5713ab...156ba4 )
by Andrey
55:20 queued 52:20
created

BaseProcessor::resetFile()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
eloc 10
c 0
b 0
f 0
dl 0
loc 18
ccs 10
cts 11
cp 0.9091
rs 9.6111
cc 5
nc 3
nop 1
crap 5.0187
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 Illuminate\Support\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 36
    public function __construct(Pathable $path)
36
    {
37 36
        $this->path = $path;
38 36
    }
39
40 36
    public function locale(string $locale): Processor
41
    {
42 36
        $this->locale = $locale;
43
44 36
        return $this;
45
    }
46
47 36
    public function force(bool $is_force = true): Processor
48
    {
49 36
        $this->is_force = $is_force;
50
51 36
        return $this;
52
    }
53
54 36
    public function full(bool $is_full = true): Processor
55
    {
56 36
        $this->is_full = $is_full;
57
58 36
        return $this;
59
    }
60
61 36
    public function result(): array
62
    {
63 36
        return $this->result;
64
    }
65
66 36
    protected function push(string $filename, string $status): void
67
    {
68 36
        $locale = $this->locale;
69
70 36
        $this->result[] = compact('locale', 'filename', 'status');
71 36
    }
72
73 36
    protected function checkExists(string $path): void
74
    {
75 36
        $this->extension === 'php'
76 13
            ? File::directoryExist($path, $this->locale)
77 23
            : File::fileExist($path, $this->locale);
78 36
    }
79
80 36
    protected function isProtected(): bool
81
    {
82 36
        return Locale::isProtected($this->locale);
83
    }
84
85 36
    protected function publishFile(SplFileInfo $file): void
86
    {
87 36
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
88 13
            return;
89
        }
90
91 36
        $filename = $this->getTargetFilename($file);
92 36
        $src_file = $this->getSourceFilePath($file);
93 36
        $dst_file = $this->targetPath($filename);
94
95 36
        if ($this->is_force || ! File::exists($dst_file)) {
96 36
            $this->copy($src_file, $dst_file, $filename);
97 36
            $this->push($filename, Status::COPIED);
98
99 36
            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 36
    protected function sourcePath(): string
126
    {
127 36
        return $this->path->source($this->locale);
128
    }
129
130 36
    protected function targetPath(string $filename = null): string
131
    {
132 36
        return $this->path->target($this->locale, $filename);
133
    }
134
135 36
    protected function copy(string $source, string $target, string $filename): void
136
    {
137 36
        $this->isValidation($filename)
138 13
            ? $this->copyValidations($source, $target, $filename)
139 36
            : $this->copyOthers($source, $target, $filename);
140 36
    }
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
        $excluded_target     = $this->excluded($target, $filename);
166 13
        $excluded_custom     = $this->excluded($target_custom, $filename);
167 13
        $excluded_attributes = $this->excluded($target_attributes, $filename);
168
169 13
        $custom     = array_merge($source_custom, $target_custom, $excluded_custom);
170 13
        $attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes);
171
172 13
        $result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes'));
173
174 13
        File::save($dst, $result);
175 13
    }
176
177 36
    protected function copyOthers(string $src, string $dst, string $filename): void
178
    {
179 36
        $source = File::load($src);
180 36
        $target = File::load($dst, true);
181
182 36
        $excluded = $this->excluded($target, $filename);
183
184 36
        $result = array_merge($target, $source, $excluded);
185
186 36
        File::save($dst, $result);
187 36
    }
188
189 36
    protected function isValidation(string $filename): bool
190
    {
191 36
        return Str::startsWith($filename, 'validation');
192
    }
193
194 36
    protected function isInline(string $filename): bool
195
    {
196 36
        return Str::contains($filename, 'inline');
197
    }
198
199 11
    protected function wantsJson(): bool
200
    {
201 11
        return $this->extension === 'json';
202
    }
203
204 36
    protected function getSourceFilePath(SplFileInfo $file): string
205
    {
206 36
        if (Config::isInline()) {
207
            $path      = $file->getPath();
208
            $extension = $file->getExtension();
209
            $basename  = $file->getBasename('.' . $extension);
210
211
            $inline = $path . '/' . $basename . '-inline.' . $extension;
212
213
            return file_exists($inline)
214
                ? $inline
215
                : $file->getRealPath();
216
        }
217
218 36
        return $file->getRealPath();
219
    }
220
221 36
    protected function getTargetFilename(SplFileInfo $file): string
222
    {
223 36
        if ($this->isInline($file->getFilename())) {
224
            return Str::replaceLast('-inline', '', $file->getFilename());
225
        }
226
227 36
        return $file->getFilename();
228
    }
229
}
230