Passed
Pull Request — main (#123)
by Andrey
29:01 queued 13:53
created

Add::collectSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 3
dl 0
loc 14
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the "andrey-helldar/laravel-lang-publisher" project.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @author Andrey Helldar <[email protected]>
10
 *
11
 * @copyright 2021 Andrey Helldar
12
 *
13
 * @license MIT
14
 *
15
 * @see https://github.com/andrey-helldar/laravel-lang-publisher
16
 */
17
18
declare(strict_types=1);
19
20
namespace Helldar\LaravelLangPublisher\Processors;
21
22
use Helldar\Contracts\LangPublisher\Provider;
23
use Helldar\LaravelLangPublisher\Comparators\Add as Comparator;
24
use Helldar\LaravelLangPublisher\Constants\Locales;
25
use Helldar\LaravelLangPublisher\Constants\Path;
26
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem;
27
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
28
29
class Add extends BaseProcessor
30
{
31
    protected $comparator = Comparator::class;
32
33
    public function handle(Provider $provider): void
34
    {
35
        foreach ($provider->plugins() as $plugin) {
36
            if (! $plugin->has()) {
37
                continue;
38
            }
39
40
            foreach ($plugin->files() as $source => $target) {
41
                $this->collectSource($provider, $source, $target);
42
                $this->collectLocales($provider, $source, $target);
43
            }
44
        }
45
    }
46
47
    public function finish(): void
48
    {
49
        foreach ($this->compare() as $filename => $values) {
50
            $path = $this->resourcesPath($filename);
51
52
            Filesystem::store($path, $values);
53
        }
54
    }
55
56
    protected function collectSource(Provider $provider, string $source, string $target): void
57
    {
58
        $path = $this->path($provider->basePath(), Path::SOURCE, $source);
59
60
        $filename = $this->preparePath($target, Locales::ENGLISH);
61
62
        $content = Filesystem::load($path);
63
64
        if ($this->hasJson($source)) {
65
            $content = $this->resolveKeys($content);
66
        }
67
68
        $this->set($this->source_keys, $filename, $this->getKeysOnly($content));
69
        $this->set($this->translated, $filename, $content);
70
    }
71
72
    protected function collectLocales(Provider $provider, string $source, string $target): void
73
    {
74
        foreach ($this->locales as $locale) {
75
            $this->collectLocale($provider, $locale, $source, $target);
76
        }
77
    }
78
79
    protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void
80
    {
81
        $path = $this->hasJson($source)
82
            ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json')
83
            : $this->path($provider->basePath(), Path::LOCALES, $locale, $source);
84
85
        $filename = $this->preparePath($target, $locale);
86
87
        $content = Filesystem::load($path);
88
89
        $this->set($this->translated, $filename, $content);
90
    }
91
92
    protected function resolveKeys(array $array): array
93
    {
94
        return Arrayable::of($array)
95
            ->renameKeys(static function ($key, $value) {
96
                return is_numeric($key) && is_string($value) ? $value : $key;
97
            })->get();
98
    }
99
}
100