Passed
Push — main ( f4d82a...ca2db5 )
by Andrey
03:23 queued 12s
created

Add::resolveKeys()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 1
nop 1
dl 0
loc 6
ccs 5
cts 5
cp 1
crap 3
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 62
    public function handle(Provider $provider): void
34
    {
35 62
        foreach ($provider->plugins() as $plugin) {
36 62
            if (! $plugin->has()) {
37 62
                continue;
38
            }
39
40 62
            foreach ($plugin->files() as $source => $target) {
41 62
                $this->collectSource($provider, $source, $target);
42 62
                $this->collectLocales($provider, $source, $target);
43
            }
44
        }
45 62
    }
46
47 62
    public function finish(): void
48
    {
49 62
        foreach ($this->compare() as $filename => $values) {
50 62
            $path = $this->resourcesPath($filename);
51
52 62
            Filesystem::store($path, $values);
53
        }
54 62
    }
55
56 62
    protected function collectSource(Provider $provider, string $source, string $target): void
57
    {
58 62
        $path = $this->vendorPath($provider->name(), Path::SOURCE, $source);
59
60 62
        $content = Filesystem::load($path);
61
62 62
        if ($this->hasJson($source)) {
63 62
            $content = $this->resolveKeys($content);
64
        }
65
66 62
        $this->setResourceKeys($target, $this->getKeysOnly($content));
67 62
        $this->setResource(Locales::ENGLISH, $target, $content);
68 62
    }
69
70 62
    protected function collectLocales(Provider $provider, string $source, string $target): void
71
    {
72 62
        foreach ($this->locales as $locale) {
73 62
            $this->collectLocale($provider, $locale, $source, $target);
74
        }
75 62
    }
76
77 62
    protected function collectLocale(Provider $provider, string $locale, string $source, string $target): void
78
    {
79 62
        $path = $this->hasJson($source)
80 62
            ? $this->vendorPath($provider->name(), Path::LOCALES, $locale, $locale . '.json')
81 62
            : $this->vendorPath($provider->name(), Path::LOCALES, $locale, $source);
82
83 62
        $content = Filesystem::load($path);
84
85 62
        $this->setResource($locale, $target, $content);
86 62
    }
87
88 62
    protected function resolveKeys(array $array): array
89
    {
90 62
        return Arrayable::of($array)
91 62
            ->renameKeys(static function ($key, $value) {
92 62
                return is_numeric($key) && is_string($value) ? $value : $key;
93 62
            })->get();
94
    }
95
}
96