Passed
Push — master ( 68f9e3...36b0af )
by Andrey
02:57 queued 39s
created

BaseProcessor::result()   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 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 36
    public function __construct(Pathable $path)
33
    {
34 36
        $this->path = $path;
35 36
    }
36
37 78
    public function locale(string $locale): self
38
    {
39 78
        $this->locale = $locale;
40
41 78
        return $this;
42
    }
43
44 78
    public function force(bool $force = false): self
45
    {
46 78
        $this->force = $force;
47
48 78
        return $this;
49
    }
50
51 54
    public function result(): array
52
    {
53 54
        return $this->result;
54
    }
55
56 54
    protected function push(string $filename, string $status): void
57
    {
58 54
        $locale = $this->locale;
59
60 54
        $this->result[] = compact('locale', 'filename', 'status');
61 54
    }
62
63 54
    protected function checkExists(string $path): void
64
    {
65 54
        $this->extension === 'php'
66 24
            ? File::directoryExist($path, $this->locale)
67 30
            : File::fileExist($path, $this->locale);
68 30
    }
69
70 18
    protected function isProtected(): bool
71
    {
72 18
        return Locale::isProtected($this->locale);
73
    }
74
75 30
    protected function publishFile(SplFileInfo $file): void
76
    {
77 30
        if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
78 12
            return;
79
        }
80
81 30
        $filename = $this->getTargetFilename($file);
82 30
        $src_file = $this->getSourceFilePath($file);
83 30
        $dst_file = $this->targetPath($filename);
84
85 30
        if ($this->force || ! File::exists($dst_file)) {
86 30
            $this->copy($src_file, $dst_file, $filename);
87 30
            $this->push($filename, Status::COPIED);
88
89 30
            return;
90
        }
91
92
        $this->push($filename, Status::SKIPPED);
93
    }
94
95 54
    protected function sourcePath(): string
96
    {
97 54
        return $this->path->source($this->locale);
98
    }
99
100 48
    protected function targetPath(string $filename = null): string
101
    {
102 48
        return $this->path->target($this->locale, $filename);
103
    }
104
105 30
    protected function copy(string $source, string $target, string $filename): void
106
    {
107 30
        $key = File::name($filename);
108
109 30
        $this->isValidation($filename)
110 12
            ? $this->copyValidations($source, $target, $key)
111 30
            : $this->copyOthers($source, $target, $key);
112 30
    }
113
114 12
    protected function copyValidations(string $src, string $dst, string $filename): void
115
    {
116 12
        $source = File::load($src);
117 12
        $target = File::load($dst, true);
118
119 12
        $source_custom     = Arr::get($source, 'custom', []);
120 12
        $source_attributes = Arr::get($source, 'attributes', []);
121
122 12
        $target_custom     = Arr::get($target, 'custom', []);
123 12
        $target_attributes = Arr::get($target, 'attributes', []);
124
125 12
        $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 12
        $excluded_custom     = $this->excluded($target_custom, $filename);
127 12
        $excluded_attributes = $this->excluded($target_attributes, $filename);
128
129 12
        $custom     = array_merge($source_custom, $target_custom, $excluded_custom);
130 12
        $attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes);
131
132 12
        $result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes'));
133
134 12
        File::save($dst, $result);
135 12
    }
136
137 30
    protected function copyOthers(string $src, string $dst, string $filename): void
138
    {
139 30
        $source = File::load($src);
140 30
        $target = File::load($dst, true);
141
142 30
        $excluded = $this->excluded($target, $filename);
143
144 30
        $result = array_merge($target, $source, $excluded);
145
146 30
        File::save($dst, $result);
147 30
    }
148
149 30
    protected function isValidation(string $filename): bool
150
    {
151 30
        return Str::startsWith($filename, 'validation');
152
    }
153
154 30
    protected function isInline(string $filename): bool
155
    {
156 30
        return Str::contains($filename, 'inline');
157
    }
158
159 30
    protected function getSourceFilePath(SplFileInfo $file): string
160
    {
161 30
        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 30
        return $file->getRealPath();
174
    }
175
176 30
    protected function getTargetFilename(SplFileInfo $file): string
177
    {
178 30
        if ($this->isInline($file->getFilename())) {
179
            return Str::replaceLast('-inline', '', $file->getFilename());
180
        }
181
182 30
        return $file->getFilename();
183
    }
184
}
185