Passed
Push — 10.x ( e98e33...d7c7ca )
by Andrey
14:24
created

Base   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 26
dl 0
loc 69
rs 10
c 1
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A store() 0 8 2
A getProcessor() 0 10 2
A collecting() 0 6 2
A handle() 0 5 1
A targetLocales() 0 3 1
A plugins() 0 3 1
A filter() 0 10 1
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\Contracts\LangPublisher\Processor;
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 Optionable;
34
    use Paths;
35
36
    protected $processor;
37
38
    public function handle()
39
    {
40
        $this->collecting();
41
42
        $this->store($this->filter());
43
    }
44
45
    protected function collecting(): void
46
    {
47
        foreach ($this->plugins() as $provider) {
48
            $this->info('Collecting ' . get_class($provider) . '...');
49
50
            $this->getProcessor()->provider($provider);
51
        }
52
    }
53
54
    protected function filter(): array
55
    {
56
        $this->info('Filtering...');
57
58
        $source     = $this->getProcessor()->source();
59
        $translated = $this->getProcessor()->translated();
60
61
        return Filter::keys($source)
62
            ->translated($translated)
63
            ->get();
64
    }
65
66
    protected function store(array $items): void
67
    {
68
        $this->info('Storing...');
69
70
        foreach ($items as $filename => $values) {
71
            $path = $this->resourcesPath($filename);
72
73
            Filesystem::store($path, $values);
74
        }
75
    }
76
77
    /**
78
     * @return \Helldar\Contracts\LangPublisher\Provider[]
79
     */
80
    protected function plugins(): array
81
    {
82
        return Config::plugins();
83
    }
84
85
    protected function getProcessor(): Processor
86
    {
87
        if (! is_string($this->processor)) {
88
            return $this->processor;
89
        }
90
91
        $locales = $this->targetLocales();
92
        $force   = $this->hasForce();
93
94
        return $this->processor = new $this->processor($locales, $force);
95
    }
96
97
    protected function targetLocales(): array
98
    {
99
        return Locales::installed();
100
    }
101
}
102