Passed
Pull Request — master (#60)
by Andrey
11:17
created

BaseProcessor   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 168
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 27
eloc 66
c 0
b 0
f 0
dl 0
loc 168
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A copyOthers() 0 10 1
A force() 0 5 1
A __construct() 0 3 1
A getTargetFilename() 0 7 2
A isInline() 0 3 1
A checkExists() 0 5 2
A copyValidations() 0 21 1
A getSourceFilePath() 0 15 3
A locale() 0 5 1
A isProtected() 0 3 1
A publishFile() 0 18 6
A push() 0 5 1
A result() 0 3 1
A isValidation() 0 3 1
A sourcePath() 0 3 1
A copy() 0 7 2
A targetPath() 0 3 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 $force;
25
26
    /** @var string */
27
    protected $extension = 'php';
28
29
    /** @var array */
30
    protected $result = [];
31
32
    public function __construct(Pathable $path)
33
    {
34
        $this->path = $path;
35
    }
36
37
    public function locale(string $locale): self
38
    {
39
        $this->locale = $locale;
40
41
        return $this;
42
    }
43
44
    public function force(bool $force = false): self
45
    {
46
        $this->force = $force;
47
48
        return $this;
49
    }
50
51
    public function result(): array
52
    {
53
        return $this->result;
54
    }
55
56
    protected function push(string $filename, string $status): void
57
    {
58
        $locale = $this->locale;
59
60
        $this->result[] = compact('locale', 'filename', 'status');
61
    }
62
63
    protected function checkExists(string $path): void
64
    {
65
        $this->extension === 'php'
66
            ? File::directoryExist($path, $this->locale)
67
            : File::fileExist($path, $this->locale);
68
    }
69
70
    protected function isProtected(): bool
71
    {
72
        return Locale::isProtected($this->locale);
73
    }
74
75
    protected function publishFile(SplFileInfo $file): void
76
    {
77
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
78
            return;
79
        }
80
81
        $filename = $this->getTargetFilename($file);
82
        $src_file = $this->getSourceFilePath($file);
83
        $dst_file = $this->targetPath($filename);
84
85
        if ($this->force || ! File::exists($dst_file)) {
86
            $this->copy($src_file, $dst_file, $filename);
87
            $this->push($filename, Status::COPIED);
88
89
            return;
90
        }
91
92
        $this->push($filename, Status::SKIPPED);
93
    }
94
95
    protected function sourcePath(): string
96
    {
97
        return $this->path->source($this->locale);
98
    }
99
100
    protected function targetPath(string $filename = null): string
101
    {
102
        return $this->path->target($this->locale, $filename);
103
    }
104
105
    protected function copy(string $source, string $target, string $filename): void
106
    {
107
        $key = File::name($filename);
108
109
        $this->isValidation($filename)
110
            ? $this->copyValidations($source, $target, $key)
111
            : $this->copyOthers($source, $target, $key);
112
    }
113
114
    protected function copyValidations(string $src, string $dst, string $filename): void
115
    {
116
        $source = File::load($src);
117
        $target = File::load($dst, true);
118
119
        $source_custom     = Arr::get($source, 'custom', []);
120
        $source_attributes = Arr::get($source, 'attributes', []);
121
122
        $target_custom     = Arr::get($target, 'custom', []);
123
        $target_attributes = Arr::get($target, 'attributes', []);
124
125
        $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

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