Passed
Push — 10.x ( 0db173...bfcd3c )
by Andrey
10:43
created

BaseProcessor::set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 3
dl 0
loc 5
rs 10
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\Plugin;
23
use Helldar\Contracts\LangPublisher\Processor;
24
use Helldar\Contracts\LangPublisher\Provider;
25
use Helldar\LaravelLangPublisher\Concerns\Has;
26
use Helldar\LaravelLangPublisher\Concerns\Paths;
27
use Helldar\LaravelLangPublisher\Constants\Locales;
28
use Helldar\LaravelLangPublisher\Constants\Path;
29
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem;
30
use Helldar\Support\Facades\Helpers\Ables\Arrayable;
31
use Illuminate\Support\Arr;
32
use Illuminate\Support\Str;
33
34
abstract class BaseProcessor implements Processor
35
{
36
    use Has;
37
    use Paths;
38
39
    protected $force = false;
40
41
    protected $locales = [];
42
43
    protected $source_keys = [];
44
45
    protected $translates = [];
46
47
    public function locales(array $locales): Processor
48
    {
49
        $this->locales = $locales;
50
51
        return $this;
52
    }
53
54
    public function provider(Provider $provider): Processor
55
    {
56
        foreach ($provider->plugins() as $plugin) {
57
            if (! $plugin->has()) {
58
                continue;
59
            }
60
61
            foreach ($plugin->source() as $source) {
62
                $this->collectSource($provider, $source, $plugin->target());
63
            }
64
65
            $this->collectLocales($provider, $plugin);
66
        }
67
68
        return $this;
69
    }
70
71
    public function store(): void
72
    {
73
        // TODO: Implement store() method.
74
    }
75
76
    public function hasForce(bool $force = false): Processor
77
    {
78
        $this->force = $force;
79
80
        return $this;
81
    }
82
83
    protected function collectSource(Provider $provider, string $source, string $target): void
84
    {
85
        $path = $this->path($provider->basePath(), Path::SOURCE, $source);
86
87
        $filename = $this->preparePath($target, Locales::ENGLISH);
88
89
        $content = Filesystem::load($path);
90
91
        if ($this->hasJson($source)) {
92
            $content = $this->resolveKeys($content);
93
        }
94
95
        $this->set($this->source_keys, $filename, $this->getKeysOnly($content));
96
        $this->set($this->translates, Locales::ENGLISH, $content);
97
    }
98
99
    protected function collectLocales(Provider $provider, Plugin $plugin): void
0 ignored issues
show
Unused Code introduced by
The parameter $provider is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    protected function collectLocales(/** @scrutinizer ignore-unused */ Provider $provider, Plugin $plugin): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $plugin is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

99
    protected function collectLocales(Provider $provider, /** @scrutinizer ignore-unused */ Plugin $plugin): void

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
100
    {
101
        foreach ($this->locales as $locale) {
102
103
        }
104
    }
105
106
    protected function set(array &$array, string $key, array $values): void
107
    {
108
        $loaded = Arr::get($array, $key, []);
109
110
        Arr::set($array, $key, array_merge_recursive($loaded, $values));
111
    }
112
113
    protected function preparePath(string $path, string $locale): string
114
    {
115
        return Str::replace('{locale}', $locale, $path);
116
    }
117
118
    protected function resolveKeys(array $array): array
119
    {
120
        return Arrayable::of($array)
121
            ->renameKeys(static function ($key, $value) {
122
                return is_numeric($key) && is_string($value) ? $value : $key;
123
            })->get();
124
    }
125
126
    protected function getKeysOnly(array $array): array
127
    {
128
        $result = [];
129
130
        foreach ($array as $key => $value) {
131
            if (is_array($value)) {
132
                $result[$key] = $this->getKeysOnly($array);
133
134
                continue;
135
            }
136
137
            array_push($result, $key);
138
        }
139
140
        return $result;
141
    }
142
}
143