Passed
Pull Request — main (#123)
by Andrey
27:30 queued 12:28
created

Base::plugins()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 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\Console;
21
22
use Helldar\LaravelLangPublisher\Concerns\Ask;
23
use Helldar\LaravelLangPublisher\Concerns\Optionable;
24
use Helldar\LaravelLangPublisher\Concerns\Paths;
25
use Helldar\LaravelLangPublisher\Facades\Helpers\Config;
26
use Helldar\LaravelLangPublisher\Facades\Helpers\Locales;
27
use Helldar\LaravelLangPublisher\Facades\Support\Filesystem;
28
use Helldar\LaravelLangPublisher\Facades\Support\Filter;
29
use Illuminate\Console\Command;
30
31
abstract class Base extends Command
32
{
33
    use Ask;
34
    use Optionable;
35
    use Paths;
36
37
    protected $processor;
38
39
    public function handle()
40
    {
41
        $this->resolveProcessor();
42
43
        $this->collecting();
44
45
        $this->store($this->filter());
46
    }
47
48
    protected function resolveProcessor(): void
49
    {
50
        $locales = $this->targetLocales();
51
        $force   = $this->hasForce();
52
53
        $this->processor = new $this->processor($locales, $force);
54
    }
55
56
    protected function collecting(): void
57
    {
58
        foreach ($this->plugins() as $provider) {
59
            $this->info('Collecting ' . get_class($provider) . '...');
60
61
            $this->processor->provider($provider);
62
        }
63
    }
64
65
    protected function filter(): array
66
    {
67
        $this->info('Filtering...');
68
69
        $source     = $this->processor->source();
70
        $translated = $this->processor->translated();
71
72
        return Filter::keys($source)
73
            ->translated($translated)
74
            ->get();
75
    }
76
77
    protected function store(array $items): void
78
    {
79
        $this->info('Storing...');
80
81
        foreach ($items as $filename => $values) {
82
            $path = $this->resourcesPath($filename);
83
84
            Filesystem::store($path, $values);
85
        }
86
    }
87
88
    /**
89
     * @return \Helldar\Contracts\LangPublisher\Provider[]
90
     */
91
    protected function plugins(): array
92
    {
93
        return Config::plugins();
94
    }
95
96
    protected function targetLocales(): array
97
    {
98
        return Locales::installed();
99
    }
100
}
101