Passed
Pull Request — main (#123)
by Andrey
42:12 queued 26:54
created

Add::collectLocale()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 4
dl 0
loc 9
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
        $content = Filesystem::load($path);
61
62
        if ($this->hasJson($source)) {
63
            $content = $this->resolveKeys($content);
64
        }
65
66
        $this->setResourceKeys($target, $this->getKeysOnly($content));
67
        $this->setResource(Locales::ENGLISH, $target, $content);
68
    }
69
70
    protected function collectLocales(Provider $provider, string $source, string $target): void
71
    {
72
        foreach ($this->locales as $locale) {
73
            $this->collectLocale($provider, $locale, $source, $target);
74
        }
75
    }
76
77
    protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void
78
    {
79
        $path = $this->hasJson($source)
80
            ? $this->path($provider->basePath(), Path::LOCALES, $locale, $locale . '.json')
81
            : $this->path($provider->basePath(), Path::LOCALES, $locale, $source);
82
83
        $content = Filesystem::load($path);
84
85
        $this->setResource($locale, $target, $content);
86
    }
87
88
    protected function resolveKeys(array $array): array
89
    {
90
        return Arrayable::of($array)
91
            ->renameKeys(static function ($key, $value) {
92
                return is_numeric($key) && is_string($value) ? $value : $key;
93
            })->get();
94
    }
95
}
96