Passed
Push — master ( c97482...68f9e3 )
by Andrey
07:00 queued 04:30
created

PublishPhp::publish()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.0283

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 19
ccs 11
cts 12
cp 0.9167
rs 8.8333
cc 7
nc 4
nop 0
crap 7.0283
1
<?php
2
3
namespace Helldar\LaravelLangPublisher\Services\Processing;
4
5
use Helldar\LaravelLangPublisher\Constants\Status;
6
use Helldar\LaravelLangPublisher\Facades\Config;
7
use Helldar\LaravelLangPublisher\Facades\File;
8
use Helldar\LaravelLangPublisher\Facades\Path;
9
use Illuminate\Support\Arr;
10
use Illuminate\Support\Str;
11
use SplFileInfo;
12
13
final class PublishPhp extends BaseProcess
14
{
15 24
    public function run(): array
16
    {
17 24
        $this->checkExists($this->sourcePath());
18 12
        $this->publish();
19
20 12
        return $this->result();
21
    }
22
23 12
    protected function publish(): void
24
    {
25 12
        foreach (File::files($this->sourcePath()) as $file) {
26 12
            if ($file->isDir() || $file->getExtension() !== $this->extension || $this->isInline($file->getFilename())) {
27 12
                continue;
28
            }
29
30 12
            $filename = $this->getTargetFilename($file);
31 12
            $src_file = $this->getSourceFilePath($file);
32 12
            $dst_file = $this->targetPath($filename);
33
34 12
            if ($this->force || ! File::exists($dst_file)) {
35 12
                $this->copy($src_file, $dst_file, $filename);
36 12
                $this->push($filename, Status::COPIED);
37
38 12
                continue;
39
            }
40
41
            $this->push($filename, Status::SKIPPED);
42
        }
43 12
    }
44
45 24
    protected function sourcePath(): string
46
    {
47 24
        return Path::source($this->locale);
48
    }
49
50 12
    protected function targetPath(string $filename): string
51
    {
52 12
        return Path::target($this->locale, $filename);
53
    }
54
55 12
    protected function copy(string $source, string $target, string $filename): void
56
    {
57 12
        $key = File::name($filename);
58
59 12
        $this->isValidation($filename)
60 12
            ? $this->copyValidations($source, $target, $key)
61 12
            : $this->copyOthers($source, $target, $key);
62 12
    }
63
64 12
    protected function copyValidations(string $src, string $dst, string $filename): void
65
    {
66 12
        $source = File::load($src);
67 12
        $target = File::load($dst, true);
68
69 12
        $source_custom     = Arr::get($source, 'custom', []);
70 12
        $source_attributes = Arr::get($source, 'attributes', []);
71
72 12
        $target_custom     = Arr::get($target, 'custom', []);
73 12
        $target_attributes = Arr::get($target, 'attributes', []);
74
75 12
        $excluded_target     = $this->excluded($target, $filename);
76 12
        $excluded_custom     = $this->excluded($target_custom, $filename);
77 12
        $excluded_attributes = $this->excluded($target_attributes, $filename);
78
79 12
        $custom     = array_merge($source_custom, $target_custom, $excluded_custom);
80 12
        $attributes = array_merge($source_attributes, $target_attributes, $excluded_attributes);
81
82 12
        $result = array_merge($target, $source, $excluded_target, compact('custom', 'attributes'));
83
84 12
        File::save($dst, $result);
85 12
    }
86
87 12
    protected function copyOthers(string $src, string $dst, string $filename): void
88
    {
89 12
        $source = File::load($src);
90 12
        $target = File::load($dst, true);
91
92 12
        $excluded = $this->excluded($target, $filename);
93
94 12
        $result = array_merge($target, $source, $excluded);
95
96 12
        File::save($dst, $result);
97 12
    }
98
99 12
    protected function isValidation(string $filename): bool
100
    {
101 12
        return Str::startsWith($filename, 'validation');
102
    }
103
104 12
    protected function isInline(string $filename): bool
105
    {
106 12
        return Str::contains($filename, 'inline');
107
    }
108
109 12
    protected function excluded(array $array, string $key): array
110
    {
111 12
        $keys = Config::getExclude($key, []);
112
113 12
        return Arr::only($array, $keys);
114
    }
115
116 12
    protected function getSourceFilePath(SplFileInfo $file): string
117
    {
118 12
        if (Config::isInline()) {
119
            $path      = $file->getPath();
120
            $extension = $file->getExtension();
121
            $basename  = $file->getBasename('.' . $extension);
122
123
            $inline = $path . '/' . $basename . '-inline.' . $extension;
124
125
            return file_exists($inline)
126
                ? $inline
127
                : $file->getRealPath();
128
        }
129
130 12
        return $file->getRealPath();
131
    }
132
133 12
    protected function getTargetFilename(SplFileInfo $file): string
134
    {
135 12
        if ($this->isInline($file->getFilename())) {
136
            return Str::replaceLast('-inline', '', $file->getFilename());
137
        }
138
139 12
        return $file->getFilename();
140
    }
141
}
142