Passed
Pull Request — master (#65)
by Andrey
12:46
created

BaseProcessor::full()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 5
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
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 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 78
    /** @var array */
33
    protected $result = [];
34 78
35 78
    public function __construct(Pathable $path)
36
    {
37 78
        $this->path = $path;
38
    }
39 78
40
    public function locale(string $locale): Processor
41 78
    {
42
        $this->locale = $locale;
43
44 78
        return $this;
45
    }
46 78
47
    public function force(bool $is_force = true): Processor
48 78
    {
49
        $this->is_force = $is_force;
50
51 54
        return $this;
52
    }
53 54
54
    public function full(bool $is_full = true): Processor
55
    {
56 54
        $this->is_full = $is_full;
57
58 54
        return $this;
59
    }
60 54
61 54
    public function result(): array
62
    {
63 54
        return $this->result;
64
    }
65 54
66 24
    protected function push(string $filename, string $status): void
67 30
    {
68 30
        $locale = $this->locale;
69
70 18
        $this->result[] = compact('locale', 'filename', 'status');
71
    }
72 18
73
    protected function checkExists(string $path): void
74
    {
75 30
        $this->extension === 'php'
76
            ? File::directoryExist($path, $this->locale)
77 30
            : File::fileExist($path, $this->locale);
78 12
    }
79
80
    protected function isProtected(): bool
81 30
    {
82 30
        return Locale::isProtected($this->locale);
83 30
    }
84
85 30
    protected function publishFile(SplFileInfo $file): void
86 30
    {
87 30
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
88
            return;
89 30
        }
90
91
        $filename = $this->getTargetFilename($file);
92
        $src_file = $this->getSourceFilePath($file);
93
        $dst_file = $this->targetPath($filename);
94
95 54
        if ($this->is_force || ! File::exists($dst_file)) {
96
            $this->copy($src_file, $dst_file, $filename);
97 54
            $this->push($filename, Status::COPIED);
98
99
            return;
100 48
        }
101
102 48
        $this->push($filename, Status::SKIPPED);
103
    }
104
105 30
    protected function sourcePath(): string
106
    {
107 30
        return $this->path->source($this->locale);
108
    }
109 30
110 12
    protected function targetPath(string $filename = null): string
111 30
    {
112 30
        return $this->path->target($this->locale, $filename);
113
    }
114 12
115
    protected function copy(string $source, string $target, string $filename): void
116 12
    {
117 12
        $this->isValidation($filename)
118
            ? $this->copyValidations($source, $target, $filename)
119 12
            : $this->copyOthers($source, $target, $filename);
120 12
    }
121
122 12
    protected function copyValidations(string $src, string $dst, string $filename): void
123 12
    {
124
        $source = File::load($src);
125 12
        $target = File::load($dst, true);
126 12
127 12
        $source_custom     = Arr::get($source, 'custom', []);
128
        $source_attributes = Arr::get($source, 'attributes', []);
129 12
130 12
        $target_custom     = Arr::get($target, 'custom', []);
131
        $target_attributes = Arr::get($target, 'attributes', []);
132 12
133
        $excluded_target     = $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 Helldar\LaravelLangPubli...rocessors\BaseProcessor such as Helldar\LaravelLangPubli...s\Processors\PublishPhp or Helldar\LaravelLangPubli...\Processors\PublishJson. ( Ignorable by Annotation )

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

133
        /** @scrutinizer ignore-call */ 
134
        $excluded_target     = $this->excluded($target, $filename);
Loading history...
134 12
        $excluded_custom     = $this->excluded($target_custom, $filename);
135 12
        $excluded_attributes = $this->excluded($target_attributes, $filename);
136
137 30
        $custom     = array_merge($source_custom, $target_custom, $excluded_custom);
138
        $attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes);
139 30
140 30
        $result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes'));
141
142 30
        File::save($dst, $result);
143
    }
144 30
145
    protected function copyOthers(string $src, string $dst, string $filename): void
146 30
    {
147 30
        $source = File::load($src);
148
        $target = File::load($dst, true);
149 30
150
        $excluded = $this->excluded($target, $filename);
151 30
152
        $result = array_merge($target, $source, $excluded);
153
154 30
        File::save($dst, $result);
155
    }
156 30
157
    protected function isValidation(string $filename): bool
158
    {
159 30
        return Str::startsWith($filename, 'validation');
160
    }
161 30
162
    protected function isInline(string $filename): bool
163
    {
164
        return Str::contains($filename, 'inline');
165
    }
166
167
    protected function getSourceFilePath(SplFileInfo $file): string
168
    {
169
        if (Config::isInline()) {
170
            $path      = $file->getPath();
171
            $extension = $file->getExtension();
172
            $basename  = $file->getBasename('.' . $extension);
173 30
174
            $inline = $path . '/' . $basename . '-inline.' . $extension;
175
176 30
            return file_exists($inline)
177
                ? $inline
178 30
                : $file->getRealPath();
179
        }
180
181
        return $file->getRealPath();
182 30
    }
183
184
    protected function getTargetFilename(SplFileInfo $file): string
185
    {
186
        if ($this->isInline($file->getFilename())) {
187
            return Str::replaceLast('-inline', '', $file->getFilename());
188
        }
189
190
        return $file->getFilename();
191
    }
192
}
193